NginxNginx proxy_set_header_安全加固
很多站长遇到Nginx proxy_set_header直接重启服务器,配置错的重启后还是起不来。正确做法是nginx -t或apachectl configtest看报错行,修正后reload。reload不中断服务,restart会短暂中断,生产环境用reload。
反向代理配置:Nginx location / { proxy_pass http://后端IP:端口; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }。后端没启动或端口不通会502,后端处理慢会504。proxy_read_timeout增大超时。
参考(Nginx配置与排查): ``` # 语法检测与重载 nginx -t nginx -s reload # 或 systemctl reload nginx
# 服务状态 systemctl status nginx systemctl restart nginx
# 错误日志 tail -50 /var/log/nginx/error.log tail -100 /www/wwwlogs/错误日志.log
# 端口监听 netstat -tlnp | grep nginx ss -tlnp | grep :80
# Nginx站点配置示例 server { listen 80; server_name 域名; root /www/wwwroot/域名; index index.html index.php;
# 伪静态 location / { try_files $uri $uri/ /index.php?$query_string; }
# PHP location ~ .php$ { fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; }
# 上传大小 client_max_body_size 100m;
# 禁止敏感文件 location ~* .(sql|bak|log|sh)$ { deny all; } } ```
安全配置:server_tokens off隐藏版本号、add_header X-Frame-Options SAMEORIGIN防点击劫持、add_header X-Content-Type-Options nosniff、SSL用TLS1.2+、禁止敏感文件访问(location ~* .(sql|bak|log)$ { deny all; })、后台路径IP白名单、限流limit_req。
Nginx与Apache区别:Nginx事件驱动异步非阻塞,高并发性能好,配置简洁,不支持 .htaccess;Apache进程/线程驱动,稳定,支持 .htaccess和丰富模块,高并发内存占用大。静态资源和高并发选Nginx,需要 .htaccess和复杂模块选Apache,也可以Nginx反代Apache。

更新时间:2026-08-26 21:40:11
上一篇:宝塔FTP修改密码后无法登录:缓存与配置重载_宝塔面板_服务器运维_配置教程