Nginx as Reverse proxy for APACHE

On 6 Jun, 2012 in Linux with Comments

APACHE is great choice for most of the webmasters who are using linux servers, to host and serve their web applications due to its flexibility Some time back our server management team had lot of issues with DDOS attacks and huge traffic, while using only APACHE web server to serve dynamic and static content. It became huge problem for us and we have to restart APACHE 10 to 15 times every day when web server stops responding due to tones of server requests.

To overcome this problem our plan was to setup CDN’s for static content where budget play’s main role so we started searching for alternative where we come through lot of web servers like litespeed, lighttpd, Nginx and some more.

Nginx as Reverse proxy for APACHE

We found Nginx is the best solution for us because it’s open source and powerful web server.Learn how to install Nginx server.

Setup Nginx as reverse proxy to APACHE

We assume that Nginx and apache is already installed in server. Make APACHE list to different port other then 80 as APACHE’s default port is 80. Change APACHE port number

Edit httpd.conf file to change the port it will be located at /etc/httpd/conf if not please search and find httpd.conf file and edit it with your favorite editor like VI.

Go to directory where httpd.conf is located using the command below

cd /etc/httpd/conf

Change 80 to something else let’s say it’s 7475. We have to change at the line where it says “Listen 80” or Listen 12.34.56.78:80 here 80 is the port number.

Next change port at NameVirtualHost line find for the line NameVirtualHost UR-IP:80 replace 80 with 7475

Change 80 to 7475 at VirtualHost line it will look like change it to

VirtualHost UR-IP:80
to
VirtualHost UR-IP:7475

I suggest find for 80 and change it 7475. We have changed the APACHE port from 80 to 7475, Restart APACHE server using the below command.

service httpd restart

Now APACHE httpd server listen’s to port 7475

Configure Nginx to act as proxy to APACHE

To achieve this we have to configure the Nginx server to listen to port 80 and whenever PHP request comes to server Nginx have to forward PHP request to APACHE server.

So every static content will be served by Nginx and dynamic (PHP) content will be served by APACHE.

Edit nginx.conf file to forward PHP request to APACHE. Go to /etc/nginx we can find nginx.conf file in that directory use the below code.

cd /etc/nginx

Edit nginx.conf file with your favorite editor like vi.

Below given the code in nignx.conf file we have edit the APACHE details in this file at the line # proxy the PHP scripts to Apache listening on 127.0.0.1:80

So follow the code below and configure your server

user username nginx;
worker_processes 2; error_log /var/log/nginx/error.log; #error_log /var/log/nginx/error.log notice; #error_log /var/log/nginx/error.log info; 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; sendfile on; tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 75; tcp_nodelay on; gzip off; gzip_http_version 1.0; gzip_comp_level 2; gzip_proxied any; gzip_min_length 1100; gzip_buffers 16 8k; gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript; # Some version of IE 6 don't handle compression well on some mime-types, # so just disable for them gzip_disable "MSIE [1-6].(?!.*SV1)"; # Set a vary header so downstream proxies don't send cached gzipped # content to IE6 gzip_vary on; server_names_hash_bucket_size 64; reset_timedout_connection on; client_max_body_size 1G; # proxy settings 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; 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; include /etc/nginx/sites-enabled/*; # # The default server # server { listen 80; server_name _; #charset koi8-r; #access_log logs/host.access.log main; location / { root /home/username/public_html; index index.html index.htm index.php; } error_page 404 /404.html; location = /404.html { root /home/username/public_html; } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /home/username/public_html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # location ~ \.php$ { proxy_pass http://127.0.0.1:7475; index index.php; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # Load config files from the /etc/nginx/conf.d directory include /etc/nginx/conf.d/*.conf; }
Tags: , , ,

Comments

  1. […] more about how to setup Nginx as reverse proxy for APACHETags: centos, Nginx, webserver, […]