The aim was to achieve the following, a user will connect to machine1 running on Nginx at port 80. The Nginx will serve the files unless the URL is of the form test1 or test2, if the url is of the form test1 it will forward the request to an apache server running on the same machine at port 85. If it is of form test2/ then the request will be sent to another machine and that response will be served to the user.
Both the machines are running on Lubuntu in Vbox.
Setting up Machine1:
In machine1 Nginx was installed first
Sudo apt install nginx
Followed by Apache2
sudo apt install apache2
Now, apache2 will fail to start since Nginx is already listening on port 80. To change the listening port edit /etc/apache2/ports.conf and change the line Listen 80 to Listen 85
You must also edit /etc/apache2/sites-enabled/000-default.conf and make the following changes
- <VirtualHost *: 80> should be changed as <VirtualHost *: 85>
- change the DocumentRoot from /var/www/html to /var/www/html_apache
Open folder /var/www/html and CUT and paste index.html to /var/www/html_apache
If you are unable to save changes to files and get a permission error, run your text editor as sudo. If you get errors while copy-pasting index files run the file manager as root. In Lubuntu you can open a terminal and type sudo pcmanfm
Also, create an index.html inside /var/www/html_apache/test1 and include some text so that you know the response is from apache.
Restart apache with the command
sudo service apache2 restartNow opening Localhost should point to Nginx welcome page and localhost:85 should show the apache welcome page
Setting up Machine2:
In machine2 you can simply install apache2, no special changes are required. Get the local IP address of machine2 using the command ifconfig (mine was 192.168.1.5). Also, create an index.html inside /var/www/html/test2 and include some textI haven't set up PHP or database. You can do that if you want.
Setting up the reverse proxy:
To set up the reverse proxy, in machine1 you should edit the file /etc/nginx/sites-available/default (another approach will be to create a new conf file and linking it to sites-enabled, but I will be editing the default file). Find the block that says location / and at the end of that block add a new location as shown below
location /test1/ {proxy_pass http://127.0.0.1:85;}
This will pass any request of the form /test1/ to localhost:85/test1/ . For example, a request like /test1/apix will be forwarded to apache in machine1 as localhost:85/test1/apix
Similarly, for test2, you can just add the IP address of machine2 (if you are hosting it somewhere use its public IP) instead of the localhost address
location /test2/ {
proxy_pass http://192.168.1.5;
}And that's it, you may also need to forward the ip address of the user to the apache servers. If not the address of the Nginx server will be recorded for each request. To do so add the headers as shown below
location /test1/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:85;
}You can also set the reverse proxy in such a way that all php files will be served from a particular server.

No comments:
Post a Comment