繁体   English   中英

Express / Nginx无法加载静态文件

[英]Express/Nginx can't load static files

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

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'))

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

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

  ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM