X
X

Select Your Currency

Türk Lirası $ US Dollar
X
X

Select Your Currency

Türk Lirası $ US Dollar

Nginx Reverse Proxy Installation (Usage in Front of Apache)

HomepageNews from UsNginx Reverse Proxy Installation (Usage in Fro...

Why Use Both Nginx and Apache Together?

In a classic LAMP (Linux, Apache, MySQL, PHP) server, Apache processes PHP and also transmits static files like images, css, js to the visitor. However, since Apache opens a new process for each connection, it consumes unnecessarily high RAM even when distributing static files on a virtual server. The solution is to install Nginx right in front of Apache as a "Reverse Proxy". While Nginx serves static files in milliseconds with its asynchronous structure, it passes dynamic PHP requests to Apache running in the background (port 8080). This duo turns into a perfect performance monster.

Step 1: Changing the Apache Port

First, Apache needs to withdraw from the default 80 (HTTP) profile and run in the background. Open the /etc/apache2/ports.conf file on Ubuntu/Debian systems:

Listen 8080

Likewise, change the <VirtualHost *:80> parts in your virtual host files to <VirtualHost *:8080> and restart Apache: sudo systemctl restart apache2

Step 2: Nginx Installation and Proxy Settings

Let's install Nginx to take over the freed-up port 80:

sudo apt update
sudo apt install nginx -y

Now edit the content of the /etc/nginx/sites-available/default (or your own domain) file according to the Proxy template below:

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location ~* \.(jpg|jpeg|png|gif|ico|css|js|xml|txt)$ {
        root /var/www/html/yourdomain;
        expires 30d;
        access_log off;
    }
}

Step 3: How the Logic Works

In the configuration above, the bottom location ~* block tells Nginx to read it straight from the folder (without ever asking Apache) if the request comes for an image or css file, and adds a 30-day cache period to the browser. All other dynamic requests (PHP) are forwarded to Apache in the back with the proxy_pass http://127.0.0.1:8080; rule.

Let's apply the settings and start Nginx: sudo systemctl restart nginx.

When your site's traffic increases, using server resources most efficiently is a necessity. In your VDS Server services with NVMe SSD disks that you will purchase from İyibirNet, you can freely set up the server architecture you desire.


Top