Nginx

通過 nginx (proxy_pass) 服務 IIS 網站不顯示 CSS、圖像或 js(404 未找到)

  • October 14, 2015

我有一個在 Ubuntu 14.04 LTS 上執行的 nginx 反向代理。當向 nginx 代理髮出請求時,它會將請求傳遞給包含一個(預設)網站的 IIS 伺服器。它似乎可以很好地呈現 HTML,但不幸的是,伺服器對所有其他資產都響應 404 錯誤。

這是/etc/nginx/nginx.conf文件:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
   worker_connections 768;
   # multi_accept on;
}

http {

   ##
   # Basic Settings
   ##

   sendfile off;
   tcp_nopush on;
   tcp_nodelay on;
   keepalive_timeout 65;
   types_hash_max_size 2048;
   # server_tokens off;

   server_names_hash_bucket_size 64;
   # server_name_in_redirect off;

   include /etc/nginx/mime.types;
   default_type application/octet-stream;

   ##
   # Logging Settings
   ##

   access_log /var/log/nginx/access.log;
   error_log /var/log/nginx/error.log;

   ##
   # Gzip Settings
   ##

   gzip on;
   gzip_disable "msie6";

   include /etc/nginx/conf.d/*.conf;
   include /etc/nginx/sites-enabled/*;
}

這是我的defaultnginx 伺服器配置:

server {
   listen 80 default_server;
   listen [::]:80 default_server ipv6only=on;

   index index.php index.html index.htm;

   server_name example.com www.example.com;

   location / {
       try_files $uri $uri/ =404;
       proxy_pass http://104.46.52.226/;
   }

   error_page 404 /404.html;
   error_page 500 502 503 504 /50x.html;
   location = /50x.html {
       root /usr/share/nginx/html;
   }

   location ~ \.php$ {
       try_files $uri =404;
       fastcgi_split_path_info ^(.+\.php)(/.+)$;
       fastcgi_pass unix:/var/run/php5-fpm.sock;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
   }
}

以下是對example.comor的請求結果www.example.com(hosts 文件更改):

在此處輸入圖像描述

我認為這可能是一個問題,因為我的伺服器配置中沒有root指令 - 但後來我真的不知道從哪裡開始解決這個問題。至少我知道這不是 IIS 伺服器本身的問題,因為我可以完美地瀏覽 localhost。

有什麼想法嗎?首先十分感謝。

編輯:這也是我的錯誤日誌:

2015/10/13 09:41:10 [emerg] 3069#0: invalid number of arguments in "proxy_pass" directive in /etc/nginx/sites-enabled/armari.com:31
2015/10/13 10:12:33 [emerg] 3606#0: "listen" directive is not allowed here in /etc/nginx/sites-enabled/armari.com:2
2015/10/13 13:39:18 [error] 3911#0: *1 open() "/usr/share/nginx/html/css/new-web.css" failed (2: No such file or directory), client: 188.39.188.214, server: armari.com, request: "GET /css/new-web.css HTTP/1.1", host: "www.armari.com", referrer: "http://www.armari.com/"
2015/10/13 13:39:18 [error] 3911#0: *3 open() "/usr/share/nginx/html/css/nivo/themes/default/default.css" failed (2: No such file or directory), client: 188.39.188.214, server: armari.com, request: "GET /css/nivo/themes/default/default.css HTTP/1.1", host: "www.armari.com", referrer: "http://www.armari.com/"
2015/10/13 13:39:18 [error] 3911#0: *4 open() "/usr/share/nginx/html/css/nivo/nivo-slider.css" failed (2: No such file or directory), client: 188.39.188.214, server: armari.com, request: "GET /css/nivo/nivo-slider.css HTTP/1.1", host: "www.armari.com", referrer: "http://www.armari.com/"
2015/10/13 13:39:21 [error] 3911#0: *8 open() "/usr/share/nginx/html/css/new-web.css" failed (2: No such file or directory), client: 188.39.188.214, server: armari.com, request: "GET /css/new-web.css HTTP/1.1", host: "armari.com", referrer: "http://armari.com/"
2015/10/13 13:39:21 [error] 3909#0: *10 open() "/usr/share/nginx/html/css/nivo/themes/default/default.css" failed (2: No such file or directory), client: 188.39.188.214, server: armari.com, request: "GET /css/nivo/themes/default/default.css HTTP/1.1", host: "armari.com", referrer: "http://armari.com/"
2015/10/13 13:39:21 [error] 3909#0: *11 open() "/usr/share/nginx/html/css/nivo/nivo-slider.css" failed (2: No such file or directory), client: 188.39.188.214, server: armari.com, request: "GET /css/nivo/nivo-slider.css HTTP/1.1", host: "armari.com", referrer: "http://armari.com/"

最後找到了一個解決方案 - 我強迫 nginx 伺服器嘗試使用try_files $uri $uri/ =404;. 我所要做的就是刪除這條線,它工作得很好:)

location / {
   proxy_pass http://104.46.52.226/;
}

從最不可能的問題開始,80 是正確的埠proxy_pass http://104.46.52.226/嗎?如果不是,這個埠是在監聽哪個埠?如果是 5000,則需要 proxy_pass http://104.46.52.226:5000;這可能不是你的問題。

您需要在位置塊中設置一些標頭,以便 IIS 可以使用它們:

   proxy_set_header  x-real-IP        $remote_addr;
   proxy_set_header  x-forwarded-for  $proxy_add_x_forwarded_for;
   proxy_set_header  host             $http_host;

引用Karl Seguin 的話:

由於 Nginx 實際上是向我們的 Web 伺服器發出請求,因此我們正在向請求中添加標頭,以便 Web 伺服器擁有所有必要的資訊。

他還談到了記憶體。

文件/etc/nginx/sites-enabled/armari.com看起來有一個註釋掉的server {行。這是錯誤之一。所有需要工作的文件都/etc/nginx/sites-enabled/需要讓 Nginx 感覺良好。如果是這個文件,你可能只是沒有粘貼#(註釋開始程式碼)。或者也許你在錯誤發生後改變了它。

引用自:https://serverfault.com/questions/728593