Apache - treat url path as virtual host

前端 未结 4 1597
不知归路
不知归路 2021-02-04 04:18

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 dif

相关标签:
4条回答
  • 2021-02-04 04:42

    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
    
    0 讨论(0)
  • 2021-02-04 04:43

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

    0 讨论(0)
  • 2021-02-04 04:44

    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')

    0 讨论(0)
  • 2021-02-04 04:55

    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/
    
    0 讨论(0)
提交回复
热议问题