Robert C. Holland
Robert C. Holland

Reputation: 1813

Nginx Reverse proxy for a docker container running at port 80

My docker compose looks like this:

version: '3.2'
services:
  mediawiki:
    image: mediawiki:lts
nginx:
    image: nginx:stable-alpine
    depends_on: 
      - mediawiki
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    ports:
      - 80:80
#...

Where mediawiki is a docker container that runs on port 80 in docker and does not appear to have a way to change the port number.

I'm trying to expose mediwiki through ngninx and the nginx config looks like this:

events {
}
http {
    server {
        listen 80;

        location / {
            client_max_body_size 2M;
            real_ip_header X-Forwarded-For;
            real_ip_recursive on;
            proxy_pass http://mediawiki:80;
        }
    }
}

Since both nginx and mediawiki is running at port 80, I can't set portmap mediwiki 80:80.

I've tried mapping it to another port under mediawiki such as 7001:80 and in nginx config replace http://mediawiki:80 with http://mediawiki:7001 but this produces bad gateway error when loading the site url at port 80.

How might I fix this?

Upvotes: 0

Views: 2043

Answers (2)

Saeed
Saeed

Reputation: 4125

Let's have a look at reverse proxy in which case I use.

version: '3.2'
services:
  mediawiki:
    image: mediawiki:lts
nginx:
    build: .
    image: A_NEW_NAME:VERSION_TAG
    depends_on: 
      - mediawiki
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./wiki.conf:/etc/sites-available/wiki.conf
    ports:
      - 80:80

This should be your wiki.conf contents:

server {
        listen 80;
        server_name THE_DOMAIN_NAME_OF_YOUR_MEDIAWIKI;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://mediawiki:80;
        proxy_redirect off;

        # Socket.IO Support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

And add a Dockerfile in the directory where your docker-compose file is:

FROM nginx:stable-alpine
COPY wiki.conf /etc/sites-available/
RUN cd /etc/sites-enabled/ && ln -s /etc/sites-available/wiki.conf

And keep your nginx.conf as default values, or change some values on your own but do not add any directives to serve wiki.

You can replace THE_DOMAIN_NAME_OF_YOUR_MEDIAWIKI wit the actual domain name. Like if you have media.com and your wiki wants to be accessible at wiki.media.com.

Now you can run docker-compose up -d --build and see the result.

Upvotes: 1

Rakesh Gupta
Rakesh Gupta

Reputation: 3750

Change the service port for media wiki to 8080, like 8080:80

and

Change the nginx port to 7001 inside the local nginx.conf and proxy_pass http://mediawiki:8080;

./nginx.conf:/etc/nginx/nginx.conf

So, nginx will run on port 7001 and mediawiki on 80.

version: '3.2'
services:
  mediawiki:
    image: mediawiki:lts
nginx:
    image: nginx:stable-alpine
    depends_on: 
      - mediawiki
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    ports:
      - 80:7001
#...

Then access the app at http://mediawiki:80

Upvotes: 1

Related Questions