1

I also search other questions but they don't fit me. (Docker nginx reverse proxy gives "502 Bad Gateway" or docker nginx 502 bad gateway)

In my situation, I install docker-ce to my personal VPS server as given:

Client:
 Version:      18.03.1-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   9ee9f40
 Built:        Wed Jun 20 21:43:51 2018
 OS/Arch:      linux/amd64
 Experimental: false
 Orchestrator: swarm

Server:
 Engine:
  Version:      18.03.1-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.9.5
  Git commit:   9ee9f40
  Built:        Wed Jun 20 21:42:00 2018
  OS/Arch:      linux/amd64
  Experimental: false

then

pull standard latest version of wordpress, mysql and jwilder/nginx reverse proxy

And use standard codes as given

MYSQL:

docker run --name myweb_com_mysql -v /opt/docker-volumes/myweb_com_mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD="myweb41#D_fG" --restart unless-stopped -d mysql

Wordpress :

docker run --name myweb_com_wordpress --link myweb_com_mysql:mysql -p 127.0.0.1:48010:80 -v /opt/docker-volumes/myweb_com_wordpress:/var/www/html -e VIRTUAL_HOST="myweb.com,www.myweb.com" --restart unless-stopped -d wordpress

and NGINX REVERSE PROXY:

docker run -d -p 80:80 --name nginx-proxy  -v /var/run/docker.sock:/tmp/docker.sock jwilder/nginx-proxy

What is wrong with that?

Regards

Dr. X
  • 2,890
  • 2
  • 15
  • 37

2 Answers2

1

Jason Wilder writes it in his README.md:

Usage

To run it:

$ docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy

Then start any containers you want proxied with an env var VIRTUAL_HOST=subdomain.youdomain.com

$ docker run -e VIRTUAL_HOST=foo.bar.com ...

I see that you put the commands in reverse order, starting nginx-proxy last. Therefore it doesn't work.

A small sidenote: I deeply respect Jason for the community work he does. But at some point I myself stopped using his nginx-proxy and just started using simple nginx image adding configs to it on the fly in my DevOps process. The reason is that mapping /var/run/docker.sock to inside of a container is a huge security risk. Another reason is that nginx-proxy was not very stable at that time and it didn't forward specific stuff to my containers (like some headers for Jira). Not sure how it works now, my DevOps is stable enough with my own solution and I'd never go exposing my prod/testing/staging environment with the said security risk.

Anyways. I generate files like myweb.conf, which include:

server {
  server_name myweb.com;

  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://myweb:80;
  }
}

Notice proxy_pass http://myweb:80; -- if containers are on the same network, nginx has other containers in it's /etc/hosts. But I am usually getting an IP from the running container to generate the conf file. Better safe than sorry.

And then I start an nginx container and add the file in there. Usually I do it with docker-compose:

version: '3.4'

services:
  service-nginx:
    image: nginx:latest
    container_name: service-nginx
    restart: always
    volumes:
      - ${nginx_basepath}/nginx.conf:/etc/nginx/nginx.conf
      - ${nginx_basepath}/conf.d/:/etc/nginx/conf.d/
    ports:
      - "80:80"

I save myweb.conf to ${nginx_basepath}/conf.d/ and it automatically appears among nginx configs, so nginx is aware of it. Afterwards, nginx just needs a reload with docker exec -i service-nginx nginx -s reload.

Good luck!

Community
  • 1
  • 1
Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
-1

hello for what I understand --link is deprecated, it is recommended to create your own network

https://docs.docker.com/network/links/#connect-using-network-port-mapping

I hope this can help you

enter link description here