Tangela
Tangela

Reputation: 161

Issue with setting up Nginx with multiple Docker containers

Currently I want to setup one server that has a Docker WordPress and Nginx that serves as a proxy in front. I would like in future to be able have multiple WordPress, NodeJS, ROR, etc, sitting behind this Nginx proxy.

When ever I try to connect to my server on port 80 I get a 403 forbidden.

I am able to build a Docker WordPress image and can connect to it on port 8080 on a remote PC.

Here is the compose.yml for my Docker WordPress:

version: "3.1"
services:

  my_wordpress:
    image: wordpress
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_PASSWORD: password
      WORDPRESS_DB_HOST: my_mysql_wordpress

  my_mysql_wordpress:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: password

This is the part when I try to build a Nginx container I am getting a 403 forbidden.

Nginx DockerFile:

FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf

Nginx compose.yml:

version: "2"
services:

  web:
    restart: always
    image: nginx
    ports:
      - "80:80"
    volumes:
      - /path/in/vm/www:/usr/share/nginx/html
    external_links:
      - mywordpress_wordpress_1:mywordpress

networks:
  default:
    external:
      name: mywordpress_default

Nginx nginx.conf:

http {

#...

upstream wordpress {
  server mywordpress:8080;
}

#...

server {
    listen 80;

    server_name 192.168.1.124 test.me;

    location / {
        proxy_pass http://wordpress/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_cache_bypass $http_upgrade;
    }
}
}

Now for me it would seem that this most likely has to do with my nginx.conf as I am still able to connect to my WordPress site on port 8080. As well as I stated I am also able to connect to my Nginx proxy and I don't see any errors when it launches.

Is what I'm trying to doing even possible or do I need to have the Nginx application sitting on the OS and not inside a docker container?

Upvotes: 1

Views: 885

Answers (2)

Tarun Lalwani
Tarun Lalwani

Reputation: 146630

You are putting Nginx and Wordpress in 2 different compose files. If you are running then on same machines then

external_links:
  - mywordpress_wordpress_1:mywordpress

Above would not work if you are on different machines. Also make sure the external link you are using the correct name by checking docker ps.

Also check the logs of your nginx container to see if it is showing in any error. Because the error log will give a pointer as to why a 403 is being thrown, and it could be that the proxy_pass is not able to connect to your wordpress server because of the way you have configured it.

If you are running these compose files on different machines then instead of external_links use extra_hosts

extra_hosts:
  - "mywordpress:<IP of the wordpress machine>"

Upvotes: 2

Bala Abhinav
Bala Abhinav

Reputation: 1348

If I am right, You want to run multiple wordpress docker images and use nginx to reverse proxy to the wordpress instances. In that Use-case, The nginx should sit on your OS and not inside a docker image. That way, the nginx will have the ability to proxy to ports on your OS which are tied to the wordpress containers.

Upvotes: 1

Related Questions