Apache + nginx as reverse proxy
One of the things I was planning to do but never did is installing nginx as reverse proxy in front of Apache.
nginx is present in the epel repos for CentOS, so the installation process is just a matter of:
yum install nginx mysql mysql-server phpmyadmin httpd
Add another repository for the Apache module mod_rpaf…
wget -q -O - http://www.atomicorp.com/installers/atomic | sh
…and install it:
yum install mod_rpaf
At this point everything needed should be installed, now it’s time to configure Apache and nginx to work together.
Create a directory for nginx virtual hosts:
mkdir /etc/nginx/v.hosts
Pick your favourite editor (vi in my case) and edit the following file…
vi /etc/nginx/nginx.con
…and replace the default “http” section with the following one:
http { include v.hosts/*.conf; 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; # Default Server Block to catch undefined host names # server { # listen 80; # server_name _; # root /usr/share/nginx/html; # index index.html index.htm; } }
Test nginx configuration with:
/usr/sbin/nginx -t
If it reports “rest successful” you are good to go, create a file called “site_address.conf” in the previously created “v.host” folder and paste the following code in it:
server { listen 80; server_name uwot.eu; access_log off; error_log off; location / { proxy_pass http://127.0.0.1:8080; 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; } }
Retest the configuration and start nginx service:
sudo /usr/sbin/nginx -t service nginx restart
Edit “/etc/httpd/conf.d/mod_rpaf.conf” and check that the configuration section “IfModule mod_rpaf.c” is similar to what follows:
RPAF_enable On RPAF_sethostname On RPAF_ProxyIPs 127.0.0.1:80 RPAF_header X-Forwarded-For
The last thing to edit is “httpd.conf” to make Apache listen to a port different from 80 because nginx is already on it.
“vi /etc/httpd/conf/httpd.conf”, search for the line “Listen 80” and replace it with “Listen 127.0.0.1:8080”.
This is it, restart both httpd and nginx and enable automatic startup.