• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

Nginx for Newbie: setting up server directive

December 18, 2023

Nginx for Newbies: Setting Up Server Directive

Nginx is a powerful web server and reverse proxy server that has gained popularity for its high performance, scalability, and ease of configuration. If you are new to Nginx, understanding how to set up the server directive is essential for getting started with this web server.

What is the Server Directive?

In Nginx, the server directive is used to define the configuration for a specific virtual server or a group of virtual servers. Each server block is identified by a listen directive that specifies the IP address and port number on which the server should listen for incoming requests.

Here is an example of a basic server block:

server {
    listen 80;
    server_name example.com;
    root /var/www/html;
    index index.html;
}

In this example, the server block listens on port 80 and is associated with the domain name example.com. The root directive specifies the root directory for serving files, and the index directive specifies the default file to be served when a directory is requested.

Configuring Multiple Server Blocks

Nginx allows you to configure multiple server blocks to host multiple websites or applications on the same server. Each server block should have a unique listen directive to differentiate them.

Here is an example of configuring multiple server blocks:

server {
    listen 80;
    server_name example.com;
    root /var/www/html;
    index index.html;
}

server {
    listen 80;
    server_name blog.example.com;
    root /var/www/blog;
    index index.php;
}

In this example, we have two server blocks. The first server block is associated with the domain name example.com, and the second server block is associated with the subdomain blog.example.com. Each server block has its own root directory and index file.

Using Server Blocks for Load Balancing

One of the powerful features of Nginx is its ability to act as a load balancer. By configuring multiple server blocks with the same listen directive, you can distribute incoming requests across multiple backend servers.

Here is an example of using server blocks for load balancing:

http {
    upstream backend {
        server backend1.example.com;
        server backend2.example.com;
        server backend3.example.com;
    }

    server {
        listen 80;
        server_name example.com;
        location / {
            proxy_pass http://backend;
        }
    }
}

In this example, the upstream directive defines a group of backend servers. The server block listens on port 80 and proxies the incoming requests to the backend servers defined in the upstream directive.

Conclusion

The server directive is a fundamental component of Nginx configuration. It allows you to define virtual servers, configure multiple websites or applications on the same server, and even set up load balancing. Understanding how to set up the server directive is crucial for effectively using Nginx as a web server or reverse proxy server.

If you are interested in exploring more about Nginx and its capabilities, consider checking out Server.HK, a leading VPS hosting provider that offers reliable and high-performance hosting solutions.

Recent Posts

  • How to Migrate Your Website to a Hong Kong VPS: Zero-Downtime Transfer Guide (2026)
  • How to Set Up Redis on Hong Kong VPS: Caching, Queues, and Session Storage (2026)
  • How to Host a Python Flask or Django Application on Hong Kong VPS (2026)
  • How to Set Up WireGuard VPN on a Hong Kong VPS: Step-by-Step Guide 2026
  • Hong Kong VPS vs DigitalOcean: Cost, Performance, and China Routing Compared (2026)

Recent Comments

  1. vibramycin injection on How to Choose the Right Hong Kong VPS Plan: A Buyer’s Guide for 2026
  2. allopurinol for gout on CN2 GIA vs BGP vs CN2 GT: What’s the Real Difference for China Connectivity?
  3. antibiotics online purchase on How to Set Up a WordPress Site on a Hong Kong VPS with aaPanel (Step-by-Step 2026)
  4. linezolid cost oral on Top 5 Use Cases for a Hong Kong Dedicated Server in 2026
  5. metoprolol generic on Hong Kong VPS vs Japan VPS: Head-to-Head for Asia-Pacific Deployments in 2026

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot