反向代理子位置

反向代理子位置

我希望 nginx 重定向(重寫?)所有對/images/css、或 的/whatever請求,因為正確地映射到該站點,但當然它總是尋找而不是http://frontend/site/imageshttp://frontend/site/csshttp://frontend/sitebackend:port/whateverhttp://frontend/site/whatever

是告訴 nginx 反向代理動態調整 url 的方法嗎?無需建立數百個/location指令

謝謝

答案1

如果site是靜態的:

rewrite ^(.*)$ /site/$1 break;

如果site是主機名稱/網域:

rewrite ^(.*)$ /$host$1 break;

rewrite也可以在server區塊中使用:ngx_http_rewrite_module



編輯:
在您發表評論後,答案正好相反:

location /nasweb/ {
 rewrite ^/nasweb(/.*)$ $1 break;
 proxy_pass http://your-internal-nas;
}

這應該允許您打開https://your-external-domain.com/nasweb/將您的請求轉發到http://your-internal-nas/

編輯:
為了防止錯誤的重定向,請使用以下命令:

proxy_redirect / /nasweb/;

相關內容