Nginx – Setup a Reverse Proxy with Nginx and Apache on EC2

amazon ec2apache-2.2nginx

Good Day,

I am currently using the free Amazon EC2 micro instance to learn Linux and server setup.

I wish to setup Nginx as a reverse web proxy. I found a great article on mediatemple on how to do it: http://wiki.mediatemple.net/w/Using_Nginx_as_a_Reverse_Web_Proxy

The directions work for most any server except for EC2.One difference between EC2 and MediaTemple is how IPs work. Overall EC2 instances do not know their elastic IP. So when following the wiki directions in the virtual hosts for instance instead of myip:80 for instance I put *:80. When just using Apache this works perfectly.

In the apache virtual hosts I did "127.0.0.1:80" and in the Nginx I put *:80.

Apache restarts, by Nginx provides an error that it cannot bind because the ip is already in use. If I could add an actual IP in the Nginx file it would work but since EC2 requires me to put in the asterisk it ends up conflicting with the apache virtual hosts entry.

Anyone know a simple way around this (other than not using EC2) 😉 Thank you!

Cheers,
Christopher

Best Answer

Your problem has nothing to do with Elastic IPs. The issue is that both Nginx and Apache can't both be listening on Port 80. The simple solution is to change the port that Apache listens on, to 81 or 8080 as examples, while allowing Nginx to listen on port 80 (because when someone types in your URL, you want the request going to Nginx, not straight to Apache).

Basically, what you want to do is go through that tutorial, and whenever you see a reference to Apache and port 80, use a different port like 8080 or 81. Then you need to update your Nginx config to tell it to look for Apache on your new port.

Restart both services and you will be good to go.

Related Topic