簡體   English   中英

在使用NGINX提供靜態文件時如何用Node.js響應404?

[英]How to respond with 404 from Node.js when serving static files with NGINX?

我有一個在端口3000上運行的Node.js應用程序,並使用NGINX作為代理。 NGINX也提供靜態文件。 啟用網站的配置:

server {
    listen 80;
    server_name myapp.dev;

    location ~ ^/(img/|js/|css/|robots.txt|favicon.ico) {
        root /srv/nodejs/myapp/public;
        access_log off;
        expires max;
    }

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

目前一切正常。 但是當請求不存在的靜態文件時(/css/doesntexist.css),我得到一個默認的NGINX 404頁面。

我知道可以將用戶重定向到自定義404頁面(例如/404.html),但我希望在從我的節點應用程序顯示自定義404時保持URL指向不存在的路徑。

有任何想法嗎?

得到它與以下工作。

location ~ ^/(img/|js/|css/|robots.txt|favicon.ico) {
    access_log off;
    expires max;
    error_page 404 = @not_found;
}

location @not_found {
    proxy_pass http://127.0.0.1:3000;
}

這是基於error_page選項的NGINX文檔中的示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM