Geavanceerd
Zoeken inInstallatiegidsen
Zo zoek je
  • Plak een foutmelding tussen aanhalingstekens, bijv. "permission denied", om de oplossingstabel te vinden.
  • Kies een gebied en een deel van de gids (installeren, eerste instellingen, dagelijks gebruik, beveiliging, oplossingen) om direct naar de juiste stap te gaan.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Gids 4Nginx · Web

Install and set up Nginx (web server and reverse proxy)

Nginx is the most used web server on the internet. In DevOps work it is mostly the front door: it ends HTTPS, sends requests to your apps (Node, Python, PHP, containers) and serves static files fast. This guide installs Nginx on RHEL and Ubuntu, builds a first site and a reverse proxy, adds free HTTPS with Certbot, and gives the security settings and fixes for 502 and 403 errors.

  • 11 Lessen
  • 16 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

4.1

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

ItemRHEL / Rocky / Alma / FedoraUbuntu / Debian
Packagenginxnginx
Servicenginx.servicenginx.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.logthe same
Ports80 (HTTP), 443 (HTTPS)the same
Runs asuser nginxuser www-data

Commando's in deze les

4.2

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

Notitie

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.

4.3

Install on Ubuntu and Debian

apt-get update
apt-get install -y nginx
ufw allow 'Nginx Full'
curl -I http://localhost

The 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).

Commando's in deze les

4.4

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/):

/etc/nginx/conf.d/00-global.conf
server_tokens off;
client_max_body_size 20m;
gzip on;
gzip_types text/css application/javascript application/json image/svg+xml;
SettingWhy
server_tokens offDo not show the Nginx version in errors and headers.
client_max_body_size 20mBigger uploads (default is 1 MB → error 413).
gzipSmaller text responses, faster pages.

A static website:

/etc/nginx/conf.d/shop.example.com.conf
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 nginx

Onthoud

Always nginx -t before reload. A reload with a broken config is refused, but a restart with a broken config stops the website.

4.5

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.

/etc/nginx/conf.d/app.example.com.conf
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:

setsebool -P httpd_can_network_connect 1
nginx -t && systemctl reload nginx

4.6

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-run

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

/etc/nginx/snippets/security.conf
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;.

4.7

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

Real uses at work:

  • Reverse proxy and TLS end for apps and containers (Docker publishes the app on 127.0.0.1 only).
  • Load balancing with an upstream block over many app servers.
  • Rate limits on login pages (limit_req), redirects, caching of static files.
  • In Kubernetes: the ingress-nginx controller is Nginx.

4.8

Make it safe

Security checklist:

  • nginx -t before every reload; keep the config in Git.
  • HTTPS only: redirect 80 → 443, TLS 1.2+, HSTS.
  • server_tokens off, security headers, deny /.git and /.env.
  • No autoindex on on 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 setsebool and restorecon instead of turning it off.

4.9

Update and remove

dnf -y upgrade nginx
apt-get install --only-upgrade -y nginx
dnf -y remove nginx
apt-get purge -y nginx nginx-common

Commando's in deze les

4.10

Fix common problems

ProblemCauseFix
502 Bad GatewayThe 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 TimeoutThe app answers too slowly.Make the app faster or raise proxy_read_timeout.
403 ForbiddenNo 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 LargeUpload 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 warningTwo server blocks have the same name.Remove the duplicate (often the default site on Ubuntu).
Changes do not showYou did not reload, or the browser/CDN cache.nginx -t && systemctl reload nginx; test with curl -I.

4.11

Next steps

1Nginx shows 502 for a proxied app on RHEL, but curl 127.0.0.1:3000 works. What is the likely cause?
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.

Alle commando's in dit hoofdstuk