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