How to Configure Nginx as a Reverse Proxy in CentOS

To Configure Nginx as a Reverse Proxy in CentOS

Nginx is a webserver or reverse proxy that helps the users to deploy and deliver the sites with high performance, security, etc. It can also create a load balance between multiple back end web servers. This article explains the configuration of Nginx as a Reverse Proxy in CentOS.



Testing Environment

Reverse Proxy:

IP address –
Hostname –


IP address –
Hostname –


Before going to set reverse proxy, setup basic apache web server on the webserver node with any content. And hostname resolution between these machines is must. Inorder to do that configure DNS server or create entry in /etc/hosts file, for example shown below.

Sample Hosts file entry: node1 node2

Installation of Nginx

Inorder to install Nginx, we need to add epel repository.

[root@node1 ~]# yum install epel-release -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base:
* extras:
* updates:
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-6 will be installed
--> Finished Dependency Resolution
Running transaction
Installing : epel-release-7-6.noarch 1/1
Verifying : epel-release-7-6.noarch 1/1

epel-release.noarch 0:7-6


Now its time to install Nginx with following command.

[root@node1 ~]# yum install nginx –y
Loading mirror speeds from cached hostfile
* base:
* epel:
* extras:
* updates:
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.10.2-1.el7 will be installed
Dependency Installed:
GeoIP.x86_64 0:1.5.0-9.el7 gperftools-libs.x86_64 0:2.4-7.el7 libunwind.x86_64 2:1.1-5.el7_2.2
nginx-all-modules.noarch 1:1.10.2-1.el7 nginx-filesystem.noarch 1:1.10.2-1.el7 nginx-mod-http-geoip.x86_64 1:1.10.2-1.el7
nginx-mod-http-image-filter.x86_64 1:1.10.2-1.el7 nginx-mod-http-perl.x86_64 1:1.10.2-1.el7 nginx-mod-http-xslt-filter.x86_64 1:1.10.2-1.el7
nginx-mod-mail.x86_64 1:1.10.2-1.el7 nginx-mod-stream.x86_64 1:1.10.2-1.el7


To Configure Nginx as Reverse Proxy

Let's open the Nginx configuration file to configure the Nginx as a reverse proxy.

[root@node1 ~]# vim /etc/nginx/nginx.conf

Disable the Nginx as a webserver in configuration file by commenting the following lines.

#    server {
#        listen       80 default_server;
#        listen       [::]:80 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;

#        location / {
#        }

#        error_page 404 /404.html;
#            location = /40x.html {
#        }

#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

Then add the following configuration to enable Nginx as a reverse proxy.

server {
   listen 80;
   access_log off;
   error_log off;
   location / {
      proxy_redirect off;
      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_max_temp_file_size 0;
      client_max_body_size 10m;
      client_body_buffer_size 128k;
      proxy_connect_timeout 90;
      proxy_send_timeout 90;
      proxy_read_timeout 90;
      proxy_buffer_size 4k;
      proxy_buffers 4 32k;
      proxy_busy_buffers_size 64k;
      proxy_temp_file_write_size 64k;

Mention the IP address and Hostname for the backend webserver

Restart and enable the nginx service to take effect. Then allow HTTP port on the firewall.

[root@node1 ~]# systemctl start nginx
[root@node1 ~]# systemctl enable nginx
ln -s '/usr/lib/systemd/system/nginx.service' '/etc/systemd/system/'
[root@node1 ~]# firewall-cmd --permanent --add-service=http
[root@node1 ~]# firewall-cmd --reload

Once the above process is completed, call reverse proxy server's IP address to view the output.



Thank you! for using Linux Help.

You find this tutorial helpful? Share with your friends to keep it alive.
For more help topics browse our website
Be the first to comment, we value your suggestions. For further queries please comment below.

Tags: Nginx CentOS Proxy server Web Server Reverse-proxy
user image Author :  Jayce
user image
nambolanpperingeth 31-12-2017 at 10:47
server section must be under http {} section