Sometimes people become amazed with an awesome feature of his new shiny tool, and never discover that it was already there for years in his old boring tool. A good example of this phenomenon is – I see people choosing nginx for it’s ability of listening to multiple ports and domains.
Definitely there are solid reasons and cases for choosing nginx. But, I’ll say, this one alone is not a good reason for switching to nginx if you’re already running on and confident with Apache Server. Because, you can serve multiple apps/sites on different ports or domains using Apache too.
Let’s see some quick example of how Apache can do it. I’ll here just list a few example of Apache VirtualHost configs for various cases with a single server.
A. Multiple sites with different domain names
Basic, simple name based VirtualHosts.
Listen 80 <VirtualHost 22.214.171.124:80> ServerName www.mysite.com DocumentRoot "/www/mysite" </VirtualHost> <VirtualHost 126.96.36.199:80> ServerName www.othersite.org DocumentRoot "/www/othersite" </VirtualHost>
B. Multiple applications on different ports
Listen to multiple port and map them to different root directory.
Listen 80 Listen 8080 <VirtualHost 188.8.131.52:80> ServerName www.example.com DocumentRoot "/www/domain-80" </VirtualHost> <VirtualHost 184.108.40.206:8080> ServerName www.example.com DocumentRoot "/www/domain-8080" </VirtualHost>
C. Same content to multiple IPs
Simply list all the IPs in your VirtualHost config.
<VirtualHost 220.127.116.11 18.104.22.168> DocumentRoot "/www/server1" ServerName server.example.com </VirtualHost>
D. Make a catch-all/any domain host for a port
<VirtualHost _default_:8000> DocumentRoot "/www/default8000" # ... </VirtualHost>
There are lots of other interesting things you can do with Apache VirtualHost. For example, listening to subdomains, proxy_pass to other IP or Port, mixing IP and name based host etc. So, please don’t forget to check Apache’s VirtualHost guide and .htaccess tricks before thinking “may be it’s not possible with Apache”.