
什么是反向代理鏡像網(wǎng)站
小黑去飯店吃飯,小白是飯店的服務(wù)員,小紅是飯店的廚師。小黑點(diǎn)了一道酸辣土豆絲,想問什么時(shí)候能上菜。
直接訪問:
小黑直接去廚房問小紅什么時(shí)候能做好,小紅說十分鐘后。
反向代理:
因某種原因,小黑去不了廚房了,但是小白可以去廚房。
小黑問小白什么時(shí)候能做好,小白走進(jìn)廚房詢問小紅什么時(shí)候能做好。
小紅告訴小白大約十分鐘,小白走回小黑面前,告訴小黑需要十分鐘。
2.反向代理操作步驟
假設(shè)需要用域名 bbbbb.com 去反向代理鏡像網(wǎng)站 aaaaa.com
購買域名 bbbbb.com:https://wangejiba.com/184.html
購買 VPS (海外主機(jī)):https://wangejiba.com/145.html
bbbbb.comDNS 解析:https://wangejiba.com/128.html
Lnmp 搭建 wbe 環(huán)境:https://wangejiba.com/104.html
創(chuàng)建虛擬主機(jī)(不用上傳網(wǎng)站程序):https://wangejiba.com/79.html
3.反向代理鏡像網(wǎng)站
看到第一步你會(huì)發(fā)現(xiàn),上面的步驟和新建一個(gè) WordPress 網(wǎng)站操作步驟一樣,只是在最后一步創(chuàng)建虛擬主機(jī)后沒有上傳網(wǎng)站程序。
3.1 HTTP 鏡像
HTTP 鏡像適用于:添加虛擬主機(jī)時(shí)選擇不啟用 SSL 證書,并且被鏡像的域名也沒有啟用 SSL 證書
打開/usr/local/nginx/conf/vhost
找到 bbbbb.com.conf 下載到本地
打開 bbbbb.com.conf 刪除所有內(nèi)容,然后把以下內(nèi)容復(fù)制到 bbbbb.com.conf
server
{
listen 80;
server_name bbbbb.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 aaaaa.com bbbbb.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://aaaaa.com;
proxy_set_header Host aaaaa.com;
proxy_pass http://aaaaa.com;
proxy_set_header Accept-Encoding "";
}
}
然后把上述代碼中的 aaaaa.com 改為被鏡像的網(wǎng)站,
bbbbb.com 改為鏡像網(wǎng)站所用的域名
因?yàn)槲业木W(wǎng)站都不帶 WWW,所以示例也沒有 WWW,如果你的有 WWW,請(qǐng)自行就改吧
修改完畢保存,把需改好的 bbbbb.com.conf 上傳到服務(wù)器進(jìn)行覆蓋替換
最后執(zhí)行:/etc/init.d/nginx restart 重啟 nginx,使其生效。
3.2 HTTPS 鏡像
HTTPS 鏡像適用于:添加虛擬主機(jī)時(shí)選擇啟用 SSL 證書,并且被鏡像的域名也啟用了 SSL 證書
打開/usr/local/nginx/conf/vhost
找到 bbbbb.com.conf 下載到本地
打開 bbbbb.com.conf ,復(fù)制文中的如下證書內(nèi)容(因域名不同,部分內(nèi)容會(huì)有差別),保存到另一個(gè)文檔中:
ssl on;
ssl_certificate /etc/letsencrypt/live/bbbbb.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/bbbbb.com/privkey.pem;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+CHACHA20:EECDH+CHACH:RSA+3DES:!MD5";
ssl_session_cache builtin:1000 shared:SSL:10m;
# openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;
add_header Strict-Transport-Security "max-age=31536000";
然后清空 bbbbb.com.conf,把下方內(nèi)容全部復(fù)制到被清空的 bbbbb.com.conf
server
{
listen 80;
listen 443 ssl;
server_name bbbbb.com ;
index index.html index.htm index.php default.html default.htm default.php;
root bbbbb.com;
ssl on;
ssl_certificate /etc/letsencrypt/live/bbbbb.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/bbbbb.com/privkey.pem;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+CHACHA20:EECDH+CHACH:RSA+3DES:!MD5";
ssl_session_cache builtin:1000 shared:SSL:10m;
# openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;
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 aaaaa.com bbbbb.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://aaaaa.com;
proxy_set_header Host aaaaa.com;
proxy_pass https://aaaaa.com;
proxy_set_header Accept-Encoding "";
}
}

修改 ABC 三處的內(nèi)容
A 處,把 bbbbb.com 替換為你用來鏡像的域名
B 處,刪 B 處內(nèi)容,把剛才單獨(dú)保存的證書內(nèi)容復(fù)制到 B 處
C 處,把 bbbbb.com 替換為你用來鏡像的域名,把 aaaaa.com 替換為被鏡像的域名。
因?yàn)槲业木W(wǎng)站都不帶 WWW,所以示例也沒有 WWW,如果你的有 WWW,請(qǐng)自行就改吧,修改完畢保存,把需改好的 bbbbb.com.conf 上傳到服務(wù)器進(jìn)行覆蓋替換。
最后執(zhí)行:/etc/init.d/nginx restart 重啟 nginx,使其生效。
4.小的補(bǔ)充
反向代理的前提,是用來鏡像的服務(wù)器能訪問被鏡像的服務(wù)器。如果被鏡像的域名在國內(nèi)被墻了,那么你購買國內(nèi)的服務(wù)器去鏡像,肯定是失敗的,因?yàn)閲鴥?nèi)的服務(wù)器無法訪問被國內(nèi)墻掉的網(wǎng)站。