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?

user2372674
  • 203
  • 2
  • 5
  • Check with `curl -v` – Alexey Ten Aug 04 '16 at 08:11
  • BTW, what are `core`, `page` and `auth` servers? May be you've mixed up them – Alexey Ten Aug 04 '16 at 08: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 04 '16 at 08:33

2 Answers2

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"
}
Simon MC. Cheng
  • 396
  • 1
  • 6
  • Actually author should use slashes and don't need rewrite. http://serverfault.com/a/586614/211028 – Alexey Ten Aug 04 '16 at 08: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 04 '16 at 08: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"
    }
}
Alexey Ten
  • 7,922
  • 31
  • 35