官方说法
List of HTTP status codes 301 Moved Permanently #This and all future requests should be directed to the given URI.[23] 302 Found #This is an example of industry practice contradicting the standard. The HTTP/1.0 specification (RFC 1945) required the client to perform a temporary redirect (the original describing phrase was "Moved Temporarily"),[24] but popular browsers implemented 302 with the functionality of a 303 See Other. Therefore, HTTP/1.1 added status codes 303 and 307 to distinguish between the two behaviours.[25] However, some Web applications and frameworks use the 302 status code as if it were the 303.
简单地说:
301和302都是web服务器响应HTTP协议请求状态的数字代码
301 代表永久性转移(Permanently Moved)
302 代表暂时性转移(Temporarily Moved)
两者之间的差别
1)对于用户
301和302没有区别,都是浏览器里面的URL跳转变成了一个新的URL地址
2)对于搜索引擎
存在网址劫持问题
302重定向和网址劫持(URL hijacking)有什么关系呢?这要从搜索引擎如何处理302转向说起。从定义来说,从网址A做一个302重定向到网址B时,主机服务器的隐含意思是网址A随时有可能改主意,重新显示本身的内容或转向其他的地方。大部分的搜索引擎在大部分情况下,当收到302重定向时,一般只要去抓取目标网址就可以了,也就是说网址B。
实际上如果搜索引擎在遇到302转向时,百分之百的都抓取目标网址B的话,就不用担心网址URL劫持了。问题就在于,有的时候搜索引擎,尤其是Google,并不能总是抓取目标网址。为什么呢?比如说,有的时候A网址很短,但是它做了一个302重定向到B网址,而B网址是一个很长的乱七八糟的URL网址,甚至还有可能包含一些问号之类的参数。很自然的,A网址更加用户友好,而B网址既难看,又不用户友好。这时Google很有可能会仍然显示网址A。
由于搜索引擎排名算法只是程序而不是人,在遇到302重定向的时候,并不能像人一样的去准确判定哪一个网址更适当,这就造成了网址URL劫持的可能性。也就是说,一个不道德的人在他自己的网址A做一个302重定向到你的网址B,出于某种原因, Google搜索结果所显示的仍然是网址A,但是所用的网页内容却是你的网址B上的内容,这种情况就叫做网址URL劫持。你辛辛苦苦所写的内容就这样被别人偷走了。
Nginx下配置301 302
1)利用server的rewrite功能对网址地址重写
vim www.conf server { listen 80; server_name lichengbing.com; location / { #rewrite ^/(.*) http://www.lichengbing.cn/$1 permanent;#301 永久跳转 rewrite ^/(.*) http://www.lichengbing.cn/$1 redirect;#302 临时跳转 } } server { listen 80; server_name www.lichengbing.com; location / { root html/www; index index.html index.htm; } access_log logs/access_www.log main; }
2)我们利用curl命令来模拟访问,获取http状态码
[root@db02 ~]# curl -I http://lichengbing.com HTTP/1.1 302 Moved Temporarily #302状态码 Server: nginx/1.6.3 Date: Fri, 05 Aug 2016 17:11:43 GMT Content-Type: text/html Content-Length: 160 Connection: keep-alive Location: http://www.lichengbing.cn/ #跳转地址
网站监控302问题
平时我们用脚本监控web服务器喜欢监控状态码,200、301、302表示正常,但是也有特殊情况,比如:
[root@db02 ~]# curl -I -s -o /dev/null -w "%{http_code}\n" http://lichengbingg.com 302 #lichengbingg.com不存在(302 FOUND) [root@db02 ~]# curl -I -s -o /dev/null -w "%{http_code}\n" http://taobao.com 302 #taobao.com 存在 (302 Moved)
所以,要么监控web服务器端口,要么就删掉302
#if [ "`nc -w 5 $url $Port &&echo ok`" = "ok" ];then #if [ "`echo -e ‘\n‘|telnet $url $Port|grep Connected|wc -l`" = "1" ];then #if [ "`nmap $url -p $Port|grep open|wc -l`" = "1" ];then #if [ "`curl -I http://$url 2>/dev/null|head -1|egrep "200|301"|wc -l`" = "1" ];then #if [ "`curl -I -s -o /dev/null -w ‘%{http_code}\n‘ http://$url`" = "200" ];then
Zabbix监控更省心。
原文地址:http://lilongzi.blog.51cto.com/5519072/1834923