How to redirect to other server? (apache and nginx)

14,654

Are you sure you want to redirect your users? From what I understand, you want server example2.com to proxy to somewhere else. You can setup Apache as a proxy with mod_proxy. And you can also define this proxy for a specific VirtualHost, see Apache's VirtualHost Examples.

For example something like this should work:

NameVirtualHost *:80
<VirtualHost *:80>
    ServerName example.com
<VirtualHost>
<VirtualHost *:80>
    ServerName example2.com
    ProxyPass / http://server2:90/
    ProxyPassReverse / http://server2:90/
    ProxyPreserveHost On  
    ProxyRequests Off
<VirtualHost>

also make sure mod_proxy is loaded

LoadModule proxy_module modules/mod_proxy.so
Share:
14,654

Related videos on Youtube

Christiaan
Author by

Christiaan

Dutch Software Engineer.

Updated on September 18, 2022

Comments

  • Christiaan
    Christiaan almost 2 years

    i have an Apache server running for two sites on port 80. Let's say the domain is example.com and I have an other domain example2.com. When a user visits example.com, I want it to serve those files (so a virtual host) but when someone tries to access the domain example2.com (that is on the same IP Address), I want it to redirect to another IP address on port 90 without the visitor knowing that he is on a different port.

    How can I achieve this?

    <VirtualHost example.com:80>   
     DocumentRoot {rootdir}  
     ServerName example.com  
     ServerAlias example.com 
    </VirtualHost>   
    <VirtualHost *:80>   
      NoProxy .example.com  
      ProxyPreserveHost On  
      ProxyRequests Off  
      ServerName *  
      ProxyPass / http://server2:90  
      ProxyPassReverse / http://server2:90  
    </VirtualHost>  
    

    This won't work and it tries to get example.com with the proxy too, does anyone know why?

  • Christiaan
    Christiaan about 11 years
    Lets say i have 2 different servers, one apache (for myself) and one nginx (for an other site). The requests come in on the apache server, and for one site need to be handeled by apache and the other by nginx. But the proxy will work. Thank you for the help! :)
  • Calimo
    Calimo about 11 years
    You say "doesn't work" but don't provide any detail on what happens. This is not very helpful. Please provide relevant description of what happens, why it isn't what you expected, and server log entries if needed. It looks like you're putting together some pieces of code you don't try to understand. Please refer to Apache manual for the details, for instance the doc of ProxyPass says If the first argument ends with a trailing /, the second argument should also end with a trailing / and vice versa. I edited my original answer with a configuration that should work.