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

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 / ProxyPassReverse /
</VirtualHost> 

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

1 Answer

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 / ProxyPassReverse / ProxyPreserveHost On ProxyRequests Off
<VirtualHost>

also make sure mod_proxy is loaded

LoadModule proxy_module modules/mod_proxy.so
3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like