Use nginx as reverse proxy to apache

Consider you have an application running in apache using port 8080. And you have nginx running on the same server listening to port 80. You can then use nginx as reverse proxy to forward requests internally to apache.

#Step 1. create a config file
sudo vim  /etc/nginx/sites-available/apache

#Step 2. copy paste the below lines in that config file.
server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;

    location / {
        proxy_pass http://localhost: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;
    }
}

#Step 3: enable the newly created configuration
sudo ln -s /etc/nginx/sites-available/apache /etc/nginx/sites-enabled/apache

#Step 4: Restart nginx
sudo nginx -t
sudo systemctl reload nginx

Assuming your apache is already running in port 8080, users accessing yourdomain.com will now be automatically forwarded to apache to server the requests.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.