Nginx and Apache are great and powerful web servers. However, they both have drawbacks; Apache uses up server memory while Nginx (best used for static files) require the help of php-fpm to process dynamic content.
This article takes you on how to configure Nginx and Apache to work together.
Unsure about navigating the intricacies of Nginx and Apache? Dive into our recommended Managed VPS Hosting Providers and let experts streamline the process for you.
Overview
Nginx is an excellent lightweight web server designed to serve high traffic while Apache is another popular web server serving more than half of all active websites in the world. One can combine the two web servers to significant effect, with Nginx serving as static web server front and while Apache is processing the back end. So let’s look into how to configure your Nginx to work with Apache side by side.
Configuring Apache and Nginx
Set different ports for each server. That means you can leave port 80 for Nginx and assign Apache a different port.
Nginx
Install Nginx
Install and configure Nginx that will serve as the front end of your site.
Download from apt-get;
sudo apt-get install nginx
Once it has downloaded, configure the virtual host to run on the front end. However, a few changes are required on the configuration file.
Configure Nginx
Open up the nginx configuration file
sudo nano /etc/nginx/sites-available/example
For example, you could tell apache to listen on 127.0.0.1:8080 and instruct Nginx to reverse –proxy traffic to Apache while still serving static content;
server { listen 127.0.0.1:80; server_name some.name another.dname; access_log /var/log/nginx/something-access.log; location / { proxy_pass http://localhost:8080; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ~* ^.+\.(jpg|js|jpeg|png)$ { root /some/where/on/your/disks; } ; put your static hosting config here. }
Activate the virtual host.
sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/example
Delete the default Nginx server block.
sudo rm /etc/nginx/sites-enabled/default
Apache
Install Apache
Install the backend which is Apache
sudo apt-get install apache2
Apache starts running on port 80 as Nginx is not started. Let’s make Apache listen on a different port so that they can work together.
Configuring Apache
Open Apache ports.conf file using the below command;
sudo nano /etc/apache2/ports.conf
Look for the following line;
Listen 127.0.0.1:80
Moreover, change it to;
Listen 127.0.01:8080
Save and Exit.
Next, edit the default virtual host file in Apache. The <VirtualHost> in this file is set to serve sites only on port 80
sudo nano /etc/apache2/sites-available/000-default.conf
Look for the following line;
<VirtualHost 127.0.0.1:80>
Moreover, change it to;
<VirtualHost 127.0.0.1:8080>
Save the file and reload Apache.
sudo service apache2 reload
Verify that Apache is now listening on 8080.
sudo netstat -tlpn
The output is shown below, with apache2 listening on :::8080.
Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1086/sshd tcp6 0 0 :::8080 :::* LISTEN 4678/apache2
Nginx is configured and running as the front web server on port while Apache is configured to run on the backend on port 8080. Nginx redirects proxy traffic as it still serves static content.
Conclusion
The most important thing we take from this simple configuration is that Apache and Nginx can and do work together. A problem may arise when they both listen to the same ports. By giving them different ports to listen to, your system functionality is assured.
Ready to get started with a Virtual Private Server? Visit our VPS Hosting page to find the Best VPS Hosting Providers that’s right for you
Check out the top 3 Linux hosting services
- Compare all the best Linux Hosting Providers
- Want suggestions about Dedicated Servers? Find the Top Dedicated Server Hosting Providers