Setup Nginx as a Reverse proxy for Apache Server

Please visit my website and subscribe to my youtube channel for more articles

https://devops4solutions.com/

In this blog, we will Setup Nginx as a Reverse proxy for Apache Server running on the same server

Requirement:

Install Apache2

sudo apt-get install apache2

sudo service apache2 start

By default, it is running on port 80, open your browser http://yourip

You will see the default page of apache ubuntu server.

Now to check this url is running on which server

Image for post
Image for post

2. Reload the page -> you will see your ip -> click on your ip ->Click Headers

You will see that is running on apache server

Image for post
Image for post

Edit Apache default configuration

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf
Listen 8080<IfModule ssl_module>
Listen 8443
</IfModule>
<IfModule mod_gnutls.c>
Listen 8443
</IfModule>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

2. sudo vi /etc/apache2/sites-enabled/000-default.conf

Change the VirtualHost to run on port 8080. All other setting remains same

<VirtualHost 127.0.0.1:8080>

3. Restart Apache Server

NGINX Configuration

Configure it to use as a proxy for apache server

server {
listen 80;
root /usr/share/nginx/html;
index index.html;
location / {
proxy_pass http://127.0.0.1:8080/;
}
}

3. sudo nginx -t

ubuntu@ip-11–0–11–165:/etc/nginx/conf.d$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

4. Restart or reload nginx

5. Now browse url — http://yourip — this will redirect to apache home page and do the above steps to check which server it is running on , you will see the nginx server

Image for post
Image for post

Below is my security group setting for this EC2 instance — Make sure port 80 is open

Image for post
Image for post

Devops Automation Enginneer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store