NGINX,将几个本地主机转发到php-fpm [英] NGINX, forwarding few localhosts to php-fpm

查看:119
本文介绍了NGINX,将几个本地主机转发到php-fpm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持简单的事情,请帮忙. 我有2个包含PHP项目的目录:/var/www/api/和/var/www/api-beta/. 我想将它们中的每一个转发到PHP-FPM. Nginx配置:

I've stuck on simple thing, please help. I have 2 directories with PHP projects: /var/www/api/ and /var/www/api-beta/. I want to forwarding each of them to PHP-FPM. Nginx config:

server {
    listen 80;
    set $doc_root /var/www/api;
    root $doc_root;
    index  index.php index.html;


  location /beta {
            alias /var/www/api-beta;
    }


    location ~ \.php$ {
        set $php_root /var/www/api;
        if ($request_uri ~* /beta) {
             set $php_root /var/www/api-beta;
              }

            fastcgi_pass   unix:/var/run/php/php7.0-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $php_root$fastcgi_script_name;
            include /etc/nginx/fastcgi_params;


    }
}

我尝试使用if ($request_uri ~* /beta)来执行此操作,但是它不起作用.我认为这是有问题的,因为来自/var/www/api的项目工作正常,但是来自/var/www/api-beta的项目却显示找不到文件".错误.

I've tried do this with if ($request_uri ~* /beta) but it didn't work. I think problem this, because project from /var/www/api works fine, but from /var/www/api-beta I have "File not found." error.

推荐答案

为每个PHP根目录创建location块可能更简单:

It may be simpler to create a location block for each PHP root:

server {
    listen 80;
    root /var/www/api;
    index  index.php index.html;

    location ~ \.php$ {
        try_files $uri =404;

        include /etc/nginx/fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME  $request_filename;
        fastcgi_pass   unix:/var/run/php/php7.0-fpm.sock;
    }

    location ^~ /beta {
        alias /var/www/api-beta;

        location ~ \.php$ {
            if (!-f $request_filename) { return 404; }

            include /etc/nginx/fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME  $request_filename;
            fastcgi_pass   unix:/var/run/php/php7.0-fpm.sock;
        }
    }
}

注意:

  • 避免同时使用别名和try_files.请参见这个长期存在的问题.
  • ^~修饰符使前缀位置优先于上述正则表达式位置.有关更多信息,请参见本文档.
  • avoid using alias and try_files together. See this long standing issue.
  • the ^~ modifier cause the prefix location to take precedence over the regular expression location above. See this document for more.

这篇关于NGINX,将几个本地主机转发到php-fpm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆