![](/img/trans.png)
[英]Serving multiple static files in nestjs for different locations and prefixes?
[英]Nginx load static files for multiple locations
我在nginx .conf文件中有多个位置,就像这样。
server {
...
location /api/ {
client_max_body_size 0;
proxy_pass http://127.0.0.1:8000/api/;
proxy_redirect off;
proxy_read_timeout 5m;
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header XForwardedFor $proxy_add_x_forwarded_for;
}
...
location /cliente/ {
proxy_pass http://127.0.0.1:4000/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
}
location / {
proxy_pass http://127.0.0.1:8000/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
}
在大多数情况下,这些位置是提供其自己的静态文件和api rest的节点服务器。
每个节点应用程序中的静态文件正在使用html文件加载并按以下方式调用
/my-static-path-1/
/my-static-path-2/
问题:
当服务器尝试加载路径时,这是直接从/
加载,而不是使用自己的子路径加载,从而导致404错误。
例:
1)url http:/myip_sample.com/cliente
调用一个nodejs应用并提供一个html文件,该文件需要加载一个CSS脚本,如/my-static-path/js/sample.js
。
2)此文件调用http:/myip_sample.com/my-static-path/js/sample.js
代替http:/myip_sample.com/ cliente /my-static-path/js/sample.js
我想不做任何更改,仅使用nginx conf文件。
提前致谢!
将它用于您拥有的每个资产文件夹位置
location /my-static-path-1 {
alias /location/to/my-static-path-1;
}
location /my-static-path-2 {
alias /location/to/my-static-path-2;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.