1. 程式人生 > >nginx做靜態代理時css載入不出問題解決

nginx做靜態代理時css載入不出問題解決

有次專案中用到了前後端分離,nginx做了前端的靜態代理。當配置nginx後,訪問頁面時出現了以下的bug

這裡寫圖片描述

css檔案實際上已經被加載出來了,但是頁面卻沒有顯示效果。報錯是這樣的:
Resource interpreted as Stylesheet but transferred with MIME type text/plain

nginx的配置如下:

events {
    worker_connections  1024;
}

http {

    upstream disconf {
    server 127.0.0.1:8086;
   }

server {
    listen   80
; server_name disconf.com; access_log /nginx-1.10.1/nginx-1.10.1/logs; error_log /nginx-1.10.1/nginx-1.10.1/logs; location / { allow all; if ($query_string) { expires max; } } set $baseUrl D:\disconf_html\html; root $baseUrl; ssi on; location ~*
/login.html { rewrite ^ http://xxxxxxxxx; } location ~ ^/(api|export) { proxy_pass_header Server; proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; proxy_pass http://disconf;
} } }

在經過好幾個小時的排查後,終於特麼知道了是什麼原因導致的。原來我http節點裡設定以下兩個屬性:

include mime.types;

default_type application/octet-stream;

如果不設定css檔案就會被當作text/plain型別傳送到瀏覽器。從而導致css檔案服務顯示。設定也比較簡單,以下為更新後的nginx.conf檔案:

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    upstream disconf {
    server 127.0.0.1:8086;
   }

server {
    listen   80;
    server_name disconf.com;
    access_log /nginx-1.10.1/nginx-1.10.1/logs;
    error_log /nginx-1.10.1/nginx-1.10.1/logs;

    location / {
        allow all;
        if ($query_string) {
            expires max;
        }
    }
    set $baseUrl  D:\disconf_html\html;
    root $baseUrl;
    ssi on;

    location ~* /login.html {
        rewrite ^ http://xxxxxxxxx;
    }   

    location ~ ^/(api|export) {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_pass http://disconf;
    }   
  }
}

執行 nginx.exe -s reload命令後,再次訪問頁面後發現:
這裡寫圖片描述
因缺思廳。問題解決的感覺真爽!