宝塔面板如何设置网站伪静态 宝塔|Nginx网站部署 伪静态配置|静态资源访问配置

宝塔面板如何设置网站伪静态 宝塔|Nginx网站部署 伪静态配置|静态资源访问配置 1. 伪静态配置# 核心修改解决root冲突 确保所有请求走/web目录 location / { # 1. 明确指定该location的root覆盖server级root root /www/wwwroot/********/web; index index.php index.html index.htm default.php default.htm default.html; # 2. 关键try_files最后回退到index.php兼容PHP若纯前端则保留/index.html try_files $uri $uri/ /index.html; # 3. 强制该location下的所有请求使用当前root避免继承server级root # 针对静态资源的location会自动匹配不影响 } # 补充确保静态资源也使用/web目录的root可选但更稳妥 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { root /www/wwwroot/******/web; # 新增指定静态资源的root expires 30d; error_log /dev/null; access_log /dev/null; } location ~ .*\.(js|css)?$ { root /www/wwwroot/*******/web; # 新增指定静态资源的root expires 12h; error_log /dev/null; access_log /dev/null; }提示nginx 的匹配优先级正则 location~ 普通前缀 location如果有其他的解析路径优先于静态文件的匹配需要在路径前方加 ^~例如location ^~ /statics/ {2. 静态资源访问配置# 专门处理 /profile/ 路径的静态资源 location /profile/ { root /www/wwwroot/********/static; # 注意root 是到 static 目录的上一级 expires 7d; # 浏览器缓存7天 gzip on; # 开启压缩 add_header Cache-Control public, max-age604800; try_files $uri $uri/ 404; # 找不到文件返回404 }