13

I use shiny server to build a web-app on port 3838, when i use nginx in my server it works well. But when I stop nginx on my server and try to use docker nginx, I find the site comes to a '502-Bad Gate Way' error and nginx log shows:

2016/04/28 18:51:15 [error] 8#8: *1 connect() failed (111: Connection refused) while connecting to upstream, ...

I install docker-nginx by this command:

sudo docker pull nginx

My docker command line is something like (for clear i add some indent):

sudo docker run --name docker-nginx -p 80:80 
    -v ~/docker-nginx/default.conf:/etc/nginx/conf.d/default.conf
    -v  /usr/share/nginx/html:/usr/share/nginx/html nginx

I create a folder name 'docker-nginx' in my home dir, move my nginx conf file in this folder, and then remove my original conf in etc/nginx dir just in case.

My nginx conf file looks like this:

server {
    listen 80 default_server;
    # listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
            proxy_pass http://127.0.0.1:3838/;
            proxy_redirect http://127.0.0.1:3838/ $scheme://$host/;
            auth_basic "Username and Password are required";
            auth_basic_user_file /etc/nginx/.htpasswd;
            # enhance the performance
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
    }
}
jjdblast
  • 525
  • 1
  • 8
  • 26

2 Answers2

9

You have to define upstream directly. Currently your nginx can not proxy to your web application.

http://nginx.org/en/docs/http/ngx_http_upstream_module.html

upstream backend {
    server backend1.example.com       weight=5;
    server backend2.example.com:8080;
    server unix:/tmp/backend3;

    server backup1.example.com:8080   backup;
    server backup2.example.com:8080   backup;
}

server {
    location / {
        proxy_pass http://backend;
    }
}
Belter
  • 3,573
  • 5
  • 42
  • 58
Meiram Chuzhenbayev
  • 898
  • 1
  • 10
  • 26
4

My situation was running 3 containers, a nginx container and two containerized services. I was using the Nginx container as a reverse proxy for my go services.

Issue is the nginx container was looking for microservice ports in its own container environment. I didn't realize that at the time and I didn't use the docker-compose.yml then. When using docker-compose.yml file you specify a network and that's that.

So when running the containers you should use --net=host. Info on that: What does --net=host option in Docker command really do?

This worked for me, I hope it saves someone the pain :):

docker run --net=host nginx:someTag

docker run --net=host service1:someTag

Osoro
  • 382
  • 3
  • 8