如何解决NGINX try_files,名称为$ uri中的最后一个单词
我的服务器上有一个Nginx,我试图让它打开地址https://example.com/pp1的文件'/config/www/pp1/index.php
'和{{3}的文件'/config/www/interpreter/index.html
' }。此外,所有类似https://example.com/interpreter的事物都应触发'/config/www/interpreter/res/docs.html
'。我做了很多尝试。当前,我在/ site-confs中的默认配置文件如下所示:
server {
listen 80;
listen 443 ssl http2;
server_name kni.mini.pw.edu.pl;
# Path for SSL config/key/certificate
ssl_certificate /config/keys/cert.crt;
ssl_certificate_key /config/keys/cert.key;
location / {
proxy_read_timeout 90;
proxy_connect_timeout 90;
proxy_redirect off;
proxy_pass http://kni_website;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
location /pp1 {
root /config/www;
index index.html index.htm index.php;
try_files $uri $uri/ index.php $uri/index.php /config/www/pp1/index.php index.php; #/index.php?$args =404;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
location /interpreter {
if ($request_uri ~* "([^/]*$)" ) {
set $last_path_component $1;
}
root /config/www;
index index.html index.htm index.php $last_path_component.html;
try_files /interpreter/res/$last_path_component.html $uri.html $uri.html $uri $uri/ /index.html index.php /$uri.html /index.php?$args =404;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
}
server {
listen 80 default_server;
listen 443 ssl;
root /config/www;
index index.html index.htm index.php;
server_name _;
ssl_certificate /config/keys/cert.crt;
ssl_certificate_key /config/keys/cert.key;
client_max_body_size 0;
location / {
try_files $uri $uri/ /index.html /index.php?$args =404;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
坦率地说,我以为我知道自己在做什么,但是现在我可以肯定一点,location /interpreter
位置块根本没有被检查,location /pp1
的内部正在引起一些疯狂庞然大物。
请帮助有需要的新手!
解决方法
主要问题是try_files
将在当前上下文中处理其 file 元素,因此您无法在同一环境中处理.html
和.php
URI声明。有关详细信息,请参见this document。
一种解决方案是使用命名位置将try_files
语句拆分为两个。首先测试$uri
,$uri.html
和$uri/index.html
,然后第二测试$uri.php
和$uri/index.php
。
例如:
root /path/to/root;
location /foo {
try_files $uri $uri.html $uri/index.html @php;
location ~* ^(.*)\.php$ { return 301 $1; }
}
location @php {
try_files $uri.php $uri/index.php =404;
fastcgi_pass ...;
...
}
添加了location ~* ^(.*)\.php$
块以正确处理以.php
结尾的URI。最简单的解决方案是将其重定向到删除了.php
的URI。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。