【发布时间】:2015-07-17 07:11:39
【问题描述】:
当我尝试将我的 Web 应用程序的子目录中的索引文件访问到相同的 URL 时,我从 Nginx 得到一个不希望的行为,它正在重新路由请求,但附加了斜杠。
我有一个简单的 Web 应用程序,它有一个根目录,以及其中的许多子目录,每个子目录中都有一个 index.php 文件。 服务器的OS是Ubuntu服务器,Nginx是服务器,安装了PHP5-fpm。
我想导航到http://foo/bar 以在bar 中获取index.php 的输出。但是如果我导航到http://foo/bar,我总是会被重定向到http://foo/bar/。
我查看了/var/log/nginx/access.log 中的 Nginx 访问日志,似乎请求正在从我请求的内容(例如http://XXX.XXX.X.X/about)重定向到相同的 URL,并通过 301 添加了斜杠(http://XXX.XXX.X.X/about/) . 以下是日志中的代码示例:
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 为什么要这样做,解决方案是什么?什么是最“干净”、“正确”或最符合惯例的解决方案?
【问题讨论】:
标签: php redirect nginx http-status-code-301