nginx靜態資源配置

解決EE工程中靜態文件顯示問題

在工程中本地測試沒有問題,發現使用nginx配置了路徑的頁面,會獲取不到相應頁面的靜態文件問題

靜態文件的路徑相似爲: http://localhost:8080/salama/static/js/lib/jquery.js
引用此靜態文件的實際路徑爲: http://localhost:8080/salama/userInfo/?log=aaa&gender=1&city=bj (訪問此頁面會傳遞部分參數,而後打開一個調查問卷頁面)
如今給出一個二級域名: api.salama.com 直接使用域名能訪問到頁面,經過nginx的配置隱藏工程名稱和第一級目錄名稱(salama/userInfo)html

nginx的配置爲:jquery

server {
        listen 80;
        server_name api.salama.com;
        location / {
                index index.html index.htm index.jsp;
                proxy_pass http://localhost:8080/salama/userInfo/;
        }
    }

這樣配置訪問對應頁面沒有問題,可是沒法獲取到靜態資源文件.
在瀏覽器控制檯看到的靜態資源文件請求路徑爲:nginx

http://api.salama.com/salama/static/js/lib/jquery.js

顯然在api.salama.com返回的頁面中是沒法獲取到個靜態資源文件的,解決辦法以下:api

server {
        listen 80;
        server_name api.salama.com;
        
        location / {
                index index.html index.htm index.jsp;
                proxy_pass http://localhost:8080/salama/userInfo/;
        }

        location /salama/static {
                # 這裏能夠直接寫localhost:8080,nginx會把location後面的參數追加過來,還要注意不要亂添加 /  不然會致使沒法訪問
                proxy_pass http://localhost:8080;       
        }

    }

參考資料:https://www.nginx.com/resources/admin-guide/瀏覽器

相關文章
相關標籤/搜索