Nginx Proxy Configuration Docker

Here's the problem:

The host machine has several docker applications running on different ports, for example. App1 @ 3001, App2 @ 3002 ... 3100, etc.

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

To do this, I run nginx on the host for proxy requests to the right port based on 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 will not work if the ur uri site changes or the site is redirected. For instance:

 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 any way to do this? Or is the only way to do this is to install dns as app1.hostname.com and perform name-based routing?

+6
source share
2 answers

Inside the 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

+2
source

You can force each application to listen on a separate port (for example, 3000 and 3001), and then configure nginx as follows (include it in 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; } 
+1
source

Source: https://habr.com/ru/post/976581/


All Articles