Nginx and apache – multiple virtual hosts on the same IP – correct configuration

apache-2.2nginxproxypassreverse-proxy

I am trying to run nginx as reverse proxy for Apache on the same machine and serve different websites from it.

My question is – is it possible to add virtual hosts only to nginx and have it pass the url/hostname/path, etc to Apache automatically depending on which host is requested.

OR do I need to set up a virtual host for every site (domain) in both nginx and Apache?

Also, are there any potential issues with this setup?

What I am planning to have in my nginx config is something like this for each domain (Apache is running on port 8080):

    server {
            listen 80; 

            root /var/www/site1.com/;
            server_name site1.com; 

            location / {
                    try_files $uri $uri/ /index.php;
            }

            location ~ \.php$ {                
                    proxy_set_header X-Real-IP  $remote_addr;
                    proxy_set_header X-Forwarded-For $remote_addr;
                    proxy_set_header Host $host;
                    proxy_pass http://127.0.0.1:8080;
            }
    }

Thanks!

Best Answer

This is possible.

On Apache web server, you need to store all the files on a default virtual host so it can be accessed as http://IP_ADDRESS:PORT/hostname/path/

On Nginx server create multiple virtual hosts & add a proy_pass rule like below.

A.com -> / will proxy forward to /a-com/ B.com -> / will proxy forward to /b-com/ C.com -> / will proxy forward to /c-com/

Hope this helps you.