位置块&;FastCGI代理


Location Block & FastCGI Proxy

我正在尝试进行以下配置;在此之前,我有一个正在运行的Nginx/PHP FastCGI实现,它在一个服务器上运行,该服务器只有一个ip地址,没有FQDN和/或DNS,为一个基于PHP的网站/应用程序提供服务。现在,我得到了关于另一台服务器的相同场景,不同之处在于需要为多个客户端站点(其中一个是Wordpress站点)提供服务器。到目前为止,我也不是一个Nginx专家。

概述:服务器块的文档根是'/usr/share/nginx/html',这是Nginx的默认值。服务很好,不是问题http://xxx.xxx.xxx.76.现在的要求是让客户端输入他们的浏览器地址http://xxx.xxx.xxx.76/pmhs作为一个例子。然后,他们基于php的网站将被提供,依此类推http://xxx.xxx.xxx.76/client以便配置和服务任何其他站点。

这些客户端的文档根目录位于标准的CentOS 7文件夹'/srv'中,其中所有客户端都配置为在其'/srv/www/{client}.production/public_html'文件夹中具有可用的站点内容。我所能挖掘到的大多数配置示例并不是真正针对这种类型的配置,大多数(如果不是全部的话)都涉及某种服务器和/或dns,这使得路由更容易理解(在我看来)。

server {
    ## -------------------------------------------------
    #   define virtual server configuration
    ## -------------------------------------------------
    listen          80;
    server_name     localhost;
    root        /usr/share/nginx/html;
    index       index.html index.htm index.php;
    access_log      /var/log/nginx/default.access.log  main;
    error_log       /var/log/nginx/default.error.log debug;
    rewrite_log     on;
    ## -------------------------------------------------
    #   default site / ip address 
    #     @ serve nginx welcome page
    ## -------------------------------------------------
    location = / {
    try_files $uri $uri/;
    }
    ## -------------------------------------------------
    #   favicon.ico location filter
    ## -------------------------------------------------
    location = /favicon.ico { access_log off; log_not_found off; }
    ## -------------------------------------------------
    #   do not serve hidden files
    ## -------------------------------------------------
    location ~ /'. { access_log off; log_not_found off; deny all; }
    ## -------------------------------------------------
    #   client website location block
    ## -------------------------------------------------
    location ~ ^'/(?<client>['w-_]+) {
        # reset the document root for the client
        #root /srv/www/$client.production/public_html;
        alias /srv/www/$client.production/public_html;
        # set the port used for the clients fastcgi pool
        if ($client = "belmond") { set $port 9000; }
        if ($client = "freeboard") { set $port 9001; }
        if ($client = "pmhs") { set $port 9002; }
        if ($client = "vesta") { set $port 9003; }
        #return 200 $document_root$uri;
        # nginx pass to php fastcgi - serve client web
        #location ~ [^/]'.php(/|$) {
        #}
    }
    location @fastcgi_proxy {
        fastcgi_split_path_info ^(.+?'.php)(.*)$;
        set $orig_path $fastcgi_path_info;
        try_files $fastcgi_script_name =404;
        fastcgi_pass                        127.0.0.1:9000;
        fastcgi_index                       index.php;
        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME       $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO             $orig_path;
        fastcgi_param PATH_TRANSLATED       $document_root$orig_path;
        set $temp                           "/var/lib/php/fpm/session";
        fastcgi_param TEMP                  $temp;
        fastcgi_read_timeout                500;
        fastcgi_ignore_client_abort         on;
        fastcgi_connect_timeout             60;
        fastcgi_send_timeout                180;
        fastcgi_buffer_size                 8k;
        fastcgi_buffers     64              8k;
        fastcgi_temp_file_write_size        256k;
    }
    ## -------------------------------------------------
    #   redirect server error pages 
    #     @ serve nginx static page(s) /50x.html
    ## -------------------------------------------------
    error_page  500 502 503 504  /50x.html;
    location = /50x.html {
    try_files $uri $uri/;
    }
}

我可以用正则表达式捕捉客户端位置,但根据我所看到的情况,我开始对下一步该做什么感到有点"模糊"。当我CURL到基本的ip addy时,这是预期的,Nginx的"欢迎"页面会返回。

现在,当我用客户端名称传递相同的addy时(返回200$document_root$uri;未注释),document_url已经被正确地别名,但现在我有点不知所措。最终,我希望将所有客户端站点配置为代理php-fpm-fastcgi服务代理(这就是为什么定义了$port代码,但尚未使用)。

只是在寻找如何清理并使其正常工作的任何方向等,同时在此过程中进一步教育自己Nginx配置。。。

目标是针对主ip地址(当前根据此配置运行):

URL = xxx.xxx.xxx.76 or xxx.xxx.xxx.76/ 
[serve] /usr/share/nginx/html/(*.html) content
[from] root /usr/share/nginx/html; 

(需要确定如何从服务器目录为客户端站点提供服务)

URL = xxx.xxx.xxx.76/pmhs or xxx.xxx.xxx.76/pmhs/ 
[serve] /srv/www/pmhs.production/public_html/(*.php) 
[from] root srv/www/pmhs.production/public_html; 
URL = xxx.xxx.xxx.76/acme or xxx.xxx.xxx.76/acme/ 
[serve] /srv/www/acme.production/public_html/(*.php) 
[from ] root srv/www/acme.production/public_html; 

这些有道理吗?我想我已经很接近了,但我只是不知道是不是这样。

好吧,在我注意到"客户端"位置块中发生了什么之后,纠正这个特定配置的底线是更改服务器上的文件路径。这是最后的配置块代码:

## -------------------------------------------------
#   client website location block
## -------------------------------------------------
location ~ ^'/(?<client>['w-_]+) {
    # reset the document root for client sites
    root /srv/www;
    # nginx pass to php fastcgi - serve client web
    location ~ [^/]'.php(/|$) {
        fastcgi_split_path_info ^(.+?'.php)(.*)$;
        set $original_path $fastcgi_path_info;
        # set the port used for the clients fastcgi pool
        if ($client = "phms") { set $port 9000; }
        if ($client = "vesta") { set $port 9001; }
        try_files $fastcgi_script_name =404;
        fastcgi_pass                        127.0.0.1:$port;
        fastcgi_index                       index.php;
        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME       $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO             $original_path;
        fastcgi_param PATH_TRANSLATED       $document_root$original_path;
        set $temp                           "/var/lib/php/fpm/session";
        fastcgi_param TEMP                  $temp;
        fastcgi_read_timeout                500;
        fastcgi_ignore_client_abort         on;
        fastcgi_connect_timeout             60;
        fastcgi_send_timeout                180;
        fastcgi_buffer_size                 8k;
        fastcgi_buffers     64              8k;
        fastcgi_temp_file_write_size        256k;
    }
}

为了简洁起见,它实际上是根据我注意到的调试值来更改文档根的文件路径。我本可以找到最初的工作路径,但这需要付出真正的努力,而且我们没有分配时间——在这一点上,我也不愿意与nginx"对抗市政厅",让事情与内部客户端文件系统定义一起工作。

最重要的是,它正在发挥作用。。。对于任何关注这一点的php-fpm开发人员来说,有一点很重要,那就是每个客户端的php-fpmpool文件都要求在下面$port变量的配置"if"代码中镜像"listen"参数中使用的特定端口。

感谢所有觉得有必要为投入做出贡献的人!