当我尝试将 Web 应用程序的子目录中的索引文件访问到相同的 URL(但附加了斜杠)时,Nginx 出现了不良行为,它正在重新路由请求。
我有一个简单的 Web 应用程序,其中设置了一个根目录和其中的许多子目录,每个子目录中都有一个 index.php 文件。
服务器的操作系统是Ubuntu服务器,Nginx是服务器,安装了PHP5-fpm。
我想导航到http://foo/bar
获取index.php的输出bar
。但如果我导航到http://foo/bar
我总是被重定向到http://foo/bar/
.
我查看了Nginx访问登录/var/log/nginx/access.log
看来请求正在从我所请求的内容重定向(http://XXX.XXX.X.X/about
例如)到相同的 URL,并添加斜杠(http://XXX.XXX.X.X/about/
)通过 301。以下是日志中的代码示例:
192.168.1.2 - - [06/May/2015:14:53:20 -0500] "GET /why HTTP/1.1" 301 178 "http://192.168.1.7/services/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:37.0) Gecko/20100101 Firefox/37.0"
192.168.1.2 - - [06/May/2015:14:53:20 -0500] "GET /why/ HTTP/1.1" 200 3086 "http://192.168.1.7/services/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:37.0) Gecko/20100101 Firefox/37.0"
192.168.1.2 - - [06/May/2015:14:53:20 -0500] "GET /contact HTTP/1.1" 301 178 "http://192.168.1.7/why/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:37.0) Gecko/20100101 Firefox/37.0"
192.168.1.2 - - [06/May/2015:14:53:20 -0500] "GET /contact/ HTTP/1.1" 200 2325 "http://192.168.1.7/why/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:37.0) Gecko/20100101 Firefox/37.0"
但我不确定他们为什么被重定向或者是什么原因造成的。
这些是 Nginx 配置文件:
nginx.conf
user nginx;
worker_processes 1;
pid /run/nginx.pid;
events {
worker_connections 1024;
# multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
include /etc/nginx/sites-enabled/*;
}
站点启用默认值
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html/;
index index.php index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ =404;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html/;
}
location ^~ /files/ {
root /var/www/html/;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Nginx 为什么要这样做以及解决方案是什么?什么是最“干净”、“正确”或最符合惯例的解决方案?