我正在嘗試完成一個 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
確實不存在,那麼你仍然會得到一個重定向循環。