【问题标题】:What is causing this 301 redirect? [closed]是什么导致了这个 301 重定向? [关闭]
【发布时间】:2012-02-27 06:42:20
【问题描述】:

我的服务器重定向 http://www.mylesgray.com:8080/ -> http://www.mylesgray.com/ 时遇到问题。

这是我的 Nginx defaultfastcgi_params 配置文件:

https://gist.github.com/1745271

https://gist.github.com/1745313

这很麻烦,因为我正在尝试运行 Nginx w/cachingVarnish w/caching on Nginx 的基准测试,看看是否有任何一种优于另一种的性能优势。

因此,我在端口8080 上有直接的 Nginx 带缓存侦听和在端口 80 上的清漆,它将任何非缓存请求转发到 localhost:8080 上的 Nginx,所以显然我想要做的是运行一个 @在http://www.mylesgray.com:8080/http://www.mylesgray.com/ 上进行987654330@ 基准测试以查看差异。

这是curl -I 在各个地址上的结果。

# curl -I http://www.mylesgray.com:8080

HTTP/1.1 301 Moved Permanently
Server: nginx/0.7.65
Date: Sun, 05 Feb 2012 12:07:34 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.3.2-1ubuntu4.7ppa5~lucid1
X-Pingback: http://www.mylesgray.com/xmlrpc.php
Location: http://www.mylesgray.com/

# curl -I http://mylesgray.com

HTTP/1.1 301 Moved Permanently
Server: nginx/0.7.65
Content-Type: text/html; charset=UTF-8
X-Powered-By: PHP/5.3.2-1ubuntu4.7ppa5~lucid1
X-Pingback: http://www.mylesgray.com/xmlrpc.php
Location: http://www.mylesgray.com/
Content-Length: 0
Date: Sun, 05 Feb 2012 12:15:51 GMT
X-Varnish: 1419774165 1419774163
Age: 15
Via: 1.1 varnish
Connection: keep-alive

# curl -I http://mylesgray.com:8080

HTTP/1.1 301 Moved Permanently
Server: nginx/0.7.65
Date: Sun, 05 Feb 2012 12:16:08 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.3.2-1ubuntu4.7ppa5~lucid1
X-Pingback: http://www.mylesgray.com/xmlrpc.php
Location: http://www.mylesgray.com/

然后运行curl -I http://www.mylesgray.com 给出:

# curl -I http://www.mylesgray.com

HTTP/1.1 200 OK
Server: nginx/0.7.65
Content-Type: text/html; charset=UTF-8
X-Powered-By: PHP/5.3.2-1ubuntu4.7ppa5~lucid1
X-Pingback: http://www.mylesgray.com/xmlrpc.php
Content-Length: 5132
Date: Sun, 05 Feb 2012 12:07:29 GMT
X-Varnish: 1419774133 1419774124
Age: 30
Via: 1.1 varnish
Connection: keep-alive

如您所见,80 由 Varnish 提供服务,8080 由 Nginx 提供服务,但我在 anywhere 找不到任何执行 301 重定向的内容,而不是在 nginx.conf 或 @987654340 中@ 文件,我不相信它是由 Wordpress 本身引起的,但非常容易纠正。

请帮忙,这让我发疯了!

迈尔斯

【问题讨论】:

    标签: linux redirect nginx http-status-code-301 varnish


    【解决方案1】:

    您应该在 URL 的末尾添加一个“/”。此外,如果您运行 ab http://foo.com,它将返回“ab: invalid URL”错误。如果你执行“ab -t 10 http://example.com/”,一切都会正常。您应该始终在您的 URL 中使用“/”,否则您的网络服务器会尝试自动将页面重定向到主页,这会在服务器上产生不必要的额外负载,并在网络上产生一些额外的字节。

    你的网络服务器告诉你它做了什么:

    '/' 丢失,端口号有问题

    # curl -I http://www.mylesgray.com:8080
    HTTP/1.1 301 Moved Permanently
    [...]
    ======> Location: http://www.mylesgray.com/
    

    缺少“www”和“/”

    # curl -I http://mylesgray.com
    HTTP/1.1 301 Moved Permanently
    [...]
    =======> Location: http://www.mylesgray.com/
    [...]
    

    '/' 和 'www' 不见了

    # curl -I http://mylesgray.com:8080
    HTTP/1.1 301 Moved Permanently
    [...]
    ========> Location: http://www.mylesgray.com/
    

    '希望有帮助:)

    【讨论】:

      【解决方案2】:

      X-Powered-By: PHP 标头的存在意味着 wordpress 正在发出 301。这是由于 wordpress 强制 www.mylesgray.com 造成的。当您使用非标准端口时,用户代理通常会在 Host: 标头中包含该端口。尝试添加

      fastcgi_param HTTP_HOST $host;
      

      与您的其余 fastcgi_param 指令(或与您的“包含 fastcgi_params;”一起),它应该可以解决这个问题。

      【讨论】:

      • 我已将此添加到我的 /etc/nginx/fastcgi_params 并重新启动所有服务,但 curl 仍显示除 http://www.mylesgray.com 之外的所有服务的 301。
      • 这是我的default 站点配置文件和我的fastcgi_params 文件:gist.github.com/1745271gist.github.com/1745313
      • 你运行的是哪个版本的 nginx?似乎直到 0.8.40 才添加使用 fastcgi_param 覆盖请求标头。
      • 啊......可能是这样 - 我正在运行 10.04 附带的任何内容,我将使用 1.0.11 重新安装,看看情况如何......
      • 你就是男人!非常棒 - 现在完美运行!
      猜你喜欢
      • 2019-07-26
      • 2014-08-12
      • 2016-02-02
      • 2011-05-14
      • 1970-01-01
      • 2011-09-29
      • 2019-07-01
      • 2011-02-12
      • 2011-11-27
      相关资源
      最近更新 更多