implement nginx reverse proxy

How To Implement NGINX Reverse Proxy

NGINX is a versatile web server that can be also used as a reverse proxy server, thereby offering load balancing and improved security. Here’s how to implement NGINX Reverse Proxy.

 

How To Implement NGINX Reverse Proxy

NGINX offers many features such as URL redirection, caching. Here are the steps to implement NGINX Reverse Proxy. After you setup NGINX as reverse proxy, you may want to use a reporting software to monitor the key metrics about your website/application such as signups, traffic, sales, revenue, etc. using dashboards & charts, to ensure everything is working well and spot issues quickly.

 

1. Install NGINX

If you have already installed NGINX, you can skip this step. Otherwise open unix terminal and run the following commands to download & install NGINX on Ubuntu.

$ sudo apt-get update
$ sudo apt-get install nginx

 

2. Disable Virtual Hosts

For our NGINX reverse proxy example, we need to disable virtual hosts with the following command

$ sudo unlink /etc/nginx/sites-enabled/default

 

3. Create Reverse Proxy Configuration File

Go to etc/nginx/sites-available directory and create a new file reverse-proxy.conf to store NGINX reverse proxy configuration details.

cd etc/nginx/sites-available/
vi reverse-proxy.conf

Add the following lines to reverse-proxy.conf file

server {
    listen 80;
    location / {
        proxy_pass http://192.x.x.2:8000;
    }
}

Let’s look at the above lines. Let’s say your application server such as Apache runs on port 8000. The above server block tells NGINX to listen to port 80, the default HTTP port for incoming requests, and redirect incoming requests to backend web server running at 192.x.x.2 listening to port 8000.

If both NGINX and Web application server are located on same system, you can replace 192.x.x.2 with 127.0.0.1

server {
    listen 80;
    location / {
        proxy_pass http://127.0.0.1:8000;
    }
}

NGINX provides proxy_pass directive to direct incoming requests to one or more backend servers. You can use it to implement NGINX reverse proxy for multiple domains or locations.

 

Typically, when NGINX passes a request to backend application servers, it removes empty headers and changes some of the other headers. If you want to pass a request header to backend value or set it, you can use the proxy_set_header directive. Here’s an example where we set the Host and Accept-Encoding headers before passing them to backend server

location / {
    proxy_set_header Host $host;
    proxy_set_header Accept-Encoding "";
    proxy_pass http://localhost:3000;
}

 

You can also implement NGINX reverse proxy with SSL my changing NGINX port to 443, and adding SSL certificate details.

 

4. Copy the Configuration File

$ sudo ln -s /etc/nginx/sites-available/reverse-proxy.conf /etc/nginx/sites-enabled/reverse-proxy.conf

This will activate the reverse-proxy.conf file.

 

Bonus Read: How to Redirect 404 to URL in NGINX

 

5. Test Config & Restart NGINX

$ service nginx configtest
$ service nginx restart

 

Open your web browser and visit your website’s homepage. It will display your application’s homepage.

That’s it! Now you know how to implement NGINX Reverse Proxy server.

By the way, if you want to create charts & dashboards to monitor your business or website, you can try Ubiq. We offer a 14-day free trial.

mm

About Ubiq

Ubiq is a powerful dashboard & reporting platform for small & medium businesses. Build dashboards, charts & reports for your business in minutes. Get insights from data quickly. Try it for free today!