搜索

首页  >  问答  >  正文

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中文网2744 天前1085

全部回复(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
  • 取消回复