nginx 动态主机和目录 path_info 缺失

nginx 动态主机和目录 path_info 缺失

我正在尝试完成一个 nginx 配置,它允许我根据 URL/ServerName 的一部分来选择项目文件夹(document_root),全部是动态的。

如果我得到一个类似这样的 Web 服务器目录:

/var/www/projecta
/var/www/project-b
/var/www/projectc

我得到了以下 URL 匹配:

www.something.projecta.demo.com -> /var/www/projecta/
xyz.project-b.demo.com -> /var/www/projectb/
aaa.bbb.ccc.ddd.projectc.demo.com -> /var/www/projectc/

等等。

如果尝试使用以下配置,它可以处理静态文件,但不适用于 PHP。据我所知,path_info 始终为空。

upstream php-www {
  server unix:/dev/shm/php-www.socket;
}

server {
  listen *:80;
  server_name  ~^(?<subdomain>.+)(?<project>[^.]+).demo.com;
  root /var/www/$project/htdocs/;

  index index.php;

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

  location ~ [^/]\.php(/|$) {
      include fastcgi_params;

      #Save path_info before it gets killed
      set $path_info $fastcgi_path_info;
      fastcgi_param PATH_INFO $path_info;
      fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;

      fastcgi_ignore_client_abort off;
      fastcgi_pass php-www;
      fastcgi_index index.php;

      fastcgi_split_path_info ^(.+?\.php)(/.*)$;

      try_files $fastcgi_script_name $uri /index.php?$query_string;
  }
}

使用静态文件而不是 php-location 似乎工作得很好。index.php 实际上只包含一个 phpinfo();

我收到的错误信息是:

[error] 7763#7763: *3 rewrite or internal redirection cycle while internally redirecting to "/index.php", client: 192.168.2.100, server: ~^(?<subdomain>.+)(?<project>[^.]+).demo.com, request: "GET / HTTP/1.1", host: "www.foo.bar.demo.com"

有任何想法吗?

附加信息:

操作系统:Debian Jessie(原始 nginx 存储库) PHP 版本:PHP 5.6.29-0+deb8u1(cgi.fix_pathinfo=0) nginx 版本输出:

nginx version: nginx/1.10.2
built by gcc 4.9.2 (Debian 4.9.2-10)
built with OpenSSL 1.0.1t  3 May 2016
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-file-aio --with-threads --with-ipv6 --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_ssl_module --with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed'

答案1

错误消息是重定向循环/index.php,可能指向最后一条try_files语句。显然,在处理完 URI$fastcgi_script_name后才有效。请注意fastcgi_split_path_info官方的 nginx食谱避免使用try_files而改用if块。

我建议你尝试一下:

location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
        rewrite ^ /index.php last;
    }

    fastcgi_pass php-www;
    fastcgi_index index.php;

    include fastcgi_params;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;

    fastcgi_ignore_client_abort off;
}

当然,如果/index.php真的不存在,那么您仍然会得到一个重定向循环。

相关内容