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:
- 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.
- 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.
- 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.