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?

  • Check with curl -v – Alexey Ten Aug 4 '16 at 8:11
  • BTW, what are core, page and auth servers? May be you've mixed up them – Alexey Ten Aug 4 '16 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. – user2372674 Aug 4 '16 at 8:33
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"
}
  • Actually author should use slashes and don't need rewrite. serverfault.com/a/586614/211028 – Alexey Ten Aug 4 '16 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. – user2372674 Aug 4 '16 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"
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.