Nginx is a lightweight web server that has been proven to serve static files faster than Apache. This tutorial will guide you on how to install Nginx as a reverse proxy over the Apache Web Server.
You have installed Apache on your server. Apache is already running a site on port 80.
Edit /etc/apache2/ports.conf to make Apache listen to a port 8080 instead of the default port 80.
Find the following line:
NameVirtualHost *:80
Listen 80Change it to:
NameVirtualHost *:8080
Listen 8080Do not forget to include your existing vhost listening port in /etc/apache2/sites-enabled/*
Change:
<VirtualHost *:80>To:
<VirtualHost *:8080>Since HTTP requests are now handled by Nginx, we can disable KeepAlive in Apache. Edit /etc/apache2/apache2.conf and change:
KeepAlive OffAlso, run the following commands to disable unused modules.
a2dismod deflate
a2dismod cgi
a2dismod autoindex
a2dismod negotiation
a2dismod sslInstall mod_rpaf in Apache to forward the visitor's IP to Apache. Otherwise, your scripts will read REMOTE_ADDR values as the server IP.
apt-get install libapache2-mod-rpaf/etc/init.d/apache2 restartInstall Nginx.
apt-get install nginxRemove the default vhost to prevent conflicts.
rm -rf /etc/nginx/sites-enabled/*Create a new default vhost:
cat >/etc/nginx/sites-available/000-default <<EOF
server {
access_log off;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
}
EOF
ln -s /etc/nginx/sites-available/000-default /etc/nginx/sites-enabled/000-defaultCreate a vhost for the existing website to forward requests to Apache:
cat >/etc/nginx/sites-available/domain.com <<EOF
server {
server_name www.domain.com domain.com;
root /var/www/domain.com/;
access_log off;
# Static contents
location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
expires max;
}
# Dynamic content, forward to Apache
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
}
EOF
ln -s /etc/nginx/sites-available/domain.com /etc/nginx/sites-enabled/domain.comRestart Nginx, and it’s done.
/etc/init.d/nginx restart