How to Configure NGINX Reverse Proxy


configure nginx reverse proxy

NGINX is an excellent web server that is easy to customize and configure. It can even act as a reverse proxy across multiple back end servers. This will protect your website from malicious attacks. Here’s how to configure NGINX reverse proxy with Apache back end servers.

 

How to Configure NGINX Reverse Proxy

Here are the steps to configure NGINX reverse proxy for Apache web server on Ubuntu server. You can easily customize them for your Linux distribution

 

1. Install Apache Web Server

Please skip this step if you have already installed Apache


$ sudo apt-get update
$ sudo apt-get install apache2

2. Install NGINX

Install NGINX server.


$ sudo apt-get -y install nginx

 

3. Configure Apache

By default, Apache will be listening to port 80. We need to change it to another port so that NGINX can listen to port 80.

Open Apache port config file


$ sudo nano /etc/apache2/ports.conf

Look for the following line


Listen 80

and change it to


Listen 8000

 

We also need to update the same in Apache’s virtual host file. Open it in a text editor


$ sudo nano /etc/apache2/sites-available/000-default.conf

 

Look for the following line


<VirtualHost 127.0.0.1:80>

and change it to


<VirtualHost 127.0.0.1:8000>

 

Now apache will listen to port 8000. You can change it to any other port as per your requirement.

 

4. Restart Apache web server

Restart Apache web server to apply changes


$ sudo service apache2 restart

 

5. Configure NGINX

Enable NGINX service


$ sudo systemctl enable nginx.service

 

Restart NGINX to apply changes


$ sudo service nginx restart

 

Next, we will add some proxy parameters to NGINX proxy_params file. Open it in a text editor


$ sudo nano /etc/nginx/proxy_params

 

You will find the following 4 lines in it, by default


proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

 

Add the following lines to it.


client_max_body_size 100M;
client_body_buffer_size 1m;
proxy_intercept_errors on;
proxy_buffering on;
proxy_buffer_size 128k;
proxy_buffers 256 16k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_max_temp_file_size 0;
proxy_read_timeout 300;

 

Save and close the file.

 

Just as we updated Apache’s virtual host file, we will do the same for NGINX.

Create a back up of your NGINX config


$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default-dist

 

Open it in a text editor


$ sudo nano /etc/nginx/sites-available/default

 

Delete the contents of your NGINX virtual host file and paste the following lines


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

 root /var/www/html;

 # Add index.php to the list if you are using PHP
 index index.html index.htm index.nginx-debian.html;

 server_name _;

 location / {
  proxy_pass http://localhost:8000;
  include /etc/nginx/proxy_params;
 }
}

 

Reload NGINX server


$ sudo service nginx reload

 

6. Test the Configuration

If you re-open your website in web browser, you won’t notice any difference. So use the curl command to request the same page. Substitute example.com with your server IP or domain name.


curl -s -D - "http://example.com" | grep "^Server:"

 

In the output, you will see “Server: nginx/1.10.3 (Ubuntu)” which means the request is being served by NGINX, which sends it to Apache and returns the response back to your browser.

 

 

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!