我试图configurationnginx,所以它会加载没有扩展名的PHP文件,但仍然将缺less文件的请求传递给前端控制器。
我开始了一个大部分的工作configuration,如下所示,从webroot / about.php正确加载“about.php”之类的URL,并正确地将缺less文件的所有请求发送到webroot / index.php:
server { listen 80; listen 443 ssl http2; server_name *.example.com; root /srv/example/$host; client_max_body_size 8M; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; large_client_header_buffers 8 32k; ssl_certificate /etc/ssl/certs/example+ca.pem; ssl_certificate_key /etc/ssl/private/example.key; ssl_ciphers '...'; location / { try_files $uri /index.php$is_args$args; } location /blog { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-Proto $scheme; proxy_pass http://127.0.0.1:2368; } location ~ [^/]\.php(/|$) { fastcgi_split_path_info ^(.+\.php\b)(.*)$; fastcgi_param SERVER_NAME $host; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME /index.php; fastcgi_index index.php; include fastcgi.conf; fcgi_pass php-fpm; } }
我试图添加一个.php arg到try_files中,就像下面这样,我可以从/root/.php加载类似/ about的URL:
location / { try_files $uri $uri.php /index.php$is_args$args; }
现在,/about.php按预期载入,但是/关于下载文件。 我无法弄清楚如何强制内部redirect,同时保持前端控制器的全部function。 我见过几个人build议删除= 404,但这不是问题在这里。
错误日志(/ about): https : //pastebin.com/XbdjLri4
173.196.243.178 - - [16/May/2017:21:35:03 +0000] "GET /about HTTP/1.1" 200 6176 "-" "Wget/1.18 (linux-gnu)"
错误日志(/about.php):https://pastebin.com/ay32GZ0m
173.196.243.178 - - [16/May/2017:21:35:10 +0000] "GET /about.php HTTP/1.1" 200 25848 "-" "Wget/1.18 (linux-gnu)"
这里的问题是在try_files之后,请求URI nginx在这里看到的值仍然存在。 由于没有其他location ,它只是将文件发送到浏览器。
我认为这种做法是可行的:
location / { if (!-e $uri) { rewrite (.+)(?!\.php)$ $1.php last; } try_files $uri /index.php$is_args$args; } location ~ [^/]\.php(/|$) { ... fcgi_pass php-fpm; }
在这里,我们分别检查服务器上是否存在请求的文件。 如果没有,那么我们重写URL,如果它没有.php扩展名,并且添加PHP扩展。
然后我们执行try_files 。