【问题标题】:How do you fix Nginx automatically 301 redirecting to the same URL with a trailing slash?你如何修复 Nginx 自动 301 重定向到带有斜杠的相同 URL?
【发布时间】: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


    【解决方案1】:

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

    文件夹 about/ 匹配,因此 nginx 将查找与 index 指令中指定的任何名称匹配的文件。但是,using an index file causes an internal redirect (quoted from the nginx docs),这是您的尾随 / 的来源。

    对于它的价值,我在我的 nginx 安装上看到了完全相同的行为,所以它不是(只是)你的配置。

    【讨论】:

    • 太棒了!我明白;非常感谢您。那么获得我想要的效果的最“正确”或最干净的方法是什么?我是否应该在根目录(about.phpcontact.php 等)中创建包含我想要的每个页面的 PHP 文件,而不是将索引文件放在子目录中?或者有没有办法将 Nginx 配置为在 URL 中不包含斜杠?
    猜你喜欢
    • 2012-02-01
    • 2013-02-15
    • 2018-12-21
    • 2018-02-19
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    相关资源
    最近更新 更多