阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

HTTP返回状态码中301和302的区别

177次阅读
没有评论

共计 2561 个字符,预计需要花费 7 分钟才能阅读完成。

官方说法

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) 对于搜索引擎
存在网址劫持问题

HTTP 返回状态码中 301 和 302 的区别

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  linuxidc.com;
        location / {
      #rewrite ^/(.*) http://www.linuxidc.net/$1 permanent;#301 永久跳转
        rewrite ^/(.*) http://www.linuxidc.net/$1 redirect;#302 临时跳转
        }
    }
    server {
        listen      80;
        server_name  www.linuxidc.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://linuxidc.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.linuxidc.net/ #跳转地址

网站监控 302 问题
平时我们用脚本监控 web 服务器喜欢监控状态码,200、301、302 表示正常,但是也有特殊情况,比如:
[root@db02 ~]# curl -I -s -o /dev/null -w “%{http_code}\n” http://linuxidc.com
302 #linuxidc.com 不存在(302 FOUND)
[root@db02 ~]# curl -I -s -o /dev/null -w “%{http_code}\n” http://muu.cc
302 #muu.cc 存在(302 Moved)

所以,要么监控 web 服务器端口,要么就删掉 302

12345 #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://www.linuxidc.com/Linux/2016-08/134023.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计2561字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中