大网数据广告活动
服务器运维问答

nginx config reverse proxy lose basepath on redirect without slash

· 52CE 我爱测

i have the following nginx configs to redirect the url path to it's perspective services server { listen 80; server_name abc.com; location = favicon.ico { access_log off; log_not_found off } proxy_http_version 1.1; proxy_set_head…

问题原文

原帖链接 · Linh Nguyen · CC BY-SA 4.0

i have the following nginx configs to redirect the url path to it's perspective services

server {
    listen 80;
    server_name abc.com;
    location = favicon.ico { access_log off; log_not_found off }
    
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_cache_bypass $http_upgrade;
    

    location /a-ms/ {
       rewrite /a-ms/(.*) /$1 break;
       proxy_pass http:host.docker.internal:3000/;
    }
    
    location /b-ms/ {
       rewrite /b-ms/(.*) /$1 break;
       proxy_pass http:host.docker.internal:4000/;
    }
    
}

the backend microservices using nodejs to host the api and swagger doc

When i got to a url from a browser like abc.com/a-ms/doc/ it return the swagger normal, but when i go to url without slash for example abc.com/a-ms/doc it redirected me to abc.com/doc which is not what i wanted(it missing the location path)(. How do i fix this with nginx config settings?

已采纳解答

原帖链接 · Linh Nguyen · CC BY-SA 4.0

following RichardSmith suggestion of using proxy_redirect and now my location redirect is correct and included the location path

location /a-ms/ {
   rewrite /a-ms/(.*) /$1 break;
   proxy_pass http:host.docker.internal:3000/;
   proxy_redirect /doc /a-ms/doc; #add this
}
    
location /b-ms/ {
   rewrite /b-ms/(.*) /$1 break;
   proxy_pass http:host.docker.internal:4000/;
   proxy_redirect /doc /b-ms/doc; #add this
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-04-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。