利用Nginx反向代理镜像HTTPS网站

Linux服务器环境配置: CentOS 7 x64 lnmp1.5,注意:lnmp.org的LNMP一键包在编译Nginx的是已经加上--with-http_sub_module扩展了,所以可以替换域名,而如果是别的一键安装包或者自己编译Nginx的话,则还需要加上这个扩展。倘若暂时不需要其他的组件(MySQL PHP等)那就先把他们关闭,只开启Nginx。

配置文件

以下说明中域名以 www.abc.com 为例,被镜像网站以 www.baidu.com 为例,然后访问你的域名看一看是否成功镜像,需要注意的一点是,如果被镜像的网站设置了防盗链,那么静态文件(js/css/图片)可能无法显示。

参数解释:一般情况下只需要更改这几个参数。

  • server_name 你的域名;
  • sub_filter 欲被镜像的域名 你的域名;
  • proxy_set_header Referer http://欲被镜像的域名;
  • proxy_set_header Host 欲被镜像的域名;
  • proxy_pass http://欲被镜像的域名;

HTTP 示例

以下示例是以www.abc.com镜像www.baidu.com为例。自行替换 其中的参数:
第二段是 屏蔽搜索引擎收录,比如镜像自己的网站,如果不屏蔽会导致 收录流失。注意:不管你是镜像 www.baidu.com 还是 www.google.com.hk (不要直接使用 .com 会被谷歌自动根据VPS所在地区重定向的),他们两个目前都是强制重定向到 https ,这意味着如果你只配置了 http 反向代理,那么访问反向代理域名后会重定向到 https 的目标域名,所以你也必须配置 https 才行。
下面这段代码是 HTTP 完整示例配置文件,注意使用时修改里面的默认域名等信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server
{
listen 80;
server_name www.abc.com;
if ($http_user_agent ~* (baiduspider|360spider|haosouspider|googlebot|soso|bing|sogou|yahoo|sohu-search|yodao|YoudaoBot|robozilla|msnbot|MJ12bot|NHN|Twiceler)) {
return 403;
}
location / {
sub_filter www.baidu.com www.abc.com;
sub_filter_once off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Referer http://www.baidu.com;
proxy_set_header Host www.baidu.com;
proxy_pass http://www.baidu.com;
proxy_set_header Accept-Encoding "";

HTTPS 示例

当你要镜像的网站不开放 HTTP或者强制HTTPS 的时候,你就需要加上 SSL 来转成 HTTPS 了。
假设SSL证书文件位置是:/root/ssl.crt
假设SSL密匙文件位置是:/root/ssl.key
第二段的 301 代码是,强制走HTTPS,如果不需要可以去掉。
第三段是 屏蔽搜索引擎收录,比如镜像自己的网站,如果不屏蔽会导致 收录流失。
同时下面这两个选项的记得把http://改成https://。

这段代码是 HTTP 完整示例配置文件,注意使用时修改里面的默认域名等信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
server
{
listen 80;
listen 443 ssl;
ssl on;
ssl_certificate /root/ssl.crt;
ssl_certificate_key /root/ssl.key;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
server_name www.abc.com;
add_header Strict-Transport-Security "max-age=31536000";
if ( $scheme = http ){
return 301 https://$server_name$request_uri;
}
if ($http_user_agent ~* (baiduspider|360spider|haosouspider|googlebot|soso|bing|sogou|yahoo|sohu-search|yodao|YoudaoBot|robozilla|msnbot|MJ12bot|NHN|Twiceler)) {
return 403;
}
location / {
sub_filter www.baidu.com www.abc.com;
sub_filter_once off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Referer https://www.baidu.com;
proxy_set_header Host www.baidu.com;
proxy_pass https://www.baidu.com;
proxy_set_header Accept-Encoding "";
}
}

------Example-------/usr/local/nginx/conf/vhost/blog.twili.live.conf------反向代理twili.me----

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
server
{
listen 80;
server_name blog.twili.live;

if ($http_user_agent ~* (baiduspider|360spider|haosouspider|googlebot|soso|bing|sogou|yahoo|sohu-search|yodao|YoudaoBot|robozilla|msnbot|MJ12bot|NHN|Twiceler)) {
return 403;
}

location / {
sub_filter twili.me blog.twili.live;
sub_filter_once off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Referer http://twili.me;
proxy_set_header Host twili.me;
proxy_pass http://twili.me;
proxy_set_header Accept-Encoding "";
}
}

server
{
listen 443 ssl;
ssl on;
ssl_certificate [Path];
ssl_certificate_key [Path];
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
server_name blog.twili.live;
add_header Strict-Transport-Security "max-age=31536000";

if ( $scheme = http ){
return 301 https://$server_name$request_uri;
}

if ($http_user_agent ~* (baiduspider|360spider|haosouspider|googlebot|soso|bing|sogou|yahoo|sohu-search|yodao|YoudaoBot|robozilla|msnbot|MJ12bot|NHN|Twiceler)) {
return 403;
}

location / {
sub_filter twili.me blog.twili.live;
sub_filter_once off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Referer https://twili.me;
proxy_set_header Host twili.me;
proxy_pass https://twili.me;
proxy_set_header Accept-Encoding "";
}
}

鸣谢:

1
2
3
https://doubibackup.com/l-en8vwt-3.html
https://www.cccitu.com/1590.html
https://www.maxoyed.com/archives/68/