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

nginx proxy_pass with flexable subpath possible?

· 52CE 我爱测

currently I have this as my proxy_pass which works fine location = /sms/resetpass { proxy_pass http://xxx.xxx.xxx.xxx/api/resetpass; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upg…

问题原文

原帖链接 · Dora · CC BY-SA 4.0

currently I have this as my proxy_pass which works fine

    location = /sms/resetpass {
             proxy_pass http://xxx.xxx.xxx.xxx/api/resetpass;
             proxy_http_version 1.1;
             proxy_set_header Upgrade $http_upgrade;
             proxy_set_header Connection 'upgrade';
             proxy_set_header Host $host;
             proxy_cache_bypass $http_upgrade;
    }

the above works fine but I figured if I want another proxy such as

    location = /sms/sample1 {
             proxy_pass http://xxx.xxx.xxx.xxx/api/sample1;
             proxy_http_version 1.1;
             proxy_set_header Upgrade $http_upgrade;
             proxy_set_header Connection 'upgrade';
             proxy_set_header Host $host;
             proxy_cache_bypass $http_upgrade;
    }

then I have to make another location block

is it possible to do something like this?

    location = /sms/something_here {
             proxy_pass http://xxx.xxx.xxx.xxx/api/something_here;
             proxy_http_version 1.1;
             proxy_set_header Upgrade $http_upgrade;
             proxy_set_header Connection 'upgrade';
             proxy_set_header Host $host;
             proxy_cache_bypass $http_upgrade;
    }

where something_here means if I pass in any url into sms/blah then the proxy_pass would be api/blah

I tried to google something like proxy_pass with subpath or something like that but doesn't seem to be what I need. So I am wondering if this is possible or I just didn't know the right word to find it?

Thanks in advance for any advices.

已采纳解答

原帖链接 · mforsetti · CC BY-SA 4.0

Use nginx location and rewrite directives, something like this should be fine:

location ~ ^/sms/ {
    rewrite ^/sms/(.*) /api/$1 break;
    proxy_pass http://1.2.3.4;
    ...
}

don't forget to test it first.

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

资料来源与适用范围

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

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

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