大网数据广告活动
服务器运维问答

Apache 2.4 + PHP-FPM + Dynamic ProxyPassMatch

· 52CE 我爱测

Expect result: If I go to http://foo.local/index.php , file /var/www/html/foo/index.php will be executed. It will be similar if I go to http://bar.local/index.php , /var/www/html/bar/index.php will be executed. Here are my config…

问题原文

原帖链接 · Chung · CC BY-SA 4.0

Expect result:

If I go to http://foo.local/index.php, file /var/www/html/foo/index.php will be executed.

It will be similar if I go to http://bar.local/index.php, /var/www/html/bar/index.php will be executed.

Here are my configurations:

<VirtualHost *:80>
    ServerAlias *.localhost
    VirtualDocumentRoot /var/www/html/%1/

    <IfModule proxy_module>
        #ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://localhost:9000/var/www/html/$1 (this  line works in normal virtualhost)
        ProxyRemoteMatch ^([^.]+).*/(.*\.php(/.*)?)$ fcgi://localhost:9000/var/www/html/$1/$2
    </IfModule>
</VirtualHost>

For now, this is working well for static files like .html. But it will print .php files instead of executing them.

Please advise, thank you.

PS: If I use mod_php instead of php-fpm, it would be easier. But for learning purpose, I really want to go the hard way.

已采纳解答

原帖链接 · Chung · CC BY-SA 4.0

Here is my own solution (use RewriteRule with [P] flag instead of ProxyPass):

<VirtualHost *:80>
    ServerAlias *.localhost
    VirtualDocumentRoot /var/www/html/%1/

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^([^.]+) [NC]
    RewriteRule ^/(.*\.php(/.*)?)$ fcgi://php:9000/var/www/html/%1/$1 [P]
    #<IfModule proxy_module>
        #ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://php:9000/var/www/html/$1
        #ProxyRemoteMatch ^([^.]+).*/(.*\.php(/.*)?)$ fcgi://php:9000/var/www/html/$1/$2
    #</IfModule>
</VirtualHost>

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-02-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。