Apache - treat url path as virtual host

2019-03-18 17:01发布

问题:

The problem is that I have only one domain name on which 3 different products need to be run (2 of them PHP based, 1 python). So what I need to do is to treat path in url as different virtual host; ie:

www.domain.com/first_url/
www.domain.com/second_url/
www.domain.com/third_url/

Where first to third will act as separate virtual hosts.

Any clue how to do this?

回答1:

You probably want to do something with the apache-config directives, since you're asking for a virtualhost solution. Apache can only work with virtualHosts as actual domains, as @cweiske explained.

The solution in this case would be to either use a .htaccess file in the sub-directories you're working in, or to set up a <Directory "/web/root/subdir">..</Directory> block within your current (virtual-)host config.

You could also choose to host them on different sub-domains if you per-se want to run them as VirtualHosts ('app1.domain.org')



回答2:

It's been a while since this question was asked but since I was looking for a solution for a similar kind of problem, I'll add the solution.

This can be achieved by using Alias or AliasMatch directive. More details can be found here:

http://httpd.apache.org/docs/2.2/mod/mod_alias.html

Alias /first_url/ /var/www/first_url_resources


回答3:

A "virtual host" in apache works on domain names only, not on parts of the path. You cannot achieve what you want.



回答4:

This example explains how to assign different PHP version per directory, it can also be adapted to add Python support by running Python interpreter as fast_cgi on particular port

For the purpose of the example I assume there is separate directory for each PHP version and they are named according to PHP version that runs them, but this can be adjusted

mkdir /home/user/www
mkdir /home/user/www/5.6.5
mkdir /home/user/www/7.0.2
mkdir /home/user/www/7.0.4
mkdir /home/user/www/7.0.6

create symbolic links to directories that should be handled by different PHP versions

sudo ln -s /home/user/www/7.0.2/ /var/www/html/7.0.2
sudo ln -s /home/user/www/7.0.4/ /var/www/html/7.0.4
sudo ln -s /home/user/www/7.0.6/ /var/www/html/7.0.6

then add following lines to /etc/apache2/sites-enabled/000-default.conf in default virtual host *:80

(for your need you can setup one more fast cgi handler here for the website that requires Python), I assume php 5.6.5 runs on port 9999, 7.0.2 runs on port 9998 etc...

DirectoryIndex index.html index.php
ProxyPassMatch ^/5.6.5/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9999/var/www/html/
ProxyPassMatch ^/7.0.2/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9998/var/www/html/
ProxyPassMatch ^/7.0.4/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9997/var/www/html/
ProxyPassMatch ^/7.0.6/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9996/var/www/html/

assuming your server is pointed by example.com you can test it on

http://example.com/5.6.5/
http://example.com/7.0.2/
http://example.com/7.0.4/
http://example.com/7.0.6/