宝塔面板如何设置网站伪静态 宝塔|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-age=604800"; try_files $uri $uri/ =404; # 找不到文件返回404 }