您如何解决Nginx自动301重定向到带有反斜杠的相同URL的问题? [英] How do you fix Nginx automatically 301 redirecting to the same URL with a trailing slash?

查看:722
本文介绍了您如何解决Nginx自动301重定向到带有反斜杠的相同URL的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将Web应用程序的子目录中的索引文件访问到相同的URL,但带有斜杠时,我从Nginx收到了不希望的行为,它将重新路由请求.

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.

我有一个简单的Web应用程序,该应用程序设置了一个根目录,并在其中包含许多子目录,每个子目录中都有一个index.php文件. 该服务器的操作系统是Ubuntu服务器,Nginx是该服务器,并且已安装PHP5-fpm.

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.

我想导航到http://foo/bar以获取bar中index.php的输出.但是,如果导航到http://foo/bar,我总是会重定向到http://foo/bar/.

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/.

我查看了/var/log/nginx/access.log中的Nginx访问日志,似乎请求已通过301从我请求的内容(例如,http://XXX.XXX.X.X/about)重定向到相同的URL,并带有一个斜杠(http://XXX.XXX.X.X/about/) .这是日志中的代码示例:

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"

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.

这些是Nginx配置文件:

These are the Nginx configuration files:

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会这样做,解决方案是什么?什么是最干净",正确"或最符合惯例的解决方案?

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?

推荐答案

这是try_files指令的结果.它正在查找名为about的文件,当找不到该文件时,它将继续查找名为about的文件夹. (该文件夹由$uri/上的尾随/指示.)

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/.)

文件夹about/已匹配,因此nginx将查找与index指令中指定的任何名称匹配的文件.但是,使用索引文件会导致内部重定向(引自nginx文档),这就是尾随/的来源.

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.

对于它的价值,我在nginx安装上看到了完全相同的行为,因此它(不是)您的配置.

For what it's worth, I see the exact same behavior on my nginx install, so it's not (just) your config.

这篇关于您如何解决Nginx自动301重定向到带有反斜杠的相同URL的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆