nginx i reverse proxy
nginx często stoi na granicy między internetem a aplikacją. Może serwować pliki statyczne, kończyć TLS, przekazywać ruch do backendów i rozdzielać kilka usług działających na jednym adresie IP.
Powiązane tematy: HTTP, HTTPS i TLS, DNS, domeny i routing, Docker, Debian - desktop i serwer oraz Bezpieczeństwo aplikacji webowych.
1. Do czego służy nginx
nginx może działać jako: - serwer statycznych plików, - reverse proxy, - terminator TLS, - load balancer, - proxy cache.
Typowy układ:
Internet
↓
nginx :80/:443
├─ app1 → localhost:8080
├─ app2 → localhost:3000
└─ pliki statyczne
2. Instalacja Debian
sudo apt update
sudo apt install nginx
sudo systemctl enable --now nginx
Status:
systemctl status nginx
3. Najważniejsze katalogi na Debianie
/etc/nginx/nginx.conf
/etc/nginx/sites-available/
/etc/nginx/sites-enabled/
/var/log/nginx/access.log
/var/log/nginx/error.log
/var/www/
4. Minimalny virtual host
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Aktywacja:
sudo ln -s /etc/nginx/sites-available/example \
/etc/nginx/sites-enabled/example
Test:
sudo nginx -t
Reload:
sudo systemctl reload nginx
5. Reverse proxy
Backend Go działa na:
127.0.0.1:8080
nginx:
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://127.0.0.1:8080;
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;
}
}
6. Kilka aplikacji na jednym IP
app1.example.com → 127.0.0.1:8080
app2.example.com → 127.0.0.1:3000
api.example.com → 127.0.0.1:9000
nginx rozróżnia je po server_name.
7. HTTPS
Z Certbotem:
sudo certbot --nginx -d app.example.com
Po konfiguracji nginx zwykle nasłuchuje na 443 ssl.
8. Redirect HTTP → HTTPS
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
9. Pliki statyczne
location /assets/ {
alias /srv/myapp/assets/;
}
root i alias działają inaczej. Przy alias ścieżka jest podstawiana bezpośrednio.
10. SPA
Dla React/Vue:
location / {
try_files $uri $uri/ /index.html;
}
Dzięki temu routing po stronie klienta działa po odświeżeniu.
11. Proxy WebSocket
location /ws/ {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
12. Limity uploadu
client_max_body_size 20M;
13. Timeout
proxy_connect_timeout 5s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
14. Cache statycznych plików
location ~* \.(css|js|png|jpg|jpeg|gif|svg|webp)$ {
expires 30d;
add_header Cache-Control "public";
}
15. Logi
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log
Filtrowanie:
grep ' 500 ' /var/log/nginx/access.log
16. Diagnostyka
Najpierw:
sudo nginx -t
systemctl status nginx
Potem:
curl -I http://127.0.0.1:8080
curl -I https://app.example.com
Jeżeli backend działa lokalnie, ale publicznie jest 502, szukaj w nginx/proxy.
17. Security headers - podstawy
Przykładowo:
add_header X-Content-Type-Options nosniff always;
add_header Referrer-Policy strict-origin-when-cross-origin always;
CSP konfiguruj świadomie pod aplikację.
18. Ukrywanie wersji
server_tokens off;
19. Load balancing
upstream backend {
server 127.0.0.1:8081;
server 127.0.0.1:8082;
}
server {
location / {
proxy_pass http://backend;
}
}
20. nginx + Docker
Container może wystawiać port tylko lokalnie:
127.0.0.1:8080:8080
nginx na hoście:
proxy_pass http://127.0.0.1:8080;
21. Typowy flow deploymentu
- aplikacja działa lokalnie,
- budujesz binarkę lub kontener,
- uruchamiasz backend na localhost,
- DNS wskazuje VPS,
- nginx kieruje domenę na backend,
- dodajesz TLS,
- sprawdzasz logi i healthcheck.
22. Najważniejsze komendy
sudo nginx -t
sudo systemctl reload nginx
sudo systemctl restart nginx
systemctl status nginx
journalctl -u nginx
tail -f /var/log/nginx/error.log
23. Co trzeba umieć
- wystawić statyczną stronę,
- skonfigurować reverse proxy,
- obsłużyć kilka domen na jednym VPS,
- dodać HTTPS,
- znaleźć przyczynę 502/504,
- czytać access/error logs.
Oficjalne źródła
- nginx documentation: https://nginx.org/en/docs/
- nginx Beginner's Guide: https://nginx.org/en/docs/beginners_guide.html
- ngx_http_proxy_module: https://nginx.org/en/docs/http/ngx_http_proxy_module.html