Hello arunsinghbhati,
You can check this documentation as reference for the steps provided below:
1. Install Nginx
To begin, access your server’s terminal via SSH. Then use the **apt-get** command to update your distribution’s packages list and install Nginx on your web server.
sudo apt update
sudo apt install nginx
2. Configure Nginx to Proxy Requests
Next, you need to configure Nginx to proxy requests for domains hosted on Apache. To do that, create a new virtual host file. Here, I’m using the nano editor to add the code, but you can use any code editor of your choice.
sudo nano /etc/nginx/sites-available/example.com.conf
Then set Nginx directives to forward requests to Apache by adding the following **server {...}** and **location** blocks:
server {
listen 80;
server_name example.com www.example.com;
index index.php;
root /var/www/example.com/public # fallback for index.php
location / {
try_files $uri $uri/ /index.php?$query_string;
}location /blog {
proxy_pass http://blog.domain.com;proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
# Proxy headers
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
# Proxy timeouts
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
In the code above, I’m defining a subdirectory **example.com/blog** link that will be served by the Apache server. Ensure that you use your proxied website’s public IP address (or URL) in the **proxy_pass** directive. In my case, my proxied website is hosted on the **blog.domain.com** subdomain.
Note: Ensure that the proxied website is installed and ready to be served before you make any changes.
You can learn more about all the reverse proxy directives used here in Nginx’s detailed index of directives.
3. Save the Virtual Host File Created
Then activate the new virtual host by creating a symlink for the files named **example.com.conf** in both the **/etc/nginx/sites-available** and the **/etc/nginx/sites-enabled** directories.
sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/example.com.conf
4. Test Nginx for Errors
After that, test Nginx for any configuration errors.
sudo nginx -t
If there are no errors, reload Nginx to enforce the changes.
sudo systemctl reload nginx
You’ve successfully set up Nginx to work as a reverse proxy now. To confirm this, you can use the phpinfo() function to check the PHP variables loaded when you visit your proxied site.
Under the **SERVER_SOFTWARE** and **DOCUMENT_ROOT** PHP variables, you’ll see that Apache serves this domain on the backend. But **HTTP_X_REAL_IP** and **HTTP_X_FORWARDED_FOR** PHP variables confirm that Nginx was used as a reverse proxy to forward the requests.
If the steps above are not what you’re looking for, here’s another reference that you can check
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([a-z0-9-]+)/? http://$1.example.com [R=301,NC,L]
Replace both instances of “example.com” with your domain name