0

this is my story.

I'm running a Meteor.js app that launches docker containers on the same host machine. Meteor.js is set to run on port 8080; where all http and https requests for "/" are forwarded to. My nginx configuration at /etc/nginx/project/sites-available/site is as follows:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    root /usr/share/nginx/html;
    index index.html index.htm;
    server_name **projectdomain.com**;

    location / {
        rewrite     ^ https://$server_name$request_uri? permanent;
    }
}

server {
    listen 443 ssl spdy; 
    server_name **projectdomain.com**;
    root html;
    index index.html;
    ssl_certificate /etc/nginx/ssl/project.crt;
    ssl_certificate_key /etc/nginx/ssl/project.key;
    ssl_stapling on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 5m;
    ssl_prefer_server_ciphers on;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-$
    add_header Strict-Transport-Security "max-age=31536000;";
    if ($http_user_agent ~ "MSIE" ) {
        return 303 https://browser-update.org/update.html;
    }
    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header X-Forwarded-For $remote_addr;
        if ($uri != '/') {
            expires 30d;
        }
    }

}

i want a certain URL, such as projectdomain.com/4200 to point to projectdomain.com:4200, where my docker container would be listening to. I want to do this because the target audience of my project are behind a corporate firewall that does not enable them to access the app running at port 4200. i mean, the docker app runs just fine and is accessible when one's not behind a firewall by heading to projectdomain.com:4200. i just want it bridged over 80 or 443 in compliance with my current nginx settings.

when i do

location /4200 {
proxy_pass http://127.0.0.1:4200;
}

although my docker container is running at 4200, heading to projectdomain.com/4200 gives an nginx 502 error. this probably has something to do with the netstat -tulpn output.

whereas my meteor project seems to run un 127.0.0.1:8080, the docker container shows to be running at :::4200. i think, the reason i get the 502 is because nginx forwards the request at /4200 to 127.0.0.1:4200 where nothing is running (as stated by netstat).

question is, what should i do to make docker run the container at 127.0.0.1:4200 instead of :::4200 , or is there any other approach i should follow?

  • You are looking for the reverse proxy of nginx. nginx does this very well. Just take a look at [nginx reverse proxy configuration example](https://duckduckgo.com/?q=nginx+reverse+proxy+configuration+example&t=ffab) ;) – Auzias Aug 26 '16 at 08:32

2 Answers2

0

First you can try nginx proxy image.

nginx-proxy sets up a container running nginx and docker-gen. docker-gen generates reverse proxy configs for nginx and reloads nginx when containers are started and stopped.

This image can't work with location. But you can use subdomains. Like 4200.projectdomain.com. It is most simple way.

Second you can configure nginx manually.
you need link containers, and configure nginx as described here

Community
  • 1
  • 1
Bukharov Sergey
  • 9,767
  • 5
  • 39
  • 54
  • Thanks Sergey, although in my case, the application I'm running (meteor) is not containerized. I've successfully configured and implemented jwilder's nginx-proxy, but examining the netstat -tulpn output i can see that the dockerized nginx is also listening at :::80 and :::443. this means that, as long as i run my application in a docker container, it can be proxied. but not if im hosting it from 127.0.0.1 (without being dockerized) – Can Mıhcı Sep 28 '16 at 11:52
0

First of all, proxy_pass should set IP address on host which container running on, not localhost, 127.0.0.1 neither. If you have many dynamic port urls, want to map them to upstream ports, use this:

location ~ ^/(\d{4,})$ {
    set $p_port $1;
    proxy_pass http://HOSTIP:$p_proxy;
}
ARKII
  • 1
  • 3