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

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

我有一個在 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.com這是對or的請求的結果www.example.com(hosts 文件已更改):

在此輸入影像描述

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

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

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

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/"

答案1

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

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

答案2

從最不可能出現的問題開始,80 是正確的連接埠嗎proxy_pass http://104.46.52.226/?如果沒有,它正在偵聽哪個連接埠?如果是 5000,則需要 proxy_passhttp://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;

去引用卡爾·塞甘

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

他還談到了緩存。

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

相關內容