How To Install Nginx as a Reverse Proxy for Apache Web Server
Nginx(fontend) as a reverse Proxy for Apache(Backend)
Before Apply Solution
client --http port tcp 80 --> Apache Web Server
After Apply Solution
client --http port tcp 80 --> Nginx - http port tcp 8080 -> Apache Web Server
Server OS :Centos
– Install Apache
#yum install httpd httpd-devel -y
Configure the Reverse Proxy settings on Apache
#vim /etc/httpd/conf/httpd.conf
Listen 80
to
Listen 8080
NameVirtualHost *:80
to
NameVirtualHost *:8080
<VirtualHost *:8080>
ServerName www.yourdomain.com
ServerAlias yourdomain.com
DocumentRoot /var/www/yourdomain.com
<Directory "/var/www/yourdomain.com">
Options FollowSymLinks -Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
RewriteEngine on
</VirtualHost>
#/etc/init.d/httpd restart
Install Nginx with yum
#yum install nginx -y
#vim /etc/nginx/nginx.conf
user nginx;
worker_processes 4;
error_log logs/error.log crit;
#error_log logs/error.log info;
# set open fd limit to 30000
worker_rlimit_nofile 30000;
events {
worker_connections 8192; # you might need to increase this setting for busy servers
multi_accept on;
use epoll; # Linux kernels 2.6.x change to epoll
}
http {
server_names_hash_max_size 2048;
server_names_hash_bucket_size 512;
server_tokens off;
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 10;
# Gzip on
gzip on;
gzip_min_length 1100;
gzip_buffers 4 32k;
gzip_types text/plain application/x-javascript text/xml text/css;
# Other configurations
ignore_invalid_headers on;
client_max_body_size 8m;
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;
# Cache most accessed static files
open_file_cache max=10000 inactive=10m;
open_file_cache_valid 2m;
open_file_cache_min_uses 1;
open_file_cache_errors on;
# virtual hosts includes
include "/etc/nginx/conf.d/*.conf";
}
and create virtual host on nginx
/etc/nginx/conf.d/yourdomainname.com.conf
server {
listen 80;
server_name www.yourdomainname.com yourdomainname.com;
access_log off;
error_log logs/www.yourdomainname.com-error_log crit;
location ~* .(gif|jpg|jpeg|png|ico|wmv|3gp|avi|mpg|mpeg|mp4|flv|mp3|mid|js|css|html|htm|wml)$ {
root /var/www/yourdomainname.com;
expires 365d;
}
location / {
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_connect_timeout 30s;
proxy_redirect http://www.yourdomainname.com:8080 http://www.yourdomainname.com;
proxy_redirect http://yourdomainname.com:8080 http://yourdomainname.com;
proxy_pass http://127.0.0.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;
}
}
Install mod_rpaf
mod_rpaf will help Apache to know who connects to it (otherwise the only IP address you will see in your logs is 127.0.0.1):
mkdir /root/installed/
cd /root/installed/
wget https://github.com/y-ken/mod_rpaf/archive/master.zip
unzip master.zip
cd mod_rpaf-master/
apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c
#vim /etc/httpd/conf.d/rpaf.conf
LoadModule rpaf_module /usr/lib64/httpd/modules/mod_rpaf-2.0.so
#Reverse proxy
RPAFenable On
RPAFsethostname On
RPAFproxy_ips 127.0.0.1 Your_Real_Server_IP
#/etc/init.d/httpd restart
curl -I http://www.yourdomainname.com/
output :
HTTP/1.1 200 OK
Server: nginx
Complete!!!!!