我的知识记录

虚拟主机Nginx跨域配置_修复实操

很多站长遇到Nginx跨域配置直接重启服务器,配置错的重启后还是起不来。正确做法是nginx -t或apachectl configtest看报错行,修正后reload。reload不中断服务,restart会短暂中断,生产环境用reload。

Nginx常见指令:root(网站根目录)、index(默认首页)、server_name(域名)、listen(端口)、location(URL匹配,=精确 ^~前缀 ~正则)、try_files(文件查找)、rewrite(URL重写)、proxy_pass(反向代理)、fastcgi_pass(PHP处理)、return(返回状态码)、access_log/error_log(日志)。

参考(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。

Apache .htaccess不生效:检查httpd.conf里Directory段的AllowOverride是否All(None时 .htaccess完全忽略)、mod_rewrite是否开启(httpd -M | grep rewrite)、.htaccess文件权限644、路径是否在网站根目录。虚拟主机用户在面板开启"伪静态"。

虚拟主机Nginx跨域配置_修复实操

标签:

更新时间:2026-08-27 12:26:56

上一篇:个人博客企业官网轻量防护配置标准流程_无需昂贵硬件_拦截90%常规攻击

下一篇:Struts2漏洞修复之升级struts2-core版本的完整步骤_网站安全运维实战指南