How To Create Reverse Proxy with Nginx?

Nginx is very popular web server software. It has some advantages over Apache. Some developers and system administrators use Nginx with Apache to get more from advantages. Nginx Reverse proxy is very popular because of the speed provided by Nginx with files and directories by using threads. In this tutorial we will look how to configure a reverse proxy with Nginx.

Install Nginx For Ubuntu, Debian, Kali, Mint

We will install nginx package for Ubuntu ,Debian, Kali and Mint with the following command.

$ sudo apt install nginx -y
Install Nginx For Ubuntu, Debian, Kali, Mint
Install Nginx For Ubuntu, Debian, Kali, Mint

Install Nginx For Fedora, CentOS, RHEL

For Fedora, CentOS and RHEL installation please execute following yum command.

$ sudo yum install nginx

Create Site Configuration

As we know every site will have a configuration in nginx configuration directory. In most cases each site configuration will hold in a separated file with named with related site name. We will create a site configuration file named poftut.com with the following lines. This configuration file will be located at

server {
 listen 80 poftut.com;

 root /var/www/html;

 server_name _;

}

Create Location

Now we will create a Location directive which will be use to add reverse proxy configuration. The location will set the path where requests will be forwarded to the web server. We will create a location which will cover all site.  We will also add some trivial directive to the location

server {
 listen 80 poftut.com;

 root /var/www/html;

 server_name _;

 location / {
  client_max_body_size 10m;
  clien_body_buffer_szie 128k;
 }

}

Add Proxy Configuration To The Location

Now we will add configuration options. We will also provide some proxy related information.

server {
 listen 80 poftut.com;

 root /var/www/html;

 server_name _; location / { 
  client_max_body_size 10m; 
  clien_body_buffer_szie 128k; 
  
  proxy_pass http://192.168.1.1:8080;
  proxy_set_header Host $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;
 }

}

We will use proxy_set_header to set header to the HTTP request forwarded to the web server.

  • `proxy_pass` is the main configuration where we set the web server protocol, address and port.
  • `Host` will provide the host information provided by web client
  • `X-Forwarded-For` will provided the web server we want to forward.
  • `X-Forwarded-Proto` will provide protocol like http or https used by web client.

Test Configuration

We have changed a lot of configuration in the nginx. So we are human an made mistakes. We should check the new configuration syntax with the configtest command provided by nginx.

$ sudo service nginx configtest

Enable Site Configuration

Now we will enable our site by adding a soft link to the /etc/nginx/sites-enabled with the following command.

$ sudo ln -s /etc/nginx/sites-available/poftut.com /etc/nginx/sites-enabled/poftut.com

Restart Nginx

In order to make reverse proxy configuration enabled we should restart the nginx web server to reread the new configuration and make it effective.

$ sudo systemctl restart nginx

Leave a Comment