Linux 是什么导致了这个 301 重定向?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9149124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is causing this 301 redirect?
提问by Myles Gray
I have a problem with my server redirecting http://www.mylesgray.com:8080/
-> http://www.mylesgray.com/
.
我的服务器重定向有问题http://www.mylesgray.com:8080/
-> http://www.mylesgray.com/
。
Here are my Nginx default
and fastcgi_params
config files:
这是我的 Nginxdefault
和fastcgi_params
配置文件:
https://gist.github.com/1745271
https://gist.github.com/1745271
https://gist.github.com/1745313
https://gist.github.com/1745313
This is rather a nusance as I am trying to run a benchmark of Nginx w/ cachingvs Varnish w/ caching on top of Nginxto see if there is any performance benefit of one over the other.
这是相当因为我试图运行的基准测试中nusance Nginx的W /缓存VS光油瓦特/ Nginx的顶部缓存,看看是否有一个比其他任何性能优势。
As such I have straight Nginx w/ caching listening on port 8080
and varnish on port 80
which forwards any non-cached requests to Nginx on localhost:8080
, so obviously what I want to do is run an ab
benchmark on http://www.mylesgray.com:8080/
and on http://www.mylesgray.com/
to see the difference.
因此我有口直Nginx的W /缓存监听8080
和清漆端口80
转发任何非缓存请求的Nginx上localhost:8080
,所以很明显我想要做的是运行ab
的基准测试http://www.mylesgray.com:8080/
和http://www.mylesgray.com/
看到的差异。
Here are the results of curl -I
on various addresses.
以下是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/
Then running curl -I http://www.mylesgray.com
gives:
然后运行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
So as you can see 80
is served by Varnish and 8080
by Nginx but I cannot find anywhereanything that does a 301 redirect, not in nginx.conf
or in the sites-enabled/default
file and I don't believe it is caused by Wordpress itself but an very much open to correction.
因此,如您所见80
,由 Varnish 和8080
Nginx 提供服务,但我找不到任何进行 301 重定向的任何内容,不在文件中nginx.conf
或在sites-enabled/default
文件中,我不相信这是由 Wordpress 本身引起的,但很容易纠正。
Please help, this is driving me nuts!
请帮忙,这让我发疯!
Myles
迈尔斯
采纳答案by kolbyHyman
The presence of an X-Powered-By: PHP header means that wordpress is issuing the 301. It's due to wordpress forcing www.mylesgray.com. When you use a nonstandard port, user agents will generally include the port in the Host: header. Try adding
X-Powered-By: PHP 标头的存在意味着 wordpress 正在发布 301。这是由于 wordpress 强制 www.mylesgray.com。当您使用非标准端口时,用户代理通常会将端口包含在 Host: 标头中。尝试添加
fastcgi_param HTTP_HOST $host;
with the rest of your fastcgi_param directives (or alog with your "include fastcgi_params;") and it should fix this.
与您的 fastcgi_param 指令的其余部分(或与您的“包含 fastcgi_params;”类似),它应该解决这个问题。
回答by Jér?me R
You should add a '/' at then end of your URLs. Furthermore if you run ab http://foo.comit will return you a "ab: invalid URL" error. If you do "ab -t 10 http://example.com/" everything will work fine. You should always use '/' in your URLs otherwize your webserver will try to redirect the page to the home page automatically for you which generates an undesirable extra load on the server and some extra bytes on the wire.
您应该在 URL 的末尾添加一个“/”。此外,如果您运行 ab http://foo.com,它将返回“ab:无效 URL”错误。如果您执行“ab -t 10 http://example.com/”,则一切正常。您应该始终在您的 URL 中使用“/”,否则您的网络服务器将尝试为您自动将页面重定向到主页,这会在服务器上产生不必要的额外负载并在线路上产生一些额外的字节。
You web server told you what it did:
你的网络服务器告诉你它做了什么:
'/' is missing and something is incorrect with the port numer:
'/' 丢失,端口号有问题:
# curl -I http://www.mylesgray.com:8080
HTTP/1.1 301 Moved Permanently
[...]
======> Location: http://www.mylesgray.com/
'www' and '/' are missing:
'www' 和 '/' 丢失:
# curl -I http://mylesgray.com
HTTP/1.1 301 Moved Permanently
[...]
=======> Location: http://www.mylesgray.com/
[...]
'/' and 'www' are missing:
'/' 和 'www' 丢失:
# curl -I http://mylesgray.com:8080
HTTP/1.1 301 Moved Permanently
[...]
========> Location: http://www.mylesgray.com/
'hope that helps :)
'希望有帮助:)