0

I would like to redirect requests to different servers depending on path, therefore I am using the following http block in the Nginx configuration:

http { include /etc/nginx/mime.types; default_type application/octet-stream; index index.html index.htm; server { access_log /var/log/nginx/staging.access.log main buffer=32k; error_log /var/log/nginx/staging.error.log error; listen 80; root /dev/null; location / { proxy_pass http://core:80; # returns "Core Service" } location /page/ { rewrite ^/page(/.*)$ $1 break; proxy_pass http://page:80; # returns "Page Service" } location /auth/ { rewrite ^/auth(/.*)$ $1 break; proxy_pass http://auth:80; # returns "Auth Service" } } } 

As far as I understand the Nginx documentation, Nginx should use the best matching location block, therefore I would expect that curl http://hostname/ should return "Core Service", curl http://hostname/auth "Auth Service" and curl http://hostname/ "Page Service". Nginx however uses a random location block:

$ curl http://hostname/ Core Service $ curl http://hostname/ Auth Service $ curl http://hostname/ Page Service $ curl -L http://hostname/page Auth Service $ curl -L http://hostname/page Auth Service $ curl -L http://hostname/page Core Service 

What is wrong with my configuration?

3
  • Check with curl -v Commented Aug 4, 2016 at 8:11
  • BTW, what are core, page and auth servers? May be you've mixed up them Commented Aug 4, 2016 at 8:13
  • @Alexey Thank you, with your hint I have found the problem: The core, page and auth server are virtual Docker servers. I thought this would not been relevant, but it was. The integrated Docker DNS server had resolved the host names somehow randomly, now with fixed IP addresses it works. Commented Aug 4, 2016 at 8:33

2 Answers 2

1

You have added trailing / character at the end of your each match, try to edit as follow:

location /page { rewrite ^/page(/.*)$ $1 break; proxy_pass http://page:80; # returns "Page Service" } location /auth { rewrite ^/auth(/.*)$ $1 break; proxy_pass http://auth:80; # returns "Auth Service" } 
2
  • Actually author should use slashes and don't need rewrite. serverfault.com/a/586614/211028 Commented Aug 4, 2016 at 8:10
  • With your configuration it works, the real problem was a bug with the integrated DNS server from Docker and the usage of host names instead of IP adresses in the Nginx configuration. Commented Aug 4, 2016 at 8:26
0

BTW, you should simplify your config:

server { access_log /var/log/nginx/staging.access.log main buffer=32k; error_log /var/log/nginx/staging.error.log error; listen 80; location / { proxy_pass http://core:80/; # returns "Core Service" } location /page/ { proxy_pass http://page:80/; # returns "Page Service" } location /auth/ { proxy_pass http://auth:80/; # returns "Auth Service" } } 
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.