Tuesday 18 March 2014

How to Setup Nginx as Reverse Proxy with Apache

WHM/Cpanel comes by default with Apache web server. That doesn’t mean that we can’t integrate Nginx “EngineX”. With some minor changes, we can install Nginx to listen


on port 80 and forward any PHP request to Apache to handle on another port like 8081. Apache is not really good in handling static files, so we pass this task to


Nginx. You will notice that your memory and CPU will decrease once you have done this setup.


Warning: This setting is not suitable for shared hosting environment. I strongly recommend you to apply this if you have 1 busy website running under cPanel. Make sure


you have compiled your Apache modules and features using EasyApache.


If you want nginx for shared hosting,please refer the following post/article :-


I am using variables as below:


OS: CentOS 5.6 32bit

cPanel: cPanel 11.30.1 (build 5)

Domain IP: 10.20.30.11

Apache port: 8081

Domain: unixsurgeon.com

User: unixsurgeon

Home directory: /home/unixsurgeon


1. Since Nginx will be reverse proxy for Apache, we don’t want our log file to record the proxy IP. We want the real IP as usual. This will make sure our stats page


like Webalizer and AWstats will record the correct information. So we need to install mod_rpaf which is “Reverse Proxy Add Forward” module for Apache. You can download


that at http://stderr.net/apache/rpaf/download:


# cd /usr/local/src

# wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz

# tar -xzf mod_rpaf-0.6.tar.gz

# cd mod_rpaf-*

# apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c


2. Once installed, we need to load the module into Apache configuration. Since cPanel already has Include Editor for Apache, we will use that functions. Login to WHM > Service Configuration > Apache Configuration > Include Editor > Pre Main Include > All Versions and paste following text:


LoadModule rpaf_module modules/mod_rpaf-2.0.so

RPAFenable On

RPAFproxy_ips 127.0.0.1  10.20.30.11 # replace the value with your server IP

RPAFsethostname On

RPAFheader X-Real-IP


3. Click Update > Restart Apache. The module should be loaded after restart.


4. Before we install Nginx, we need to change Apache port to 8081. Login to WHM > Server Configuration > Tweak Settings > Apache non-SSL IP/port:


0.0.0.0:8081


5. We need to run following command so cPanel will remember that Apache configuration template has changed:


#  /usr/local/cpanel/bin/apache_conf_distiller –update –main

#  /scripts/rebuildhttpdconf


6. Lets install Nginx and all requirements using yum. You can download Nginx source at http://nginx.org/en/download.html:


#  yum install pcre* -y

#  cd /usr/local/src

#  wget http://nginx.org/download/nginx-1.0.5.tar.gz

#  cd nginx-*

#  ./configure

#  make

#  make install


7. Once installed, we need to do some changes to Nginx configuration file. Using text editor, copy and paste following line and change the required value to fit your environment:


—————————————


user  nobody;


worker_processes  1;


error_log  logs/error.log  info;


events

worker_connections  1024;


http

server_names_hash_max_size 2048;

include       mime.types;

default_type  application/octet-stream;


log_format   main ‘$remote_addr – $remote_user [$time_local]  $status ‘

‘”$request” $body_bytes_sent “$http_referer” ‘

‘”$http_user_agent” “$http_x_forwarded_for”‘;


sendfile        on;

tcp_nopush     on;


keepalive_timeout  10;


gzip  on;

gzip_min_length  1100;

gzip_buffers  4 32k;

gzip_types    text/plain application/x-javascript text/xml text/css;

ignore_invalid_headers on;


client_header_timeout  3m;

client_body_timeout 3m;

send_timeout     3m;

connection_pool_size  256;

client_header_buffer_size 4k;

large_client_header_buffers 4 32k;

request_pool_size  4k;

output_buffers   4 32k;

postpone_output  1460;


server css)$

# this is your public_html directory

root   /home/unixsurgeon/public_html;


location /

client_max_body_size    10m;

client_body_buffer_size 128k;


proxy_send_timeout   90;

proxy_read_timeout   90;


proxy_buffer_size    4k;

proxy_buffers     16 32k;

proxy_busy_buffers_size 64k;

proxy_temp_file_write_size 64k;


proxy_connect_timeout 30s;


# change to your domain name

proxy_redirect  http://www.unixsurgeon.com:8081   http://www.unixsurgeon.com;

proxy_redirect  http://unixsurgeon.com:8081   http://unixsurgeon.com;


proxy_pass   http://127.0.0.1:8081/;

proxy_set_header   Host   $host;

proxy_set_header   X-Real-IP  $remote_addr;

proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;



———————————


8. We need to check the Nginx configuration file before start the Nginx. Use following command to check the configuration file:


root@cpanel [~]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful


9. If everything is OK, we can start the Nginx as below:


# /usr/local/nginx/sbin/nginx start


10. Lets check whether Nginx and Apache are listening to the correct port:


root@cpanel [~]# netstat -tulpn | grep -e nginx -e httpd

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      16728/nginx

tcp        0      0 0.0.0.0:8081                  0.0.0.0:*                   LISTEN      19655/httpd

tcp        0      0 0.0.0.0:443                 0.0.0.0:*                   LISTEN      19655/httpd


11. If everything is run as expected, edit /etc/rc.local using text editor and add following line so Nginx will start automatically after reboot :-


# /usr/local/nginx/sbin/nginx restart


Nginx no need to be restarted to load the latest configuration file. You can run following command and it will reload the configuration on-the-fly without downtime:


kill -HUP `ps -ef | grep nginx | grep master | awk ‘print $2?`


No comments:

Post a Comment