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

Proxy pass on subdirectory not working

· 52CE 我爱测

I have an api running at http://127.0.0.1:9650 . For example the following is giving a json object as response: curl http://127.0.0.1:9650/ext/health I would like to make the following work at https://example.com/my-node/ext/heal…

问题原文

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

I have an api running at http://127.0.0.1:9650. For example the following is giving a json object as response:

curl http://127.0.0.1:9650/ext/health

I would like to make the following work at https://example.com/my-node/ext/health.

I have the following nginx configuration:

 location /my-node {
    proxy_pass http://127.0.0.1:9650;
    proxy_http_version 1.1;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }

but this does not seem to work (I am getting a 404)

I tried adding:

proxy_redirect http://127.0.0.1:9650 /my-node;

but no luck there either. Does anyone have a suggestion how I can solve this?

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 4.0

Using proxy_pass without an optional URI (e.g. proxy_pass http://127.0.0.1:9650;), the requested URL is passed upstream unmodified.

So https://example.com/my-node/ext/health is passed upstream to http://127.0.0.1:9650/my-node/ext/health - notice that the "/my-node" part remains intact.


Using proxy_pass with an optional URI within a prefix location will translate the requested URL by substituting the location prefix with the optional URI.

For example:

location /my-node/ {
    proxy_pass http://127.0.0.1:9650/;
    ...
}

In this case, the optional URI is /, so /my-node/ in the requested URL is replaced by / before passing upstream.

So https://example.com/my-node/ext/health is passed upstream to http://127.0.0.1:9650/ext/health - notice that the "/my-node" part has been removed.

See the proxy_pass directive for details.

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

资料来源与适用范围

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

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

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