Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx proxy pass configuration docker

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?

like image 850
nmd Avatar asked Oct 10 '14 17:10

nmd


People also ask

Where is NGINX config in Docker container?

Maintaining Content and Configuration Files on the Docker Host. Any change made to the files in the local directories /var/www and /var/nginx/conf on the Docker host are reflected in the directories /usr/share/nginx/html and /etc/nginx in the container.

How do I proxy a docker container?

You can do this from Docker's settings Docker > Preferences > Resources > Proxies . All you need to do is provide values for the following variables: HTTP_PROXY : the proxy server endpoint to handle HTTP calls.

What is a reverse proxy container?

“a reverse proxy is a type of proxy server that retrieves resources on behalf of a client from one or more servers.” A reverse proxy is like a middleman (proxy) between a user (client) making a request to that proxy and that proxy making requests and retrieving its results from other servers.

How do you expose the NGINX container on the Internet?

First, you need to expose the Nginx container to the internet. Kubernetes will create a service with an external load balancer with a public IP address. You can view your service by executing the following command. Now, you will get the external IP address of the Nginx cluster.


2 Answers

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

like image 90
tommyyards Avatar answered Oct 20 '22 01:10

tommyyards


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;
 }
like image 34
iTech Avatar answered Oct 19 '22 23:10

iTech