Running Nginx With Apache, With Reverse Proxy
When I migrate my wordpress-es to VPS, Apache was the default setup.
Problem: I wanted to run some Node.js apps in the same VPS, but node requires nginx.
Hence, I configured my VPS to run both Apache and Nginx.
That’s possible, when you make nginx a reverse proxy server.
The plan is as such:
- nginx listens to port 80
- nginx 1st server block is configured for node app at port 8021
- nginx 2nd server block is configured to proxy to apache at port 8011
- apache listen to port 8011
- apache’s virtual hosts listen to port 8011
The details and a sample of the .conf
files as follows. My scenario is using nginxapp.com
and apacheapp.com
Nginx
There are 2 server blocks, which I put in 2 seperate conf files: nginxapp.com.conf
and apache-proxy.conf
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Note: You should setup your node app and listen on 8021. In the meanwhile (testing), you can put a dummy index.html in /var/www/nginxapp.com/
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Enable the nginx conf with these commands:
1 2 |
|
Apache
For apache, you need to change the port file to listen to 8011.
1 2 3 |
|
Below is a sample of a virtual host file. You need to change for all. The change is again on the listening port.
1 2 3 |
|
Restart
With that, restart each service with apache first, then nginx.
1 2 |
|
Now, you if you go to http://apacheapp.com
, it should work, and it is because nginx is proxying nicely to apache.
You can also go to http://apacheapp.com:8011
, which is directly to apache.
And of course, you can now write your node app at http://nodeapp.com
.
Other guides
I referenced from some of Digital Ocean guides: