Skip to content

Nginx Basics

What Nginx Is Used For

Nginx is most often used as:

  • reverse proxy
  • static web server
  • TLS termination point
  • load balancer between upstream services

Basic Commands

nginx -v
nginx -t
nginx -T
systemctl status nginx
systemctl reload nginx
systemctl restart nginx

Common Paths

  • /etc/nginx/nginx.conf
  • /etc/nginx/conf.d/*.conf
  • /etc/nginx/sites-available/
  • /etc/nginx/sites-enabled/
  • /var/log/nginx/access.log
  • /var/log/nginx/error.log

Minimal Reverse Proxy

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Static Content

server {
    listen 80;
    server_name static.example.com;
    root /srv/static;
    index index.html;
}

TLS

server {
    listen 443 ssl http2;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:8000;
    }
}

Troubleshooting

nginx -t
tail -f /var/log/nginx/error.log
tail -f /var/log/nginx/access.log
curl -I http://127.0.0.1
curl -H 'Host: example.com' http://127.0.0.1

Common Issues

  • the upstream listens on the wrong address or port
  • missing proxy_set_header Host
  • broken TLS chain or wrong certificate path
  • conflicting server_name directives
  • SELinux or firewall blocks access to the backend

Practice

  • Always run nginx -t before reload.
  • Review both access and error logs together.
  • If Nginx runs inside Docker or Kubernetes, also verify service routing and network policy, not just the config itself.