![](/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.