首頁  >  問答  >  主體

express - nginx運行node程序,路由跳轉是404,靜態檔案可以正常載入

nginx的設定碼是這樣的

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                root /home/ubuntu/card/public;
                try_files $uri $uri/ =404;
                proxy_pass http://127.0.0.1:3000;
        }
        
}

node程式本身沒有問題

PHP中文网PHP中文网2704 天前1041

全部回覆(2)我來回復

  • 伊谢尔伦

    伊谢尔伦2017-05-24 11:34:53

    我的理解是try_files 如果不成功,應該讓express伺服器嘗試回應

    location / {

                root /home/ubuntu/card/public;
                try_files $uri $uri/ @proxy;
        } 

    location @proxy {

    proxy_pass http://127.0.0.1:3000;  

    }

    回覆
    0
  • 漂亮男人

    漂亮男人2017-05-24 11:34:53

    假設你的目錄/home/ubuntu/card/public下方有個叫uploads的資料夾,nginx設定如下

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
    
        server_name _;
    
        location / {
            proxy_pass http://127.0.0.1:3000;
        }
    
        location /uploads {
            root /home/ubuntu/card/public/;
            index index.html index.htm index.nginx-debian.html;
            try_files $uri $uri/ =404;
        }
    }

    那麼訪問http://localhost/會跳到nodejs,http://localhost/uploads/會訪問/home/ubuntu/card/public/uploads下的靜態檔案。

    回覆
    0
  • 取消回覆