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;



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.

  • Thanks! I will try this, sorry I am not a pro at nginx configs, will each of the redirects need to be in a separate server block? – YemSalat Aug 26 '15 at 6:46
  • 1
    Yes. Create a seperate server block for each virtual host domain & apply proxy_pass rules to each of them. – serverliving.com Aug 26 '15 at 6:49
  • Thanks again! (I updated my question and added nginx config example - do you reckon it looks OK?) – YemSalat Aug 26 '15 at 6:58
  • Do you only want .php files to be handled by Apache? – serverliving.com Aug 26 '15 at 7:03
  • 1
    In that case your rule should forward like this proxy_pass . And I believe you need to keep the DocumentRoot value in apache to /var/www/ just – serverliving.com Aug 26 '15 at 7:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.