工具 & 效率9 分钟阅读
Nginx 运维命令 100 条
本文整理 Nginx 安装信息、配置校验、静态服务、反向代理、负载均衡、TLS、缓存、日志和故障排查常用的 100 条命令与配置片段。示例按开源版 Nginx 编写,模块可用性取决于构建参数。
2026年8月19日阅读—点赞—收藏—
dba100nginx墨力计划
在知识库中专注阅读,并随时返回相关工具与课程
本文整理 Nginx 安装信息、配置校验、静态服务、反向代理、负载均衡、TLS、缓存、日志和故障排查常用的 100 条命令与配置片段。示例按开源版 Nginx 编写,模块可用性取决于构建参数。
本文整理 Nginx 安装信息、配置校验、静态服务、反向代理、负载均衡、TLS、缓存、日志和故障排查常用的 100 条命令与配置片段。示例按开源版 Nginx 编写,模块可用性取决于构建参数。
1nginx -v1nginx -V1nginx -h1sudo nginx -t1sudo nginx -T1sudo nginx -c /etc/nginx/nginx.conf1nginx -p /opt/nginx/ -c conf/nginx.conf1sudo nginx -s reload1sudo nginx -s quit1ps -ef | grep '[n]ginx'1systemctl status nginx --no-pager1sudo systemctl enable --now nginx1sudo systemctl reload nginx1sudo systemctl restart nginx1journalctl -u nginx -n 200 --no-pager1ss -lntp | grep nginx1lsof -p "$(cat /run/nginx.pid)" | grep nginx.conf1cat /proc/$(cat /run/nginx.pid)/limits1curl -I http://127.0.0.1/1curl -I -H 'Host: app.example.com' http://127.0.0.1/1listen 80;1server_name app.example.com;1root /srv/www/app;1index index.html index.htm;1location = /health { return 200 "ok\n"; }1location / { try_files $uri $uri/ /index.html; }1autoindex off;1include /etc/nginx/mime.types;1client_max_body_size 50m;1location ~* \.(css|js|png|jpg|svg)$ { expires 7d; add_header Cache-Control "public"; }1location /api/ { proxy_pass http://127.0.0.1:8080; }1proxy_set_header Host $host;1proxy_set_header X-Real-IP $remote_addr;1proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;1proxy_set_header X-Forwarded-Proto $scheme;1proxy_connect_timeout 5s;1proxy_read_timeout 60s;1proxy_buffering off;1proxy_set_header Upgrade $http_upgrade;1proxy_http_version 1.1;1upstream api_backend { server 10.0.0.11:8080; server 10.0.0.12:8080; }1proxy_pass http://api_backend;1server 10.0.0.11:8080 weight=2;1least_conn;1ip_hash;1hash $request_uri consistent;1server 10.0.0.11:8080 max_fails=3 fail_timeout=30s;1server 10.0.0.13:8080 backup;1keepalive 64;1proxy_next_upstream error timeout http_502 http_503 http_504;1openssl x509 -in /etc/nginx/tls/fullchain.pem -noout -subject -issuer -dates1openssl s_client -connect app.example.com:443 -servername app.example.com </dev/null1listen 443 ssl;1ssl_certificate /etc/nginx/tls/fullchain.pem;1ssl_certificate_key /etc/nginx/tls/privkey.pem;1ssl_protocols TLSv1.2 TLSv1.3;1ssl_session_cache shared:SSL:10m;1add_header Strict-Transport-Security "max-age=31536000" always;1add_header X-Content-Type-Options nosniff always;1add_header Referrer-Policy strict-origin-when-cross-origin always;1allow 10.0.0.0/8;1deny all;1auth_basic "Restricted"; auth_basic_user_file /etc/nginx/.htpasswd;1htpasswd -c /etc/nginx/.htpasswd ops1limit_req_zone $binary_remote_addr zone=api_rate:10m rate=10r/s;1limit_req zone=api_rate burst=20 nodelay;1limit_conn_zone $binary_remote_addr zone=perip:10m;1limit_conn perip 20;1limit_rate 1m;1server_tokens off;1gzip on;1gzip_types text/plain text/css application/json application/javascript;1proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=api_cache:20m max_size=10g inactive=60m;1proxy_cache api_cache;1proxy_cache_valid 200 10m;1proxy_cache_bypass $http_authorization;1add_header X-Cache-Status $upstream_cache_status;1worker_processes auto;1events { worker_connections 4096; }1sendfile on;1access_log /var/log/nginx/access.log combined;1error_log /var/log/nginx/error.log warn;1tail -F /var/log/nginx/error.log1awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c | sort -nr1awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head1awk '{print $7}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head1awk '$9 ~ /^5/' /var/log/nginx/access.log | tail -1001sudo nginx -s reopen1sudo logrotate -d /etc/logrotate.d/nginx1sudo logrotate -f /etc/logrotate.d/nginx1location = /nginx_status { stub_status; allow 127.0.0.1; deny all; }1curl -s http://127.0.0.1/nginx_status1sudo nginx -T 2>&1 | grep '^# configuration file'1sudo nginx -T 2>&1 | grep -n 'listen '1curl -fsS --connect-timeout 3 http://10.0.0.11:8080/health1getent ahosts api.internal1ss -antp | grep nginx1sudo strace -f -p "$(pgrep -n nginx)"1sudo nginx -T 2>&1 > nginx-config.txt1sudo nginx -t && sudo systemctl reload nginxNginx 变更应始终先执行 nginx -t,再平滑加载,并同步检查错误日志、上游健康和 TLS。性能优化应基于实际连接数、响应时间与文件描述符,而不是照搬固定参数。