VPS中利用nginx做反向代理访问维基中文的方法


根据V2EX访问分享的配置文件,原文链接点我,并且稍做改变,解决了手机访问时跳回原网站移动见面的问题,并强制使用SSL加密访问,感谢原作者lingbaoboy,以下是配置文件内容:

server {
listen 80;
server_name wiki.zhtube.com;
#强制跳转到HTTPS
return 301 https://wiki.zhtube.com;
}
server
{
listen 443;
listen [::]:443;
server_name wiki.zhtube.com;
ssl on;
ssl_certificate www.zhtube.com.crt;
ssl_certificate_key www.zhtube.com.key;

location / {
#禁止搜索引擎
if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot")
{
return 403;
}

proxy_set_header Host 'zh.wikipedia.org';
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#强制定义浏览器UA,我定义的是WIN10,遨游浏览器,需要的可以根据自己的需要定义
proxy_hide_header User-Agent;
proxy_set_header User-Agent "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.8.1000 Chrome/30.0.1599.101 Safari/537.36";
proxy_pass https://zh.wikipedia.org/;
proxy_set_header Accept-Encoding "";
sub_filter https://zh.wikipedia.org/ https://wiki.zhtube.com/;
sub_filter_once off;
}
}

其中需要注意的是我用的是军哥的LNMP,SUB模块可能不会自动安装,需要自己添加
# 下载模块
git clone git://github.com/yaoweibin/ngx_http_substitutions_filter_module.git
# 查看原来的CONFIGURE
nginx -V
# 进入NGINX的安装目录,复制原来的配置参数,在最后添加上需要添加的模块路径
./configure --user=www --group=www --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module --with-http_spdy_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --add-module=/root/ngx_http_substitutions_filter_module
# 然后make
make
# 停止NGINX服务
service nginx stop
# 进入objs目录
cd objs
# 覆盖文件
cp nginx /usr/local/nginx/sbin/
# 启动nginx
service nginx start


《“VPS中利用nginx做反向代理访问维基中文的方法”》 有 3 条评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注