Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Know it before you install
One server block = one website (by domain name and port). A location block inside it says what to do with a path: serve files (root) or pass the request to an app (proxy_pass).
| Item | RHEL / Rocky / Alma / Fedora | Ubuntu / Debian |
|---|---|---|
| Package | nginx | nginx |
| Service | nginx.service | nginx.service |
| Main config | /etc/nginx/nginx.conf | /etc/nginx/nginx.conf |
| Your sites | /etc/nginx/conf.d/*.conf | /etc/nginx/sites-available/ + link in sites-enabled/ |
| Default web root | /usr/share/nginx/html | /var/www/html |
| Logs | /var/log/nginx/access.log, error.log | the same |
| Ports | 80 (HTTP), 443 (HTTPS) | the same |
| Runs as | user nginx | user www-data |
Install on RHEL, Rocky, Alma and Fedora
dnf -y install nginx
systemctl enable --now nginx
firewall-cmd --permanent --add-service={http,https}
firewall-cmd --reload
curl -I http://localhost
HTTP/1.1 200 OK
Server: nginx/1.26.3Notitie
RHEL 8 and 9 have more than one version as module streams: dnf module list nginx, then for example dnf module enable nginx:1.24 before the install.
Install on Ubuntu and Debian
apt-get update
apt-get install -y nginx
ufw allow 'Nginx Full'
curl -I http://localhostThe service starts by itself after the install on Ubuntu. Work tip: For the newest version on any distribution, use the official repository from nginx.org (packages nginx mainline or stable).
First configuration: your first site
First, the global settings that every server should have. Add them in the http { } block of nginx.conf (or in a file in conf.d/):
server_tokens off;
client_max_body_size 20m;
gzip on;
gzip_types text/css application/javascript application/json image/svg+xml;| Setting | Why |
|---|---|
server_tokens off | Do not show the Nginx version in errors and headers. |
client_max_body_size 20m | Bigger uploads (default is 1 MB → error 413). |
gzip | Smaller text responses, faster pages. |
A static website:
server {
listen 80;
server_name shop.example.com;
root /var/www/shop;
index index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ /\.(git|env|ht) {
deny all;
}
access_log /var/log/nginx/shop.access.log;
error_log /var/log/nginx/shop.error.log;
}On Ubuntu, put the file in sites-available/ and enable it with ln -s /etc/nginx/sites-available/shop.example.com.conf /etc/nginx/sites-enabled/.
mkdir -p /var/www/shop
echo '<h1>It works</h1>' > /var/www/shop/index.html
restorecon -Rv /var/www/shop
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
systemctl reload nginxOnthoud
Always nginx -t before reload. A reload with a broken config is refused, but a restart with a broken config stops the website.
Reverse proxy to an app
The most common job: your app listens only on 127.0.0.1:3000, and Nginx is the public front.
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
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;
proxy_read_timeout 60s;
}
}On RHEL, SELinux blocks Nginx from connecting to the app until you allow it:
Add HTTPS with Let's Encrypt
The DNS name must point to this server and port 80 must be open. Certbot gets the certificate, changes the Nginx config and renews it automatically.
dnf -y install certbot python3-certbot-nginx
apt-get install -y certbot python3-certbot-nginx
certbot --nginx -d shop.example.com -d www.shop.example.com
certbot renew --dry-runUse the first install line on RHEL (needs the EPEL repository: dnf -y install epel-release on Rocky/Alma), the second on Ubuntu. Then add strong TLS and security headers inside the server { listen 443 ssl; … } block:
ssl_protocols TLSv1.2 TLSv1.3;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;Load it with include /etc/nginx/snippets/security.conf;.
Daily use: the commands and the real uses
nginx -t
systemctl reload nginx
nginx -T | less
tail -f /var/log/nginx/error.log
tail -f /var/log/nginx/access.log
curl -I https://shop.example.com
curl -s -o /dev/null -w '%{http_code} %{time_total}\n' https://shop.example.comReal uses at work:
- Reverse proxy and TLS end for apps and containers (Docker publishes the app on
127.0.0.1only). - Load balancing with an
upstreamblock over many app servers. - Rate limits on login pages (
limit_req), redirects, caching of static files. - In Kubernetes: the ingress-nginx controller is Nginx.
Make it safe
Security checklist:
nginx -tbefore every reload; keep the config in Git.- HTTPS only: redirect 80 → 443, TLS 1.2+, HSTS.
server_tokens off, security headers, deny/.gitand/.env.- No
autoindex onon real sites. - Rate limit login and API paths:
limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;. - Apps listen on
127.0.0.1, only Nginx is public. - Keep SELinux on; use
setseboolandrestoreconinstead of turning it off.
Update and remove
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
| 502 Bad Gateway | The app is down, wrong port, or SELinux blocks the connection. | ss -ltnp for the app port; curl 127.0.0.1:3000; setsebool -P httpd_can_network_connect 1. |
| 504 Gateway Timeout | The app answers too slowly. | Make the app faster or raise proxy_read_timeout. |
| 403 Forbidden | No index file, file rights, or SELinux label. | Check index; namei -l /var/www/shop/index.html; restorecon -Rv /var/www/shop. |
| 413 Request Entity Too Large | Upload bigger than client_max_body_size. | Raise it (for example 20m) and reload. |
bind() to 0.0.0.0:80 failed (Address already in use) | Apache or another program uses port 80. | ss -ltnp 'sport = :80'; stop the other service. |
conflicting server name warning | Two server blocks have the same name. | Remove the duplicate (often the default site on Ubuntu). |
| Changes do not show | You did not reload, or the browser/CDN cache. | nginx -t && systemctl reload nginx; test with curl -I. |
Next steps
- Quick how-tos: Set up Nginx as a reverse proxy, Get a free TLS certificate, Find slow requests in the logs, Redirect HTTP to HTTPS, Check the TLS settings, Security headers, Rate limits, Cache static files, Two websites on one server.
- Every option and example: Command page: nginx, Command page: certbot.
- Nginx and the other proxies at work: Web, proxy and TLS services, Traffic tools.
- Practise: Lab: web, proxy and TLS.
1Nginx shows 502 for a proxied app on RHEL, but curl 127.0.0.1:3000 works. What is the likely cause?
SELinux. Allow it with setsebool -P httpd_can_network_connect 1.
2Why reload and not restart after a config change?
Reload keeps the old workers until the new config is loaded, so no request is lost, and a broken config is refused.