分享的乐趣

使用Nginx反向代理网站

Nginx是一个轻量级的高并发Web服务器,很多知名站点如网易云、凤凰网、携程网等都把nginx作为自己网站的Web服务器。Nginx用途很多可以作为HTTP服务器、反向代理网站、负载均衡等。

今天我们一起学习一下用Nginx反向代理Google,搭建一个镜像网站

搭建准备:
  • 一个安装Debian8系统的国外vps
  • ssh工具
步骤:

一、安装Nginx

Debian8系统:

更新源:

echo deb http://nginx.org/packages/debian/ jessie nginx >> /etc/apt/sources.list
echo deb-src http://nginx.org/packages/debian/ jessie nginx >> /etc/apt/sources.list

安装Nginx

wget http://nginx.org/keys/nginx_signing.key && apt-key add nginx_signing.key && apt-get update && apt-get install nginx

二、使用acme.sh配置ssl

curl -L get.acme.sh | bash -

签发 SSL 证书,签发前将你的域名解析至你 VPS 的 IP

service nginx stop && /root/.acme.sh/acme.sh --issue --standalone -d google.zhangxinyu.org

把google.zhangxinyu.org替换成你的域名;如果提示 Please install socat tools first 执行

apt-get install socat

签发成功后会提示

Sun Jan 14 02:53:57 UTC 2018] Your cert is in /root/.acme.sh/google.zhangxinyu.org/google.zhangxinyu.org.cer
[Sun Jan 14 02:53:57 UTC 2018] Your cert key is in /root/.acme.sh/google.zhangxinyu.org/google.zhangxinyu.org.key
[Sun Jan 14 02:53:57 UTC 2018] The intermediate CA cert is in /root/.acme.sh/google.zhangxinyu.org/ca.cer
[Sun Jan 14 02:53:57 UTC 2018] And the full chain certs is there: /root/.acme.sh/google.zhangxinyu.org/fullchain.cer

其中

/root/.acme.sh/google.zhangxinyu.org/google.zhangxinyu.org.key 是你的密钥 
/root/.acme.sh/google.zhangxinyu.org/fullchain.cer 是你的域名证书,
这两个是我们需要的

配置Nginx文件

vim /etc/nginx/conf.d/google.conf

复制粘贴下面文件;把 google.zhangxinyu.org换成你的域名

server
 {
 listen 80;
 listen 443 ssl;
 ssl on;
 ssl_certificate /root/.acme.sh/google.zhangxinyu.org/fullchain.cer;
 ssl_certificate_key /root/.acme.sh/google.zhangxinyu.org/google.zhangxinyu.org.key;
 ssl_session_cache shared:SSL:10m;
 ssl_session_timeout 10m;
 server_name google.zhangxinyu.org;
 add_header Strict-Transport-Security "max-age=31536000";

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

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.google.com.ph google.zhangxinyu.org;
 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.google.com.ph;
 proxy_set_header Host www.google.com.ph;
 proxy_pass https://www.google.com.ph;
 proxy_set_header Accept-Encoding "";
 }
 }

ESC  输入 :wq  回车 保存并退出

重启Nginx

service nginx start

访问 https://google.zhangxinyu.org

就能不用梯子访问google了。

如果你想反代其他网站只要把上面Nginx配置中的 google.com.ph 替换一下即可。

后言:

理论上这样说能够访问google的,但是用这种方法有一定的风险被GFW识别,域名有被墙的风险,如果你想稳定运行google镜像网站,你可以参考GitHub上Nginx Google扩展项目:

https://github.com/cuber/ngx_http_google_filter_module/blob/master/README.zh-CN.md

赞(3) 打赏
未经允许不得转载:张新宇 » 使用Nginx反向代理网站
分享到: 更多 (0)

评论 抢沙发

  • Q Q(选填)
  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用可以打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏