如何设置Nginx实现百度蜘蛛的301重定向跳转?
- 内容介绍
- 文章标签
- 相关推荐
本文共计661个文字,预计阅读时间需要3分钟。
先简单说下我遇到的状况。我有一个域名的http网页被墙,国内用户可以通过https方式打开。为了SEO和遵守规则,尽量不更换网站域名的前缀。现在我的目标是实现让国外蜘蛛(如Google)也能访问。

先简单说下我碰到的情况。我一个域名的www.hellonet8.com$request_uri;
}
#排除对一个网址的301重定向
if ($request_uri !~ ^/.*/.*notify_url\.php.*){
return 301 $server_name$request_uri;
}
location ~ .*\.(php|php5)?$
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
access_log /home/wwwlogs/www.nbhao.org.log www.nbhao.org;
}
server {
listen 443 ;
......
#www.sijitao.net/1835.html
}

2、修改新域名nginx配置成如下
server
{
listen 80;
#listen [::]:80;
server_name www.hellonet8.com;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/hellonet8.com/www;
#如果用户不是百度蜘蛛且访问的网址不是baidu.*\.html格式
set $flag 0;
if ($www.nbhao.org$request_uri;
}
#接上不,如果是百度蜘蛛,执行proxy_pass反代。
location ^~ / {
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
if ($request_uri !~ ^.*/baidu.*\.html.*){
proxy_pass www.nbhao.org;
}
}
......
access_log /home/wwwlogs/www.hellonet8.com.log access;
}
3、通过curl模拟百度蜘蛛、google、普通用户验证
[stocky@s1 ~]$ curl -I -A Baiduspider www.nbhao.org
HTTP/1.1 301 Moved Permanently
Server: nginx/1.0.15
Date: Wed, 04 Mar 2015 08:59:27 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: www.hellonet8.com/
[stocky@s1 ~]$ curl -I -A Baiduspider www.hellonet8.com
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 04 Mar 2015 09:01:29 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Vary: Accept-Encoding
Vary: Accept-Encoding
X-Powered-By: PHP/5.2.17p1
[stocky@s1 ~]$ curl -I -A google www.nbhao.org
HTTP/1.1 301 Moved Permanently
Server: nginx/1.0.15
Date: Wed, 04 Mar 2015 09:00:08 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: www.nbhao.org/
[stocky@s1 ~]$ curl -I -A google www.hellonet8.com
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 04 Mar 2015 09:02:07 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: www.nbhao.org/
[stocky@s1 ~]$ curl -I -A Mozilla www.nbhao.org
HTTP/1.1 301 Moved Permanently
Server: nginx/1.0.15
Date: Wed, 04 Mar 2015 09:01:59 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: www.nbhao.org/
[stocky@s1 ~]$ curl -I -A Mozilla www.hellonet8.com
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 04 Mar 2015 09:04:00 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: www.nbhao.org/
测试后发现满足要求。
本文共计661个文字,预计阅读时间需要3分钟。
先简单说下我遇到的状况。我有一个域名的http网页被墙,国内用户可以通过https方式打开。为了SEO和遵守规则,尽量不更换网站域名的前缀。现在我的目标是实现让国外蜘蛛(如Google)也能访问。

先简单说下我碰到的情况。我一个域名的www.hellonet8.com$request_uri;
}
#排除对一个网址的301重定向
if ($request_uri !~ ^/.*/.*notify_url\.php.*){
return 301 $server_name$request_uri;
}
location ~ .*\.(php|php5)?$
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
access_log /home/wwwlogs/www.nbhao.org.log www.nbhao.org;
}
server {
listen 443 ;
......
#www.sijitao.net/1835.html
}

2、修改新域名nginx配置成如下
server
{
listen 80;
#listen [::]:80;
server_name www.hellonet8.com;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/hellonet8.com/www;
#如果用户不是百度蜘蛛且访问的网址不是baidu.*\.html格式
set $flag 0;
if ($www.nbhao.org$request_uri;
}
#接上不,如果是百度蜘蛛,执行proxy_pass反代。
location ^~ / {
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
if ($request_uri !~ ^.*/baidu.*\.html.*){
proxy_pass www.nbhao.org;
}
}
......
access_log /home/wwwlogs/www.hellonet8.com.log access;
}
3、通过curl模拟百度蜘蛛、google、普通用户验证
[stocky@s1 ~]$ curl -I -A Baiduspider www.nbhao.org
HTTP/1.1 301 Moved Permanently
Server: nginx/1.0.15
Date: Wed, 04 Mar 2015 08:59:27 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: www.hellonet8.com/
[stocky@s1 ~]$ curl -I -A Baiduspider www.hellonet8.com
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 04 Mar 2015 09:01:29 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Vary: Accept-Encoding
Vary: Accept-Encoding
X-Powered-By: PHP/5.2.17p1
[stocky@s1 ~]$ curl -I -A google www.nbhao.org
HTTP/1.1 301 Moved Permanently
Server: nginx/1.0.15
Date: Wed, 04 Mar 2015 09:00:08 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: www.nbhao.org/
[stocky@s1 ~]$ curl -I -A google www.hellonet8.com
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 04 Mar 2015 09:02:07 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: www.nbhao.org/
[stocky@s1 ~]$ curl -I -A Mozilla www.nbhao.org
HTTP/1.1 301 Moved Permanently
Server: nginx/1.0.15
Date: Wed, 04 Mar 2015 09:01:59 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: www.nbhao.org/
[stocky@s1 ~]$ curl -I -A Mozilla www.hellonet8.com
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 04 Mar 2015 09:04:00 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: www.nbhao.org/
测试后发现满足要求。
