#linux #network

Nginx("engine x") is an HTTP web server, reverse proxy, content cache, load balancer, TCP/UDP proxy server, and mail proxy server.

1. Locate Configuration Files

  • Main config: /etc/nginx/nginx.conf
  • Site configs:
    • Available: /etc/nginx/sites-available/
    • Enabled: /etc/nginx/sites-enabled/


2. Create a New Server Block

sudo vim /etc/nginx/sites-available/example.com

add:
server {
    listen 80;
    server_name example.com www.example.com;

    location / {
        root /var/www/example.com;
        index index.html index.htm;
    }
}


3. Enable the Server Block

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/


4. Test and Reload Nginx

sudo nginx -t
sudo systemctl reload nginx


5. Create New Rules

Example Redirect HTTP to HTTPS
server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}


Further information: Documentation