Nginx and apache has been widely used in the IT industry for very long time. Nginx is good as it can handle multiple concurrent request in which Apache was struggling. When apache creates a seperate child processes for each request and each process work on a blocking thread, nginx runs multiple non-blocking threads. There are several pro and cons of nginx but i personally prefer to use the best of both of them. Let’s try to host a website and make use of both nginx and apache (httpd).
First, Install apache in server using command
yum install httpd -y
Once the installation has been done, you need to do some configuration in httpd.conf file which lies in
Here search for the line which say listen 80. What we are trying to achieve is apache hosting the actual content of site where as nginx will handle the incoming request from client at standard HTTP port i.e 80. So, we are going to change the port at which apache will run to 8080. Change the line
Also, find the line that says DocumentRoot and provide the path of the website contents. Mine is in /var/www/html
That’s it. You have configured http server i.e apache at port 8080. Start apache server with command
systemctl start httpd
If you want to check the status, you can issue the command systemctl status httpd. Now, we have hosted the website at port 8080. What we need to do is redirect the incoming request at port 80 to port 8080. For this we will use nginx as a reverse proxy.
Let’s first install nginx with command
yum install nginx -y
Once installation has been done, go to /etc/nginx/nginx.conf. Open that file you will see something like this.
You will also see a server block, which i have already deleted. Delete the server block inside the http block. We will create new server block later. save the file and exit.
now go inside the conf.d directory in /etc/nginx. Create a new file and save it as any name you want forexample awssite.conf . Now, open the file and write following.
Once you have written all of these, you can see a interesting element called proxy_pass. This is what will help us sent traffic to apache. Here proxy_pass will listen at port 80, which can be seen as we ran server at made it listen at port 80. Any request for “/” will send it to localhost:8080 and as we did before we have apache running and ready to server content for request coming at port 8080.
Now save the file and and start nginx using command
systemctl start nginx
to check which port nginx and apache is running, run this command
From above image we can see that nginx is running at port 80 and apache (httpd) is running at port 8080. Now if we try to browse the website then nginx will forward that request to apache server which will then serve the content.
This is a basic way how you can use nginx in the front to serve requests and apache in the back to serve web content.