nginx 正则匹配 location 访问 .css .js报404问题解决


#故障分析
1,nginx 在一个域名的情况下,会根据 location去匹配不同的目录。或代理到不同的应用上。
2,部分测试匹配规则,具体解释详见另一篇nginx配置详解的文章

##20190321_test
    
        ##location = /fuhai/ {    ##1,测试无效
        #location ~* /fuhai/ {    ##2,可以访问test.html 但无法访问.css .js 的文件
        #location ~ /fuhai/ {     ##3,同上
        #location ^~ /fuhai/ {    ##4,同上
        
            root   /data/nginx/files/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    #location ~* \.(css|js)$ {    ##5,貌似解决不了匹配问题
    #    root   /data/nginx/files/fuhai/static/;
    #}
    location ~* /static {    ##6,解决.css .js文件所在目录
        root   /data/nginx/files/fuhai/;
    }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

解决办法:

第一种办法,是开发在代码中将.css .js所在的文件夹目录改为当前目录,见截图:

修改后:

上面这种方式nginx,只需配置第2;3;4方式均可解决。

第二种方法,通过nginx来处理:

#20190321_test
    
        ##location = /fuhai/ {
        #location ~ /fuhai/ {
        #location ^~ /fuhai/ {

        location ~* /fuhai/ {
            root   /data/nginx/files/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    
    location ~* /static {  
        root   /data/nginx/files/fuhai/;
    }

## location ~* /static { 为了处理.css .js 文件用

访问测试:

测试成功。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM