Express / Nginx无法加载静态文件
我的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 www.example ;
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 www.example ;
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
但浏览器不是:
.png
路由器:
App.use('/api', require('./routes/api'))
回答如下:
试试这个Nginx配置,它会将http://example/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;
...
}