Перейти к содержимому
RunStack
Вики· Обновлено 19 окт. 2026 г.

Шпаргалка: nginx

nginx: проверка конфига, перезагрузка, reverse proxy, HTTPS, редиректы, лимиты, логи.

Управление

nginx -t                                   # проверить конфиг
nginx -t && systemctl reload nginx         # применить без простоя
nginx -T | less                            # весь итоговый конфиг
systemctl status nginx
tail -f /var/log/nginx/access.log /var/log/nginx/error.log
ls /etc/nginx/sites-enabled/ /etc/nginx/conf.d/
ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled/app

Reverse proxy

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;
    }
}

WebSocket (чаты, панели, Uptime Kuma) — добавить в location:

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 3600s;

HTTPS (Let's Encrypt)

apt install certbot python3-certbot-nginx
certbot --nginx -d app.example.com -d www.app.example.com
certbot renew --dry-run                    # проверить продление
certbot certificates                       # что выпущено и до когда
server {
    listen 443 ssl;
    http2 on;
    server_name app.example.com;
    ssl_certificate     /etc/letsencrypt/live/app.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem;
    add_header Strict-Transport-Security "max-age=31536000" always;
}

Редиректы

# http → https
server { listen 80; server_name app.example.com; return 301 https://$host$request_uri; }

# www → без www
server { listen 443 ssl; server_name www.example.com; return 301 https://example.com$request_uri; }

# старый путь → новый
location = /old { return 301 /new; }

Статика и SPA

root /var/www/app;
location / { try_files $uri $uri/ /index.html; }
location ~* \.(js|css|png|jpg|svg|woff2)$ { expires 30d; access_log off; }

Лимиты и доступ

client_max_body_size 100m;                 # размер загрузки (413 Request Entity Too Large)

# в http {}
limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;
# в location
limit_req zone=login burst=5 nodelay;

# только свои IP
allow 203.0.113.10;
allow 10.0.0.0/8;
deny all;

# пароль
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;

Файл паролей без пакета apache2-utils — генератор htpasswd.

Частые ошибки

ОшибкаГде смотреть
502 Bad Gatewayприложение не слушает адрес из proxy_pass: ss -ltnp | grep 3000
504 Gateway Timeoutприложение долго отвечает: proxy_read_timeout 120s;
413client_max_body_size
редирект по кругуприложение за прокси не видит HTTPS: X-Forwarded-Proto