I am getting an undesired behaviour from Nginx where it is rerouteing requests when I try to access index files within subdirectories of my web application to the same URL, but with an appended slash.
I have a simple web application set up with a root directory, and a number of subdirectories within it, each with a index.php file within them. The server's OS is Ubuntu server, Nginx is the server, and PHP5-fpm is installed.
I want to navigate to http://foo/bar to get the output of the index.php within bar. But if I navigate to http://foo/bar I am always redirected to http://foo/bar/.
I have looked at the Nginx access log in /var/log/nginx/access.log and it seems the requests are being redirected from what I am requesting (http://XXX.XXX.X.X/about for instance) to the same URL with an added slash (http://XXX.XXX.X.X/about/) via 301. Here is an example of the code from the log:
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"
But I am not sure why they are being redirected or what is causing it.
These are the Nginx configuration files:
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/*;
}
sites-enabled default
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;
}
}
Why is Nginx doing this and what is(are) the solution(s) to this? What is the most "clean", "correct", or the solution most true to convention?
This is a result of the try_files directive. It's looking for the file named about, and when it fails to find it, it proceeds to look for a folder named about. (The folder is indicated by the trailing / on $uri/.)
The folder about/ matched, so nginx will then look for a file matching any of the names specified in the index directive. However, using an index file causes an internal redirect (quoted from the nginx docs), which is where your trailing / comes from.
For what it's worth, I see the exact same behavior on my nginx install, so it's not (just) your config.
来源:https://stackoverflow.com/questions/30088763/how-do-you-fix-nginx-automatically-301-redirecting-to-the-same-url-with-a-traili