NGINX try_files 名称作为 $uri 中的最后一个词
NGINX try_files with name as the last word in the $uri
我的服务器上有一个 nginx,我正试图让它打开文件“/config/www/pp1/index.php
”,地址 https://example.com/pp1 and '/config/www/interpreter/index.html
' for https://example.com/interpreter. Furthermore all things like https://example.com/interpreter/res/docs 应该启动“/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 ;
}
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 ; }
}
location @php {
try_files $uri.php $uri/index.php =404;
fastcgi_pass ...;
...
}
添加 location ~* ^(.*)\.php$
块以正确处理以 .php
结尾的 URI。最简单的解决方案是将它们重定向到移除了 .php
的 URI。
我的服务器上有一个 nginx,我正试图让它打开文件“/config/www/pp1/index.php
”,地址 https://example.com/pp1 and '/config/www/interpreter/index.html
' for https://example.com/interpreter. Furthermore all things like https://example.com/interpreter/res/docs 应该启动“/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 ;
}
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 ; }
}
location @php {
try_files $uri.php $uri/index.php =404;
fastcgi_pass ...;
...
}
添加 location ~* ^(.*)\.php$
块以正确处理以 .php
结尾的 URI。最简单的解决方案是将它们重定向到移除了 .php
的 URI。