Answers: <blockquote>
    <p>I though about putting the container on a not used port, like 8999 for example, and proxying the request to this port, and blocking these ports on the firewall, so no one outside can access them. If I go with this option, does the nginx configuration inside of the containers will need the server_name still?</p>
  </blockquote>
  
  <p>You're thinking along the right lines. You do need the <code>server_name</code> directives in the container, since it can be accessed both by <code>app2.com/example</code> and <code>app3.com</code>.</p>
  
  <p>Here's an example setup. I'll emulate an nginx on the host machine with a docker container running on the host network.</p>
  
  <p>Scroll down past the code and console log for some explanations.</p>
  
  <p>Here is the listing of the files I used:</p>
  
  <pre><code>lllamnyp@lllamnyp:~/Cloud/dev/stackexchange/devops/q11420$ tail -n +1 *
  </code></pre>
  
  <p><strong>==> app1.html &lt;==</strong></p>
  
  <pre><code>I am app1!
  </code></pre>
  
  <p><strong>==> app2_example.html &lt;==</strong></p>
  
  <pre><code>I am app2 in example subpath!
  </code></pre>
  
  <p><strong>==> app2.html &lt;==</strong></p>
  
  <pre><code>I am app2!
  </code></pre>
  
  <p><strong>==> app3.html &lt;==</strong></p>
  
  <pre><code>I am app3!
  </code></pre>
  
  <p><strong>==> container_nginx.conf &lt;==</strong></p>
  
  <pre><code>server {
    server_name app2.com;
    location /example {
      root /var/www/app2/;
    }
  }
  
  server {
    server_name app3.com;
    location / {
      root /var/www/app3/;
    }
  }
  </code></pre>
  
  <p><strong>==> host_nginx.conf &lt;==</strong></p>
  
  <pre><code>server {
    server_name app1.com;
    location / { 
      root /var/www/app1/;
    }
  }
  
  server {
    server_name app2.com;
    location / { 
      root /var/www/app2/;
    }
  
    location /example {
      proxy_pass http://localhost:8999;
      proxy_set_header Host $http_host;
    }
  }
  
  server {
    server_name app3.com;
    location / {
      proxy_pass http://localhost:8999;
      proxy_set_header Host $http_host;
    }
  }
  </code></pre>
  
  <p><strong>==> nginx.conf &lt;==</strong></p>
  
  <pre><code>user  nginx;
  worker_processes  1;
  
  error_log  /var/log/nginx/error.log warn;
  pid        /var/run/nginx.pid;
  
  
  events {
      worker_connections  1024;
  }
  
  
  http {
      include       /etc/nginx/mime.types;
      default_type  application/octet-stream;
  
      log_format  main  '$remote_addr - $remote_user [$time_local] "$request" "$http_host" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';
  
      access_log  /var/log/nginx/access.log  main;
  
      sendfile        on;
      #tcp_nopush     on;
  
      keepalive_timeout  65;
  
      #gzip  on;
  
      include /etc/nginx/conf.d/*.conf;
  }
  </code></pre>
  
  <p><strong>==> run.sh &lt;==</strong></p>
  
  <pre><code>docker run --name host_nginx --network=host --rm -d \
    -v `pwd`/nginx.conf:/etc/nginx/nginx.conf \
    -v `pwd`/host_nginx.conf:/etc/nginx/conf.d/default.conf \
    -v `pwd`/app1.html:/var/www/app1/index.html \
    -v `pwd`/app2.html:/var/www/app2/index.html \
    nginx
  
  docker run --name container_nginx -p 8999:80 --rm -d \
    -v `pwd`/nginx.conf:/etc/nginx/nginx.conf \
    -v `pwd`/container_nginx.conf:/etc/nginx/conf.d/default.conf \
    -v `pwd`/app2_example.html:/var/www/app2/example/index.html \
    -v `pwd`/app3.html:/var/www/app3/index.html \
    nginx
  </code></pre>
  
  <p>Here is the log from my console, where I am testing things out.</p>
  
  <pre><code>lllamnyp@lllamnyp:~/Cloud/dev/stackexchange/devops/q11420$ sudo ./run.sh 
  73ebc99b8fae1d18bcbc571adbab6b65eaa49f861d66f11a7490d61b4e1beb0f
  9f3761ab0619255d9bde08eee70e1fb9b686b645f38d976de7c39d4c8f0ad70d
  lllamnyp@lllamnyp:~/Cloud/dev/stackexchange/devops/q11420$ curl app1.com
  I am app1!
  lllamnyp@lllamnyp:~/Cloud/dev/stackexchange/devops/q11420$ curl app2.com
  I am app2!
  lllamnyp@lllamnyp:~/Cloud/dev/stackexchange/devops/q11420$ curl app3.com
  I am app3!
  lllamnyp@lllamnyp:~/Cloud/dev/stackexchange/devops/q11420$ curl app2.com/example
  &lt;html&gt;
  &lt;head&gt;&lt;title&gt;301 Moved Permanently&lt;/title&gt;&lt;/head&gt;
  &lt;body&gt;
  &lt;center&gt;&lt;h1&gt;301 Moved Permanently&lt;/h1&gt;&lt;/center&gt;
  &lt;hr&gt;&lt;center&gt;nginx/1.17.10&lt;/center&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  lllamnyp@lllamnyp:~/Cloud/dev/stackexchange/devops/q11420$ curl app2.com/example/
  I am app2 in example subpath!
  lllamnyp@lllamnyp:~/Cloud/dev/stackexchange/devops/q11420$ sudo docker logs host_nginx
  127.0.0.1 - - [25/Apr/2020:15:11:15 +0000] "GET / HTTP/1.1" "app1.com" 200 11 "-" "curl/7.68.0" "-"
  127.0.0.1 - - [25/Apr/2020:15:11:17 +0000] "GET / HTTP/1.1" "app2.com" 200 11 "-" "curl/7.68.0" "-"
  127.0.0.1 - - [25/Apr/2020:15:11:20 +0000] "GET / HTTP/1.1" "app3.com" 200 11 "-" "curl/7.68.0" "-"
  127.0.0.1 - - [25/Apr/2020:15:11:25 +0000] "GET /example HTTP/1.1" "app2.com" 301 170 "-" "curl/7.68.0" "-"
  127.0.0.1 - - [25/Apr/2020:15:11:29 +0000] "GET /example/ HTTP/1.1" "app2.com" 200 30 "-" "curl/7.68.0" "-"
  lllamnyp@lllamnyp:~/Cloud/dev/stackexchange/devops/q11420$ sudo docker logs container_nginx
  172.17.0.1 - - [25/Apr/2020:15:11:20 +0000] "GET / HTTP/1.0" "app3.com" 200 11 "-" "curl/7.68.0" "-"
  172.17.0.1 - - [25/Apr/2020:15:11:25 +0000] "GET /example HTTP/1.0" "app2.com" 301 170 "-" "curl/7.68.0" "-"
  172.17.0.1 - - [25/Apr/2020:15:11:29 +0000] "GET /example/ HTTP/1.0" "app2.com" 200 30 "-" "curl/7.68.0" "-"
  </code></pre>
  
  <p>I corrected some errors in your config (missing semicolon here and there), changed the server roots to directories, rather than <code>index.html</code> and also the request for <code>app2.com/example</code> would request <code>/var/www/app2/example/index.html</code> so pay close attention to the way in which I mounted the files when starting up the containers in <code>./run.sh</code>. Finally, it was necessary to do <code>proxy_set_header Host $http_host;</code> in the host nginx config, so that the requested <code>Host</code> header gets correctly relayed to the containerized nginx and both app2 and app3 work correctly.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-04-25T15:20:49.103Z
Answers: 2020-04-25T15:20:49.103Z
Answers: LLlAMnYP
Body: <p>I have the following architecture, which consists
  of an nginx server on the host machine, and 3 urls, app1.com, app2.com, app3.com:
  <img src="https://i.sstatic.net/Ilgm2.png" alt="Architecture"></p>
  
  <p>app1 and app2 are both in the host machine, lets say they are an HTML file on the host,
  on the container, there is app3 and app2.com/example url.</p>
  
  <p>My question is, how can I reference the container in the nginx of the host?
  Since I cannot expose port 80 and 443 of the container, because the host
  is already using these ports, I am not sure what is the best approach to this.</p>
  
  <p>I though about putting the container on a not used port, like 8999 for example,
  and proxying the request to this port, and blocking these ports on the firewall,
  so no one outside can access them. If I go with this option, does the nginx
  configuration inside of the containers will need the server_name still? In the
  case of the app3.com/example</p>
  
  <p>The other option I thougth about is using the docker socket, or even the docker0
  network, but I don't know if this is viable.</p>
  
  <p>An example of the nginx configuration file of the host:</p>
  
  <pre><code>server {
    server_name app1.com;
    location / { 
      root /var/www/app1/index.html;
    }
  }
  
  server {
    server_name app2.com;
    location / { 
      root /var/www/app2/index.html
    }
  
    location /example {
      proxy_pass http://localhost:8999;
    }
  }
  
  server {
    server_name app3.com;
    location / {
      proxy_pass http://localhost:8999;
    }
  }
  </code></pre>
  
  <p>Example of the nginx configuration file of the container:</p>
  
  <pre><code>server {
    server_name app2.com
    location /example {
      root /var/www/app2/index.html;
    }
  }
  
  server {
    server_name app3.com;
    location / {
      root /var/www/app3/index.html;
    }
  }
  </code></pre>
  
Tags: <docker><nginx>
Title: Question on NGINX reverse proxy when there are services on the host and on containers
UserDisplayName: Lucas Terças
ThreadText: # Question on NGINX reverse proxy when there are services on the host and on containers
  
  (Asked by: username_0 on 2020-04-23)
  
  I have the following architecture, which consists
  of an nginx server on the host machine, and 3 urls, app1.com, app2.com, app3.com:
  ![Architecture](https://i.sstatic.net/Ilgm2.png)
  
  app1 and app2 are both in the host machine, lets say they are an HTML file on the host,
  on the container, there is app3 and app2.com/example url.
  
  My question is, how can I reference the container in the nginx of the host?
  Since I cannot expose port 80 and 443 of the container, because the host
  is already using these ports, I am not sure what is the best approach to this.
  
  I though about putting the container on a not used port, like 8999 for example,
  and proxying the request to this port, and blocking these ports on the firewall,
  so no one outside can access them. If I go with this option, does the nginx
  configuration inside of the containers will need the server\_name still? In the
  case of the app3.com/example
  
  The other option I thougth about is using the docker socket, or even the docker0
  network, but I don't know if this is viable.
  
  An example of the nginx configuration file of the host:
  
  ```
  server {
    server_name app1.com;
    location / { 
      root /var/www/app1/index.html;
    }
  }
  
  server {
    server_name app2.com;
    location / { 
      root /var/www/app2/index.html
    }
  
    location /example {
      proxy_pass http://localhost:8999;
    }
  }
  
  server {
    server_name app3.com;
    location / {
      proxy_pass http://localhost:8999;
    }
  }
  
  ```
  
  Example of the nginx configuration file of the container:
  
  ```
  server {
    server_name app2.com
    location /example {
      root /var/www/app2/index.html;
    }
  }
  
  server {
    server_name app3.com;
    location / {
      root /var/www/app3/index.html;
    }
  }
  
  ```
  
  **Tags:** docker, nginx
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-04-25. Score: 1 | Accepted answer
  
  > I though about putting the container on a not used port, like 8999 for example, and proxying the request to this port, and blocking these ports on the firewall, so no one outside can access them. If I go with this option, does the nginx configuration inside of the containers will need the server\_name still?
  
  You're thinking along the right lines. You do need the `server_name` directives in the container, since it can be accessed both by `app2.com/example` and `app3.com`.
  
  Here's an example setup. I'll emulate an nginx on the host machine with a docker container running on the host network.
  
  Scroll down past the code and console log for some explanations.
  
  Here is the listing of the files I used:
  
  ```
  username_1@username_1:~/Cloud/dev/stackexchange/devops/q11420$ tail -n +1 *
  
  ```
  
  **==> app1.html <==**
  
  ```
  I am app1!
  
  ```
  
  **==> app2\_example.html <==**
  
  ```
  I am app2 in example subpath!
  
  ```
  
  **==> app2.html <==**
  
  ```
  I am app2!
  
  ```
  
  **==> app3.html <==**
  
  ```
  I am app3!
  
  ```
  
  **==> container\_nginx.conf <==**
  
  ```
  server {
    server_name app2.com;
    location /example {
      root /var/www/app2/;
    }
  }
  
  server {
    server_name app3.com;
    location / {
      root /var/www/app3/;
    }
  }
  
  ```
  
  **==> host\_nginx.conf <==**
  
  ```
  server {
    server_name app1.com;
    location / { 
      root /var/www/app1/;
    }
  }
  
  server {
    server_name app2.com;
    location / { 
      root /var/www/app2/;
    }
  
    location /example {
      proxy_pass http://localhost:8999;
      proxy_set_header Host $http_host;
    }
  }
  
  server {
    server_name app3.com;
    location / {
      proxy_pass http://localhost:8999;
      proxy_set_header Host $http_host;
    }
  }
  
  ```
  
  **==> nginx.conf <==**
  
  ```
  user  nginx;
  worker_processes  1;
  
  error_log  /var/log/nginx/error.log warn;
  pid        /var/run/nginx.pid;
  
  events {
      worker_connections  1024;
  }
  
  http {
      include       /etc/nginx/mime.types;
      default_type  application/octet-stream;
  
      log_format  main  '$remote_addr - $remote_user [$time_local] "$request" "$http_host" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';
  
      access_log  /var/log/nginx/access.log  main;
  
      sendfile        on;
      #tcp_nopush     on;
  
      keepalive_timeout  65;
  
      #gzip  on;
  
      include /etc/nginx/conf.d/*.conf;
  }
  
  ```
  
  **==> run.sh <==**
  
  ```
  docker run --name host_nginx --network=host --rm -d \
    -v `pwd`/nginx.conf:/etc/nginx/nginx.conf \
    -v `pwd`/host_nginx.conf:/etc/nginx/conf.d/default.conf \
    -v `pwd`/app1.html:/var/www/app1/index.html \
    -v `pwd`/app2.html:/var/www/app2/index.html \
    nginx
  
  docker run --name container_nginx -p 8999:80 --rm -d \
    -v `pwd`/nginx.conf:/etc/nginx/nginx.conf \
    -v `pwd`/container_nginx.conf:/etc/nginx/conf.d/default.conf \
    -v `pwd`/app2_example.html:/var/www/app2/example/index.html \
    -v `pwd`/app3.html:/var/www/app3/index.html \
    nginx
  
  ```
  
  Here is the log from my console, where I am testing things out.
  
  ```
  username_1@username_1:~/Cloud/dev/stackexchange/devops/q11420$ sudo ./run.sh 
  73ebc99b8fae1d18bcbc571adbab6b65eaa49f861d66f11a7490d61b4e1beb0f
  9f3761ab0619255d9bde08eee70e1fb9b686b645f38d976de7c39d4c8f0ad70d
  username_1@username_1:~/Cloud/dev/stackexchange/devops/q11420$ curl app1.com
  I am app1!
  username_1@username_1:~/Cloud/dev/stackexchange/devops/q11420$ curl app2.com
  I am app2!
  username_1@username_1:~/Cloud/dev/stackexchange/devops/q11420$ curl app3.com
  I am app3!
  username_1@username_1:~/Cloud/dev/stackexchange/devops/q11420$ curl app2.com/example
  <html>
  <head><title>301 Moved Permanently</title></head>
  <body>
  <center><h1>301 Moved Permanently</h1></center>
  <hr><center>nginx/1.17.10</center>
  </body>
  </html>
  username_1@username_1:~/Cloud/dev/stackexchange/devops/q11420$ curl app2.com/example/
  I am app2 in example subpath!
  username_1@username_1:~/Cloud/dev/stackexchange/devops/q11420$ sudo docker logs host_nginx
  127.0.0.1 - - [25/Apr/2020:15:11:15 +0000] "GET / HTTP/1.1" "app1.com" 200 11 "-" "curl/7.68.0" "-"
  127.0.0.1 - - [25/Apr/2020:15:11:17 +0000] "GET / HTTP/1.1" "app2.com" 200 11 "-" "curl/7.68.0" "-"
  127.0.0.1 - - [25/Apr/2020:15:11:20 +0000] "GET / HTTP/1.1" "app3.com" 200 11 "-" "curl/7.68.0" "-"
  127.0.0.1 - - [25/Apr/2020:15:11:25 +0000] "GET /example HTTP/1.1" "app2.com" 301 170 "-" "curl/7.68.0" "-"
  127.0.0.1 - - [25/Apr/2020:15:11:29 +0000] "GET /example/ HTTP/1.1" "app2.com" 200 30 "-" "curl/7.68.0" "-"
  username_1@username_1:~/Cloud/dev/stackexchange/devops/q11420$ sudo docker logs container_nginx
  172.17.0.1 - - [25/Apr/2020:15:11:20 +0000] "GET / HTTP/1.0" "app3.com" 200 11 "-" "curl/7.68.0" "-"
  172.17.0.1 - - [25/Apr/2020:15:11:25 +0000] "GET /example HTTP/1.0" "app2.com" 301 170 "-" "curl/7.68.0" "-"
  172.17.0.1 - - [25/Apr/2020:15:11:29 +0000] "GET /example/ HTTP/1.0" "app2.com" 200 30 "-" "curl/7.68.0" "-"
  
  ```
  
  I corrected some errors in your config (missing semicolon here and there), changed the server roots to directories, rather than `index.html` and also the request for `app2.com/example` would request `/var/www/app2/example/index.html` so pay close attention to the way in which I mounted the files when starting up the containers in `./run.sh`. Finally, it was necessary to do `proxy_set_header Host $http_host;` in the host nginx config, so that the requested `Host` header gets correctly relayed to the containerized nginx and both app2 and app3 work correctly.
  
  
  ---
  
  