Wednesday, September 3, 2014

centos - How to restrict Apache Location directive to cetain sub-domain?



On our server www.example.com, we use the directive to proxy traffic to a back-end server:




ProxyPass http://192.168.1.20




Then we added a sub-domain uat.example.com which points to the same IP address of www.example.com. We want to use it as a proxy for client to test an app being developed. Hopefully, the client can access via:



http://uat.example.com/app2_uat


Now if we add a Location:




ProxyPass http://192.168.1.30




The client can access both:



http://www.example.com/app2_uat
http://uat.example.com/app2_uat


How can I restrict Location such that only:




http://uat.example.com/app2_uat


Is accessible? (i.e. http://www.example.com/app2_uat should not be accessible.)


Answer



That’s easy to do if you use the NameVirtualHost option in Apache. I have a detailed answer provided for another user over here. The basic concept is you will be creating two different VirtualHost configs for each subdomain. Once you do that you can customize the configuration of both subdomains & achieve the granularity you are looking for.



EDIT: Adding details to how to setup NameVirtualHost for the original posters specific question details:




In general you need to first activate NameVirtualHost for the port you want. I will assume you will use port 80, so find this line in your Apache config—it might be in /etc/apache2/ports.conf—& set as so:



NameVirtualHost *:80


Make sure your Apache config—chances are the same /etc/apache2/ports.conf as above—is set to list to port 80. Which should be the case, but adding here for reference:



Listen 80



Here is where the magic happens. Again, I am just doing the bare-bones basics so adjust to whatever your server settings are. This is for www.example.com:




ServerName www.example.com
ServerAlias www.example.com


ProxyPass http://192.168.1.20






And now for uat.example.com create a new config like so:




ServerName uat.example.com
ServerAlias uat.example.com



ProxyPass http://192.168.1.30





By using NameVirtualHost you can setup two separate domain configs tied to one IP address. Again, please review the answer I linked to above for more details.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...