0

I'm trying to accomplish a nginx Configuration wich allows me based on part of the URL/ServerName to choose the project folder (document_root), all dynamic.

So if i got a Web-Server directory like:

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

And i got the following URL matches:

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/

and so on.

If tried it with the following configuration and it works with static files but not with PHP. As far as i can see and tell the path_info is always empty.

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;
  }
}

With static files and not php-location it seems working great. the index.php actually only contains only a phpinfo();

The error message i'm getting is:

[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"

Any Ideas?

Addition Information:

OS: Debian Jessie (original nginx repository) PHP Version: PHP 5.6.29-0+deb8u1 (cgi.fix_pathinfo=0) nginx Version output:

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 Answers1

0

The error message is a redirection loop on /index.php which probably points to the last try_files statement. Obviously $fastcgi_script_name is invalid until after fastcgi_split_path_info has processed the URI. Notice that the official nginx recipe avoids using try_files and uses an if block instead.

I would suggest that you try:

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;
}

Of course, if /index.php really does not exist, then you will still get a redirection loop.

Richard Smith
  • 12,834
  • 2
  • 21
  • 29