11-10-2015, 08:42 PM
Hello fellow members of post4vps.com or non-member if you have found my tutorial by using a search engine (thanks for your interest in reverse proxies by the way),
I am going to explain you how to configure nginx as a reverse proxy for Apache. Of course you could use any webserver or even a Node.JS server instead of Apache. The same idea although I don't explain you how to configure that server. Apart from that nginx functions as a reverse proxy it also caches static files to make your website even faster!
Requirements:
- Linux-based server (Ubuntu, Debian, CentOS, FreeBSD, Fedora, etc. most will fit.)
- Sudo-privileges. As you are going to install a software package into Linux's filesystem you will need to have sudo-privileges (login as root does also qualify).
- Actually there isn't really a minimum amount of RAM you need, since most of the VPS'ses provided by VPS providers are backed with enough memory. But I'm sure you are good to go with about 512MB RAM.
Some things I have to mention:
- We use nginx listening on port 80. SSL listens on 443 by default, I don't tell you how to configure SSL here.
- Our backend webserver (Apache) listens on port 8080.
- I based this tutorial on how I did this on Ubuntu. You might need to change some thing to make it work on the OS of your choice. The config files are pretty much the same.
!!!!IMPORTANT!!!! PLEASE READ THIS BEFORE TRYING OUT MY TUTORIAL!!!!
Unfortunaly I was unable to test this out, it should work, but I am not sure. The code used here is based on the code I use on my production server. Apart from my production server I have no servers so I were unable to test out as I just said. Using this tutorial is fully on your own responsibility / your own risk. I am not responsible for any damage caused by following my tutorial.
Let's begin!
Step 1: Install Apache, our frontend webserver.
This step is basically one of the most easy one I am going to give.
Ubuntu/Debian:
CentOS, Fedora, SuSE linux, Red Hat, Red Hat Enterprise, other RHEL based:
FreeBSD:
I hope I don't have to explain any of these commands any further. I think you will understand what they will do.
Step 2: Configure nginx.
Create a new nginx site configuration file.
Of course you can use any editor you like like, vi, nano, vim, etc.
First, open up your nginx site's configuration file.
We start by making the file look like this:
Of course, you should change post4vps.com to your own domainname
In that file you will make the following changes:
- Add "location /" to catch every request. In this block we are going to determine whether the request is for an static file or an dynamic file. When it's not an static file, we pass the request to the proxy server (Apache).
In the following scenario, static files are files with the following extensions:
Images: jpeg, jpg, png, gif, bmp, ico, svg, tif, tiff
Web assets: css, js
HTML documents: htm, html
The others: ttf, otf, webp, woff, txt, csv, rtf, doc, docx, xls, xlsx, ppt, pptx, odf, odp, ods, odt, pdf
- Change root to the correct directory. Since the Apache webserver - which we will be going to use for our backend webserver - uses /var/www/html/ we're going to change the root directory to that location.
- You've might already have seen it, in above blocks, we have a @fallback, let's create that "location".
What this block is, every file nginx couldn't provide you will "fallback" on this block, and so it will be proxied to our Apache webserver, which on it's turn will try to solve your request.
- Last but not least, we will add some blocks to deny access to some folders.
After making all these changes, your site's configuration will look like this.
Save this file and quit the editor.
Using nano:
Ctrl+O to save, Ctrl+X to quit.
Using vi / vim:
:wq (write, quit) to save the file and then quit the editor.
Step 3: Activate the virtual host file.
We activate the virtual host file using the following command:
When you've changed the file name in the previous step, also change it here.
Remove the default nginx server block to avoid conflicts.
Restart nginx to make the new server block work.
Step 4: Install Apache, our backend webserver.
This step is one of the most easy one I am going to give.
Ubuntu/Debian:
CentOS, Fedora, SuSE linux, Red Hat, Red Hat Enterprise, other RHEL based:
FreeBSD:
As I've also said at the first step of this tutorial, I hope I don't have to explain any of these commands any further. I think you will understand what they will do.
Step 5: Let Apache listen on 8080 instead of the default port 80.
Open up /etc/apache2/ports.conf or /etc/httpd/ports.conf (according to your OS) in your favorite editor.
Change Listen 80 to Listen 8080.
Then we are going to make a copy of the default VirtualHost.
You might need to replace "apache2" with "httpd", according to your OS. Make it fit your needs eitherway, I based this tutorial on Ubuntu.
Enable the virtualhost
Restart Apache to make your virtual host take effect.
You might need to replace "apache2" with "httpd", according to your OS.
In that file change port 80 to 8080.
For example:
will become
Then you might want to install PHP to Apache. There are plenty of tutorials to do that, so don't be lazy and just look it up by yourself.
To test whether your reverse proxy works just well, put the following into /var/www/html/info.php
Then go to http://DOMAIN/info.php or http://IP/info.php
If you get a page that looks like this, you are good to go!
Thanks for reading my tutorial on how to configure nginx as a reverse / caching proxy for Apache.
REGARDING REPRODUCEMENT
Reproducing my tutorial without my permission is not permitted!! I and only I am allowed to reproduce my own tutorial unless I have permitted you to do and you can show me you have permission.
If you want to post this tutorial on your own website, please send me a PM. Most of the time I will give you permission under the condition that you won't claim it as it's your own .
This tutorial is available on the next websites/forums, with permission:
- freedomain.club
- post4vps.com
I am going to explain you how to configure nginx as a reverse proxy for Apache. Of course you could use any webserver or even a Node.JS server instead of Apache. The same idea although I don't explain you how to configure that server. Apart from that nginx functions as a reverse proxy it also caches static files to make your website even faster!
Requirements:
- Linux-based server (Ubuntu, Debian, CentOS, FreeBSD, Fedora, etc. most will fit.)
- Sudo-privileges. As you are going to install a software package into Linux's filesystem you will need to have sudo-privileges (login as root does also qualify).
- Actually there isn't really a minimum amount of RAM you need, since most of the VPS'ses provided by VPS providers are backed with enough memory. But I'm sure you are good to go with about 512MB RAM.
Some things I have to mention:
- We use nginx listening on port 80. SSL listens on 443 by default, I don't tell you how to configure SSL here.
- Our backend webserver (Apache) listens on port 8080.
- I based this tutorial on how I did this on Ubuntu. You might need to change some thing to make it work on the OS of your choice. The config files are pretty much the same.
!!!!IMPORTANT!!!! PLEASE READ THIS BEFORE TRYING OUT MY TUTORIAL!!!!
Unfortunaly I was unable to test this out, it should work, but I am not sure. The code used here is based on the code I use on my production server. Apart from my production server I have no servers so I were unable to test out as I just said. Using this tutorial is fully on your own responsibility / your own risk. I am not responsible for any damage caused by following my tutorial.
Let's begin!
Step 1: Install Apache, our frontend webserver.
This step is basically one of the most easy one I am going to give.
Ubuntu/Debian:
Code: (Select All)
sudo apt-get install nginx
CentOS, Fedora, SuSE linux, Red Hat, Red Hat Enterprise, other RHEL based:
Code: (Select All)
sudo yum install nginx
FreeBSD:
Code: (Select All)
sudo pkg install nginx
sudo sysrc nginx_enable=yes
sudo service nginx start
I hope I don't have to explain any of these commands any further. I think you will understand what they will do.
Step 2: Configure nginx.
Create a new nginx site configuration file.
Code: (Select All)
sudo nano /etc/nginx/sites-available/reverseproxy
First, open up your nginx site's configuration file.
We start by making the file look like this:
Code: (Select All)
server {
listen 80;
server_name post4vps.com www.post4vps.com;
}
Of course, you should change post4vps.com to your own domainname
In that file you will make the following changes:
- Add "location /" to catch every request. In this block we are going to determine whether the request is for an static file or an dynamic file. When it's not an static file, we pass the request to the proxy server (Apache).
In the following scenario, static files are files with the following extensions:
Images: jpeg, jpg, png, gif, bmp, ico, svg, tif, tiff
Web assets: css, js
HTML documents: htm, html
The others: ttf, otf, webp, woff, txt, csv, rtf, doc, docx, xls, xlsx, ppt, pptx, odf, odp, ods, odt, pdf
Code: (Select All)
location / {
proxy_pass http://127.0.0.1:8080;
location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|tif|tiff|css|js|htm|html|ttf|otf|webp|woff|txt|csv|rtf|doc|docx|xls|xlsx|ppt|pptx|odf|odp|ods|odt|pdf$
root /usr/share/nginx/html/;
expires max;
try_files $uri @fallback;
}
}
- Change root to the correct directory. Since the Apache webserver - which we will be going to use for our backend webserver - uses /var/www/html/ we're going to change the root directory to that location.
Code: (Select All)
location / {
proxy_pass http://127.0.0.1:8080;
location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|tif|tiff|css|js|htm|html|ttf|otf|webp|woff|txt|csv|rtf|doc|docx|xls|xlsx|ppt|pptx|odf|odp|ods|odt|pdf$
root /var/www/html/;
expires max;
try_files $uri @fallback;
}
}
- You've might already have seen it, in above blocks, we have a @fallback, let's create that "location".
What this block is, every file nginx couldn't provide you will "fallback" on this block, and so it will be proxied to our Apache webserver, which on it's turn will try to solve your request.
Code: (Select All)
location @fallback {
proxy_pass http://127.0.0.1:8080;
}
- Last but not least, we will add some blocks to deny access to some folders.
Code: (Select All)
location ~ /\.ht {return 404;}
location ~ /\.svn/ {return 404;}
location ~ /\.git/ {return 404;}
location ~ /\.hg/ {return 404;}
location ~ /\.bzr/ {return 404;}
After making all these changes, your site's configuration will look like this.
Code: (Select All)
server {
listen 80;
server_name post4vps.com www.post4vps.com;
location / {
proxy_pass http://127.0.0.1:8080;
location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|tif|tiff|css|js|htm|html|ttf|otf|webp|woff|txt|csv|rtf|doc|docx|xls|xlsx|ppt|pptx|odf|odp|ods|odt|pdf$
root /usr/share/nginx/html/;
expires max;
try_files $uri @fallback;
}
}
location @fallback {
proxy_pass http://127.0.0.1:8080;
}
location ~ /\.ht {return 404;}
location ~ /\.svn/ {return 404;}
location ~ /\.git/ {return 404;}
location ~ /\.hg/ {return 404;}
location ~ /\.bzr/ {return 404;}
}
Save this file and quit the editor.
Using nano:
Ctrl+O to save, Ctrl+X to quit.
Using vi / vim:
:wq (write, quit) to save the file and then quit the editor.
Step 3: Activate the virtual host file.
We activate the virtual host file using the following command:
Code: (Select All)
sudo ln -s /etc/nginx/sites-available/reverseproxy /etc/nginx/sites-enabled/reverseproxy
When you've changed the file name in the previous step, also change it here.
Remove the default nginx server block to avoid conflicts.
Code: (Select All)
sudo rm /etc/nginx/sites-enabled/default
Restart nginx to make the new server block work.
Code: (Select All)
sudo service nginx restart
Step 4: Install Apache, our backend webserver.
This step is one of the most easy one I am going to give.
Ubuntu/Debian:
Code: (Select All)
sudo apt-get install apache2
CentOS, Fedora, SuSE linux, Red Hat, Red Hat Enterprise, other RHEL based:
Code: (Select All)
sudo yum install httpd
FreeBSD:
Code: (Select All)
sudo pkg install apache24
sudo sysrc apache24_enable=yes
sudo service apache24 start
As I've also said at the first step of this tutorial, I hope I don't have to explain any of these commands any further. I think you will understand what they will do.
Step 5: Let Apache listen on 8080 instead of the default port 80.
Open up /etc/apache2/ports.conf or /etc/httpd/ports.conf (according to your OS) in your favorite editor.
Change Listen 80 to Listen 8080.
Then we are going to make a copy of the default VirtualHost.
Code: (Select All)
sudo cp /etc/apache2/sites-available/000-default /etc/apache2/sites-available/reverseproxy
Enable the virtualhost
Code: (Select All)
sudo a2ensite reverseproxy
Restart Apache to make your virtual host take effect.
Code: (Select All)
sudo /etc/init.d/apache2 restart
In that file change port 80 to 8080.
For example:
Code: (Select All)
<VirtualHost *:80>
Code: (Select All)
<VirtualHost *:8080>
Then you might want to install PHP to Apache. There are plenty of tutorials to do that, so don't be lazy and just look it up by yourself.
To test whether your reverse proxy works just well, put the following into /var/www/html/info.php
Code: (Select All)
<?php phpinfo(); ?>
Then go to http://DOMAIN/info.php or http://IP/info.php
If you get a page that looks like this, you are good to go!
Thanks for reading my tutorial on how to configure nginx as a reverse / caching proxy for Apache.
REGARDING REPRODUCEMENT
Reproducing my tutorial without my permission is not permitted!! I and only I am allowed to reproduce my own tutorial unless I have permitted you to do and you can show me you have permission.
If you want to post this tutorial on your own website, please send me a PM. Most of the time I will give you permission under the condition that you won't claim it as it's your own .
This tutorial is available on the next websites/forums, with permission:
- freedomain.club
- post4vps.com