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

reverse proxy two locations, one nested inside the other

· 52CE 我爱测

I have two server programs on a machine. One's listening at localhost:3000, and the other at localhost:3001. The second one is an API server, and the first delivers web pages. I want to use nginx as a reverse proxy in such a way …

问题原文

原帖链接 · O. Jones · CC BY-SA 4.0

I have two server programs on a machine. One's listening at localhost:3000, and the other at localhost:3001. The second one is an API server, and the first delivers web pages.

I want to use nginx as a reverse proxy in such a way that incoming requests to URIs like this

https://example.com/api/what/ever 

are redirected to the second server as http://localhost:3001/what/ever, and URIs like this

https://example.com/ and https://example.com/some/route/what/ever

are redirected to the first server as

http://localhost:3000/ and http://localhost:3000/some/route/what/ever

In other words, I want to pick off the example.com/api/* requests to send to the second server, and send the rest to the first server.

But, it works as if my /api/ location directive is not present: everything's getting passed to the first server, which of course responds 404 to the api requests, and normally to other requests.

Here's my nginx.conf attempt.

          location ^~ /hub/  {
            rewrite ^/hub(.*)$ $1 last;
            proxy_pass http://localhost:3001;
            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;
            proxy_set_header        X-Real-IP       $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
          }

          location / {
            proxy_pass http://localhost:3000;
            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;
            proxy_set_header        X-Real-IP       $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
          }

I also tried location ~ ^(?!/api).*$ { for the second location directive, using a regex in an attempt to make it not match my /api/ URIs, with the same result.

What am I doing wrong? Is this even possible?

已采纳解答

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

The last keyword on a rewrite statement causes Nginx to restart the search for a location to process the rewritten URI. Your rewrite...last statement sends the request to the other location block.

To process the rewritten URI within the same location block, use break instead. See this document for details.

For example:

location ^~ /hub/ {
    rewrite ^/hub(.*)$ $1 break;
    proxy_pass http://localhost:3001;
    ...
}

Alternatively, the same translation can be achieved by appending an optional URI to the value in the proxy_pass statement. See this document for details.

For example:

location ^~ /hub/ {
    proxy_pass http://localhost:3001/;
    ...
}

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

资料来源与适用范围

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

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

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