6

Here's the problem:

The host machine has multiple docker apps running on different ports for eg. App1 @ 3001, App2 @ 3002...3100 etc

Now I would like to access the apps in this format http://hostname.com/app1, http://hostname.com/app2..

To do this i'm running nginx on the host to proxy requests to the right port based on the sub-uri

location = /app1 {
    proxy_redirect http://hostname:3001/;
    include /etc/nginx/proxy_params;
}

location ^~ /app1 {
    proxy_redirect http://hostname:3001/app1;
    include /etc/nginx/proxy_params;
}

But this does not work when the site's sub uri changes or if the site redirects. For example:

If I visit the site at hostname:3001 -> I can see the site
If I visit the site at http://hostname.com/app1 -> I can see the site
If the site page is at hostname:3001/static/index.html then when i access it as http://hostname.com/app1 the page changes to http://hostname.com/static/index.html -> I get 404.

Is there a way to do this? Or is the only way to do it is to set the dns as app1.hostname.com and do a name based routing?

2
  • Your problem is not related to Docker or Nginx. It's a more generic problem of reverse proxying with changed application path: your application expects it's serving at / while reverse proxy serves it at /app1. I am sure you can find the problem description and workarounds easily. Commented Oct 24, 2015 at 6:48
  • Yup I had asked this long time back. I fixed this by creating dns record for the host where the container is running and map the container port to host port and then add a simple config to nginx map app1.abc.com to host:port in proxy pass. It has worked well and is easy to scale. Commented Oct 26, 2015 at 15:32

2 Answers 2

2

Inside your server {} block you want:

location /app1 {
    rewrite ^/app1(.*) /$1 break;
    proxy_pass http://hostname:3001/;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location /app2 {
    rewrite ^/app2(.*) /$1 break;
    proxy_pass http://hostname:3002/;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

The rewrite rule here will pass the correct uris to the ports

Sign up to request clarification or add additional context in comments.

Comments

1

You can make every app listens on a separate port (e.g. 3000 and 3001) then configure your nginx as follows (include it inside the server {} definition block):

 location /app1 {
  proxy_pass        http://localhost:3000;
  proxy_set_header  X-Real-IP  $remote_addr;
 }

 location /app2 {
  proxy_pass        http://localhost:3001;
  proxy_set_header  X-Real-IP  $remote_addr;
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.