Question

NGINX reverse proxy to Apache docker container

Posted September 4, 2018 15k views
Nginx Apache Docker Ubuntu 16.04

I’m trying to host several websites on my droplet. I’m to do that, I’m using NGINX (not container) as reverse proxy to Dockerized apps. One such app I’m using is the dockerized Mediawiki set to run on 0.0.0.0:8081.

Mediawiki container is based on php7.2-apache.

Nginx configuration :

server {

        listen 443 ssl;
        index index.php;

        server_name my.website.com;

        ssl_stapling on;
        ssl_stapling_verify on;

        location / {
                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_pass http://0.0.0.0:8081;
        }

    ssl_certificate /etc/letsencrypt/live/my.website.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/my.website.com/privkey.pem; # managed by Certbot
}

I run the application on port 8081, as can be seen by through docker ps -a

CONTAINER          IMAGE          PORTS
e40c9815d6cc      mediawiki    0.0.0.0:8081 -> 80/tcp

I can access my.website.com, but it shows the default Apache Ubuntu default page. Accessing other pages and resources (index.php, /folder/index.php, images/pic.jpg) returns 404.

Testing the container with similar setup on my machine works. I think there maybe something up I didn’t get with the NGINX config.

Help?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

3 answers

Hi @aerlaut!

Perhaps proxy_pass should be:

proxy_pass http://127.0.0.1:8081;

From what I understand is that, you want NGINX to listen on any IP address and network device in your server, at port 8081, (listen on 0.0.0.0:8081) and reroute that request to your local docker container on port 8081 (send to 127.0.0.1:8081).

Additionally what does nginx´s error log contains?

I think this below link would be helpful for you

https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-web-server-and-reverse-proxy-for-apache-on-one-ubuntu-18-04-server

if after that you have questions ask about, thanks.

by Jesin A
In this tutorial you'll configure Nginx as both a web server and as a reverse proxy for Apache to host four domains on a single server.

It’s simple. Just change the old configuration:

proxy_pass http://0.0.0.0:8081;

To this new one:

proxy_pass http://0.0.0.0:80;

The reason is, between container and Nginx in host server can’t communicate with published port. It just can communicate with the target port. 8081:80 (8081 is published port and 80 is target port).

Let say you have 8081:443, you must create the configuration with proxy_pass http://0.0.0.0:443; for Nginx listen to 443 port number. May this help.

edited by MattIPv4
Submit an Answer