express - nginx運行node程序,路由跳轉是404,靜態檔案可以正常載入
PHP中文网
PHP中文网 2017-05-24 11:32:53
0
2
1036

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中文网

认证高级PHP讲师

全部回覆(2)
伊谢尔伦

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

location / {

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

location @proxy {

proxy_pass http://127.0.0.1:3000;  

}

漂亮男人

假設你的目錄/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下的靜態檔案。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!