walkingluo

walkingluo

Nginx Basic Configuration

Nginx is an open-source high-performance HTTP server and reverse proxy server that can be used to configure and manage the traffic of web applications. Here are some examples of Nginx configurations:

  1. Using Nginx as a web server
server {
    listen 80;
    server_name example.com;
    root /var/www/html/example;
    index index.html;
     
    location / {
        try_files $uri $uri/ =404;
    }
}  

In this example, Nginx is configured to listen on port 80 and redirect requests to the index.html file in the /var/www/html/example directory. If the requested file does not exist, a 404 error will be returned.

  1. Reverse proxy server
server {
    listen 80;
    server_name example.com;
     
    location / {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

In this example, Nginx is configured to forward requests to port 8080 on the local host. By setting proxy_set_header, information from the original request such as the hostname and IP address can be passed to the backend server.

  1. Load balancing
upstream backend {
    server backend1.example.com;
    server backend2.example.com;
    server backend3.example.com;
}
  
server {
    listen 80;
     
    location / {
        proxy_pass http://backend;
    }
}

In this example, the upstream directive is used to define a load balancer named backend that distributes requests to 3 backend servers. In the server code block, the proxy_pass directive is used to forward requests to the load balancer.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.