中资源虚拟主机Nginx默认站点_location与rewrite
很多站长遇到Nginx默认站点直接重启服务器,配置错的重启后还是起不来。正确做法是nginx -t或apachectl configtest看报错行,修正后reload。reload不中断服务,restart会短暂中断,生产环境用reload。
Nginx配置结构:main(全局)→ http(HTTP全局)→ server(虚拟主机)→ location(URL匹配)。每个块用花括号包裹,指令以分号结尾。include可以引入子配置。nginx -t检测语法,nginx -s reload重载。配置文件在 /etc/nginx/nginx.conf或宝塔的 /www/server/panel/vhost/nginx/。
参考(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错误日志速查:"emerg" 配置语法错(nginx -t看行号)、"alert" 权限问题、"crit" 资源不足、"error" 一般错误(404/403/502)、"warn" 警告。日志级别在error_log指令设置,生产环境用warn或error,调试用debug。

更新时间:2026-08-27 10:18:26
上一篇:360浏览器提示网站不安全常见场景_从检测到解除_完整方案