Setup | Configure Reverse Proxies using Nginx HTTP Server on Ubuntu 18.04

This brief tutorial shows students and new users how to install and configure reverse proxies with Nginx HTTP Server..

Reverse proxy is when a proxy server (in this case, Nginx HTTP) accepts all traffic and forwards it to a specific resource, like a backend server or container..

Nginx HTTP server is one of the most popular open source web servers that is also a reverse proxy…

There are many reasons to install and use a proxy server… Example, reverse proxy can be used to added security, or for load balancing, restrict access to certain locations in order to prevent attacks and many more…

When you’re ready to configure Nginx as reverse proxy, follow the steps below:

Step 1: Install Nginx

Installing Nginx is easy.. to do that, simply run the commands below

sudo apt-get update
sudo apt-get install nginx

After installing Nginx, the commands below can be used to stop, start and enable Nginx to always restart when the server boots up.

sudo systemctl stop nginx.service
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

To test Nginx setup, open your browser and browse to the server hostname or IP address and you should see Nginx default test page as shown below.. When you see that, then Nginx is working as expected..

nginx default home page test

Step 2: Configure Nginx as Reverse Proxy

Assuming that Nginx is installed and ready, you can now configure Nginx to act as a reverse proxy..

Nginx proxy module‚Äôs proxy_pass function provide a reverse proxy… To use proxy_pass, you must first know where you want to direct traffic..

In a typical setup, the reverse proxy server will listen for all traffic on the default HTTP port, which is port 80..

The backend server which host the content will listen on a custom port… Most likely port 8080.

In this post, we’re going set up Nginx to listen on port 80, then direct traffic to the backend server which listens on port 8080

Below, run the command to create a proxy VirtualHost file called NginxProxy.

sudo nano /etc/nginx/sites-available/NginxProxy

Then add the block of codes in the file then save.

server {
    listen 80;
    listen [::]:80;

    proxy_redirect           off;
    proxy_set_header         X-Real-IP $remote_addr;
    proxy_set_header         X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header         Host $http_host;

    location / {
            proxy_pass http://localhost:8080;

Save the file when done.

The file contains your server name as well as the proxy_pass where traffic are directed when received by the proxy server….

To text Nginx configurations, run the commands below:

sudo nginx -t

You should then see an output similar to the lines below

nginx: the configuration file /etc/nginx/sites-available/NginxProxy syntax is ok
nginx: configuration file /etc/nginx/sites-available/NginxProxy test is successful

If everything is checked and successful, run the commands below to enable the proxy site and restart Nginx HTTP server…

sudo ln -s /etc/nginx/sites-available/NginxProxy /etc/nginx/sites-enabled/
sudo systemctl restart nginx.service

Launch a web browser and navigate to your server hostname (… You will now be proxied to your Apache2 server on port 8888.

Congratulations! You have successfully configure Nginx reverse proxy on Ubuntu 18.04

You may also like the post below:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.