Using nginx as a reverse proxy
I will be using nginx to serve as a reverse proxy for my network. With Nginx, I can manage my proxy configurations in a single server. Nginx is one of the most widely used web servers and can also function as a reverse proxy, load balancer, mail proxy, and HTTP cache. Nginx is free and open-source software.
Linux example
On Linux distributions, nginx should be available in the software repositories. Once installed and enabled, you will need to open port 80
and/or 443
to access from other systems.
Red Hat systems
On Red Hat distributions such as Fedora and RHEL, you can install with the dnf
package manager.
|
|
Debian based systems
On Debian based distributions, you can install with the apt
package manager.
|
|
Enabling nginx server on linux with systemD
When you install nginx on your linux distribution, you can start the web server and have it run on boot by starting and enabling the nginx system daemon.
|
|
You can check it worked by entering the system’s hostname or IP address into a browser or by accessing the web server port with curl
or wget
from a terminal.
Test from CLI
From the terminal you can test the new web server by using the curl
or wget
commands.
|
|
OR
|
|
You should see the HTML output from the default page:
|
|
HTTPS reverse proxy example
Here is an example of how to have nginx listen on port 80 to redirect traffic to the secure port 443
which will use a certificate for TLS encryption to support https
connections.
By default, the configuration file is named nginx.conf
and placed in the directory /usr/local/nginx/conf
, /etc/nginx
, or /usr/local/etc/nginx
.
Disable default configuration
We do not want the default settings interferring with the proxy server.
|
|
Each proxy server should have a unique server_name
and proper ssl_certificate
.
|
|
This example configuration should be saved in /etc/nginx/conf.d/
and will redirect incoming requests to foo.com
to the proxy_pass
which in this example is traffic on the system’s port 3100.
You can configure the firewall to only allow connections to port 443 and incoming requests to the web server will be encrypted.
Docker example
You can also run nginx in a lightweight image with docker
. If you are not familiar with docker, check out a previous post to get started.
You can take the configuration file and copy it to a container image to create a container that will act as a reverse proxy.
To build a new container, you need to create a Dockerfile
and copy the config file into the container image.
Dockerfile
|
|
You can build the image on the local machine:
|
|
Run the new image:
|
|
Docker compose
Or create a docker-compose
template in yaml
format to combine with other containers:
|
|
Take the Dockerfile
from the example and then create a new nginx proxy config file:
|
|
Now start the container and connect to localhost
port 80
to test the proxy.
|
|
Test by going to http://localhost in the browser or CLI
|
|
You can proxy multiple containers they just need to be in different server {}
blocks in the nginx config file. You will want to use different subdomains for the server_name
option or different location /
paths on the nginx host.
You will want to use DNS to help direct traffic to the proper proxied subdomain. You will want to create DNS records for each subdomain that point to the IP address of the system where the nginx proxy is running, whether that is running on docker or on the system. On Linux systems, you can modify /etc/hosts/
file to test.
|
|
Next steps
If you created docker containers, you can clean those up with the command docker compose down
and clean everything with the command docker system prune -a
Nginx can also be used to serve static content. This blog is an example of static content copied to an nginx container. The entire site and nginx software is only a ~12 MB container image file.
In the homelab, you can set up nginx along with any other web application to either direct traffic to a different server or use it to bootstrap HTTPS encryption onto a service that otherwise does not use SSL/TLS certificates. One use case in my homelab is to install nginx and grafana loki to collect logs from other systems. Check out a previous post to see more about grafana loki. By default the loki service does not use HTTPS so by adding an nginx reverse proxy, you can ensure that all logs are encrypted in transit over the network and the only plain text communication is on the host where nginx and loki run.