服务两个不同版本的php应用程序

时间:2019-08-21 05:54:11

标签: php nginx

我正在尝试在nginx中运行两个几乎相同的php应用程序。由于api的微小更改,这是一个临时解决方案。我想使用诸如v=testing之类的查询字符串,并让nginx服务不同的应用程序。

使用ubuntu 16.04,nginx / 1.10.3,PHP 7.0.33

example.com_2是第二个php应用程序。我尝试了以下方法:

server {
    server_name example.localhost;
    root   /var/www/example.com/public;
    index index.php index.html;
    listen 80;

    include php_biz.conf;
}

location / {
    if ($arg_v = testing) {
        root /var/www/example.com_2/public;
    }
    try_files $uri $uri/ /index.php;
}

location ~ .*\.php$ {
    try_files $uri =404; # 404 if file doesnt exist
    include        fastcgi_params;
    fastcgi_pass   php_biz;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
}
upstream php_biz {
    server unix:/run/php/php7.0-fpm-biz.sock;
}

当我访问example.com_2 php应用程序时,我从nginx而不是从php应用程序获取404。从example.com_2访问静态文件有效。

因此,似乎在if块中设置root不会对php应用程序起作用,而对static起作用。我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

尝试使用地图:

map $arg_v $app_suffix {
    default "";
    testing "_2";
}
server {
    server_name example.localhost;
    root   /var/www/example.com${app_suffix}/public;
    index index.php index.html;
    listen 80;

    include php_biz.conf;
}

location / {
    try_files $uri $uri/ /index.php;
}

location ~ .*\.php$ {
    try_files $uri =404; # 404 if file doesnt exist
    include        fastcgi_params;
    fastcgi_pass   php_biz;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
}
upstream php_biz {
    server unix:/run/php/php7.0-fpm-biz.sock;
}
相关问题