简体   繁体   中英

Using Nginx to redirect to dynamic host

I've used Nginx as a load balancer and reverse proxy with success, but this issue which seems really simple has tripped me up. I've spent a few hours on trying to configure Nginx but have failed.

I have a dynamic set of servers/hostnames (because of using docker), say service2, service3, service5. I simply want to configure nginx to proxy to these based on the URL. The purpose of this is so i can access the services from a single endpoint rather than expose them all.

This is the type of proxy I'm trying to get working

If i specify all endpoints it works, although still only for the initial page, so http://localhost:8009/service2 do proxy to http://service2/ however http://localhost:8009/service2/home just fails but this would be because it's failing to match a location. If if i just do location / it works, then i can only reverse proxy all requests to a single server.

server {
listen       80;
server_name  localhost;

    location /service2 {
        proxy_pass http://service2/;
    }

    location /service5 {
        proxy_pass http://service5/;
    }
}

Here is probably my best example of regexing the url to be dynamic, but this just errors so assume it's not valid for nginx.

server {
listen       80;
server_name  localhost;

    location ~ (?<myhost>.*)/(?<myuri>.*)$ {
        proxy_pass http://$myhost/$myuri;
    }
}

The regex that you are using is not the correct one. you can achieve what you are trying to do in the following way

 location ~ /(?<myhost>[^/]+)(/(?<myuri>.*))? {
      return 301 http://$myhost:80/$myuri?$query_string;
    }
➜  ~   curl --head http://127.0.0.1:32769/service1
HTTP/1.1 301 Moved Permanently
Server: nginx/1.17.8
Date: Sun, 23 Feb 2020 20:06:29 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: http://service1:80/?

➜  ~   curl --head http://127.0.0.1:32769/service1/test
HTTP/1.1 301 Moved Permanently
Server: nginx/1.17.8
Date: Sun, 23 Feb 2020 20:08:43 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: http://service1:80/test?

➜  ~   curl --head http://127.0.0.1:32769/service1/test?x=100
HTTP/1.1 301 Moved Permanently
Server: nginx/1.17.8
Date: Sun, 23 Feb 2020 20:08:53 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: http://service1:80/test?x=100

for proxy_pass you don't need to include the URI or the query strings, you can do it like this

server {
    listen       80  default_server;
    server_name  _;

    location ~ /(?<myhost>[^/]+) {
        resolver 127.0.0.11 ipv6=off;

        set $target http://$myhost:80;

        proxy_set_header Host $myhost;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass $target;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM