Express / Nginx无法加载静态文件

时间:2017-12-28 13:01:37

标签: node.js express nginx

My Express应用无法在浏览器上加载静态文件,但它可以在curl中运行。我使用nginx作为webserver。应用程序运行良好但没有任何静态文件。我在这里做错了什么?

App.js

...

App.use(cors())
App.use("/data", express.static(__dirname + '/data'));

App.use('/api', require('./routes/api'))

App.listen(1337)

nginx

server
{
        listen x.x.x.x:80;
        server_name example.com www.example.com ;

        location /api {

                proxy_pass http://x.x.x.x:1337;

                ...
        }

    location / {
                return 301 https://$server_name$request_uri;
        }

}

server
{
        listen x.x.x.x:443 ssl;
        server_name example.com www.example.com ;
    root /path/to/public_html;
        index index.php index.html index.htm;
        ssl on;
        location /api {
                add_header X-Cache "HIT from Backend";

                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;

                proxy_pass http://x.x.x.x:1337;

                ...
        }
}

卷曲没问题:

http://127.0.0.1:1337/data/pic.png

但浏览器不是:

http://example.com/api/data/pic.png

路由器:

App.use('/api', require('./routes/api'))

1 个答案:

答案 0 :(得分:1)

尝试使用此Nginx配置,它会将http://example.com/api/data/pic.png重定向到http://x.x.x.x:1337/data/pic.png

location /api {
  rewrite /api(.*) /$1 break;
  proxy_pass http://x.x.x.x:1337;

  ...
}
相关问题