Reverse Proxy multiple sites using NGINX

According to NGINX wiki, NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server.

We decided to use NGINX as a reverse proxy to multiple LXD containers. Please follow this guide to install and configure NGINX on your system. This article assumes that you are using “Ubuntu 16.04” as the host of LXD Containers.

  • Install OS Updates and then install nginx on your server running LXD Containers.
sudo apt update
sudo apt upgrade
sudo apt install nginx-full

Forwarding Incoming connection for various Ports to Containers

  • Create a directory to store configuration files for port forwarding for application such as Databases. In this case we have a MariaDB database server inside a LXD Container. The following configuration will pass all connections for port 3306 to route to MariaDB container.
sudo mkdir /etc/nginx/tcpconf.d
cd /etc/nginx/tcpconf.d
nano containers.conf

stream {
 upstream mariadb01 {
 server 192.168.10.20:3306;
 }
server {
 listen 3306;
 proxy_pass mariadb01;
 }
}
  • Add this Configuration Directory to nginx config file.
nano /etc/nginx/nginx.conf
### Add following line to the end of the file.
include /etc/nginx/tcpconf.d/*.conf;

 

Forwarding Web traffic to Containers

  • For passing a Web Application such as DokuWiki, please create configuration under nginx configuration direcoty. This will pass all traffic
IP Address of DokuWiki Container = 192.168.10.25

nano /etc/nginx/conf.d/dokuwiki.conf
upstream dokuwiki {
 server 192.168.10.25:80;
}

server {
 listen 80;
 server_name dokuwiki.domainname.com;
 location / {
 proxy_pass http://dokuwiki;
 proxy_redirect off;
 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }
}
  • Create a DNS Entry of LXD host on your DNS Server. In this case we will be using Windows Server 2012 R2 as DNS Server.
IP Address of LXD Server = 192.168.1.120 
IP Address of DokuWiki Container = 192.168.10.25
IP Address of MariaDB Container = 192.168.10.20

Open DNS Management on Windows Server 
Go to Forward Lookup Zone and Create an "A Record" 
Enter desired hostname under "Name" on the windows for "A Record" 
Enter IP address of the LXD Host inside the box for "IP Address"
  • Restart NGINX Service to load new configuration
sudo systemctl restart nginx.service

Now you are ready to connect to your container from outside the LXD Container Host.

For MariaDB:  

Use your favorite database client and enter following details.

Hostname: mariadb01.domainname.com
Port: 3306
And Username and Password you created when you created Database.

For DokuWiki: 

Open your favorite browser and enter following URL
URL: http://dokuwiki.domainname.com

 

I hope you found this article useful and I would really appreciate if you could provide your thoughts on this post.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s