Need help? Chat now!

Hostwinds Tutorials

Search results for:


Table of Contents


Installing Docker
How To Install Docker
Installing Nginx
Setting up a simple Apache Container
Configuring Nginx as a reverse proxy

Use Ngnix to Proxy Docker Containers on Ubuntu

Tags Ubuntu,  Docker 

Installing Docker
How To Install Docker
Installing Nginx
Setting up a simple Apache Container
Configuring Nginx as a reverse proxy

Installing Docker

If you've heard about Docker, you may know about its ability to help you build applications in a sealed environment. This is helpful, as it eliminates any changes that may occur between your developer machine and your production machine, such as a VPS server from Hostwinds.

How To Install Docker

Install docker using this command:

apt-get install docker

Container technology is beneficial in sealing an environment, but when you need to configure things like SSL or other applications, this can introduce additional complexity.

Installing Nginx

Nginx (Pronounced "Engine-ex") is a highly configurable web server. This makes it great for things setting up what's called a "reverse proxy," which simply means that this webserver will take in requests and forward them to our Docker containers. Add this into your Hostwinds VPS with:

apt-get install nginx

Setting up a simple Apache Container

For this, We're going to use the official Apache image on the Docker hub. It's documentation can be found here.

As the documentation mentions, we will need to create a DockerFile.

Using a text editor, such as nano (nano apache. docker), write a file with these contents:

FROM httpd:2.4
COPY ./index.html /usr/local/apache2/htdocs/

Now create a file called index.html, and insert this into its contents:

<h1> Hello World! </h1>

It is possible to use any static page or even a directory in place of the index file. For simplicity, a single HTML file is fine. Once the file is written, run these commands to build the docker file.

docker build -t apache-demo
docker run -dit --name demo-web -p 8080:80 apache-demo

Visiting your server at [yourdomain:8080] or [your IP]:8080 Should result in seeing this page in your web browser

Configuring Nginx as a reverse proxy

With the docker container set up to serve your files, we can set up Nginx. This way, we can mostly leave the container's configuration alone and silo it off behind Nginx. Edit /etc/nginx/nginx.conf and add in this code:

server {
                server_name [YOUR.DOMAIN.NAME];
                listen 80;
                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://127.0.0.1:8080;
                }
        }

From here, NGINX takes in all the public requests, and the Apache container serves your files.

Written by Hostwinds Team  /  September 24, 2019