Nginx Reverse Proxy Setup for Linux Server

Share this Article :

Nginx Reverse ProxyNginx also pronounced “Engine-X” is a free, open-source HTTP Web server and one of the best alternative to Apache http server. It is a high-performance edge web server with the lowest memory footprint and the key features to build modern and efficient web infrastructure. Nginx also provides a combination of Nginx web servers, Nginx reverse proxy and Nginx load balancing solution to the websites that running on high traffic and just wants to be consistently efficient. Nginx has the lowest memory footprint possible and optimizes CPU usage while delivering maximum performance even on a very cheap server hardware. More importantly, Nginx is able to continuously take more connections while maintaining low memory¬†usage.

What is Nginx Reverse Proxy ?

When Nginx reverse proxy received request, it sends a request to the specified proxied server. In this case the specified proxied server is Apache web server. When Nginx reverse proxy fetches the response from Apache web server, It will sends it back to the client. In other words, Nginx reverse proxy serve as front-end server for Apache web service.

How to Setup Nginx Reverse Proxy for Linux

This article will show you how to install and configure Nginx reverse proxy for Apache web server. It was assumed that Apache web server has been running at 192.168.1.55 and nginx will be install at another server with Ip address, 192.168.1.54. This has been tested and working fine at CentOS 6 / CentOS 7 / RHEL 7 / Oracle Linux 7.

Nginx Reverse Proxy Server : 192.168.1.54
Apache Web server : 192.168.1.55

1. Prepared yum repository for nginx :

# vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

2. Perform yum install for nginx :

# yum install nginx -y

3. Backup original nginx config file :

# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

4. Modify default nginx config file :

# vi /etc/nginx/nginx.conf
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {

    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    charset   utf-8;
    keepalive_timeout  65;
    server_tokens       off;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         off;

    server {
          listen 80;
          server_name  _;
          root   /usr/share/nginx/html;
          index  index.html index.htm;     }

    include  conf.d/*.conf;
}

5. Add the host configuration file under /etc/nginx/conf.d/. Nginx config file will load all *.conf files under conf.d folder :

As example, the website domain is ehowstuff.local. So virtual server ehowstuff.local will be created and named as ehowstuff.local.conf :

# vi /etc/nginx/conf.d/ehowstuff.local.conf
server {
      listen 80;
      server_name  ehowstuff.local www.ehowstuff.local;

 access_log  off;
 error_log off;

location / {
  proxy_pass              http://192.168.1.55:80/;
  proxy_set_header        X-Real-IP       $remote_addr;
  proxy_set_header        Host  $host;
  proxy_redirect          off;
  proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_connect_timeout   90;
  proxy_send_timeout      90;
  proxy_read_timeout      90;
  client_max_body_size    10m;
  client_body_buffer_size 128k;
  proxy_buffer_size       4k;
  proxy_buffers           4 32k;
  proxy_busy_buffers_size 64k;
}
}

On above configuration file, all the traffic to ehowstuff.local port 80 will be redirected to the Apache web server that hosted at 192.168.1.55. Nginx reverse proxy serve as front-end server for Apache web service.

6. Verify the configuration file :

# /usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

7. Start nginx server :

# service nginx start

8. Configure nginx start at boot :

# chkconfig nginx on

 

Leave a Reply