Nginx反向绑定域名方法和详细操作应用实例:Google和Gravatar
Nginx 反向绑定域名方法和详细操作应用实例:Google 和Gravatar 反向绑定域名,即将域名B 绑定到域名A 上,用户只要访问B 就等同于进入A ,内容都是由A 提供,它有点像建立了一个A
Nginx 反向绑定域名方法和详细操作应用实例:
Google 和Gravatar 反向绑定域名,即将域名B 绑定到域名A 上,用户只要访问B 就等同于进入A ,内容都是由A 提供,它有点像建立了一个A 的镜像。什么时候要用到反向绑定域名?服务器集群和网站负载均衡时,把用户访问请求发送不同的服务器上。
关于反向绑定域名的方法部落之前也分享过好几次,本篇文章就来详细介绍一下Nginx 反向绑定域名方法。之所以要用Nginx ,主要在于Nginx 在反向绑定域名有着天然的优势,并且功能强大,可以满足我们更多更高的应用需求。
平常我们都是用Nginx 反向绑定域名来搞定无法绑定域名的空间,这次来分享一下搞定Google 和Gravatar 两个网站访问的问题,更多的有关于反向绑定域名的方法还有:
1. 多种应用:反向绑定域名的方法-强制绑定域名,实现负载均衡,域名内网转发;
2. 简单操作:反向绑定域名方法-Nginx 反向配置和kangle 服务器反向设置;
3. 虚拟主机:7ghost 基于PHP 的网站反向绑定域名程序无需.htaccess 的URL 重写。 注意:上面提到的Nginx 反向绑定域名都需要用到VPS 主机,如果你只有虚拟主机,则可以试试7ghost 。
Nginx 反向绑定域名方法和详细操作应用实例:Google和Gravatar
一、Nginx 安装和基本操作命令
1、Nginx 可以直接使用LNMP 这样的一键安装包,例如:LNMP 新版VPS 主机控制面板安装。
2、如果你是用一个专门的服务器来作反向绑定域名用,则只需要安装一个Nginx 即可,为VPS 主要省点资源。命令:
∙
∙
∙ wget http://sysoev.ru/nginx/nginx-0.7.64.tar.gz tar zxvf nginx-0.7.64.tar.gz cd nginx-0.7.64
,∙ ./configure --user=www --group=www --prefix=/usr/local/nginx
--with-http_stub_status_module --with-http_ssl_module --with-ipv6
∙ make && make install 3、如果在执行以上命令遇到./configure: error: the HTTP rewrite module requires the PCRE library.错误提示,运行:yum -y install pcre-developensslopenssl-devel
4、执行以下命令,把ngx_http_substitutions_filter_module模块编译进去,主要为了反向绑定域名过滤到页面的URL 地址。
∙
∙ git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module.git ./configure --prefix=/usr/local/nginx --with-http_stub_status_module
--with-http_ssl_module --with-http_gzip_static_module --with-ipv6
--add-module=/root/nginx-0.7.64/ngx_http_substitutions_filter_module
∙ make && make install
5、最后添加www 用户,启动Nginx 服务。
∙
∙
∙ /usr/sbin/groupadd -f www /usr/sbin/useradd -g www www /usr/local/nginx/sbin/nginx
6、或者,你也可以直接使用以下命令: ∙
∙ cd /tmp git clone git://github.com/yaoweibin/ngx_http_substitutions_filter_module.git #
nginx 的过滤器模块(比http_sub_module更加灵活)
∙
∙
∙ wget http://nginx.org/download/nginx-1.7.7.tar.gz tar -xzvf nginx-1.7.7.tar.gz cd /tmp/nginx-1.7.7
,∙
∙
∙
∙
∙
∙
∙
∙ ./configure --prefix=/www/wdlinux/nginx # 安装位置 --with-http_ssl_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --add-module=/tmp/ngx_http_substitutions_filter_module # 添加nginx 过滤器模块 make & make install 7、Nginx 的配置文件一般是在:/usr/local/nginx/conf 这个目录下nginx.conf 。
8、修改了

nginx.conf 文件后,记得先检测一下语法是否正常:
/usr/local/nginx/sbin/nginx -t,防止重启Nginx 后服务器不正常。
9、Nginx 重启命令:/usr/local/nginx/sbin/nginx -s reload 或者 kill -HUP `cat /usr/local/nginx/logs/nginx.pid` 或者 service nginxresatrt
10、上面介绍的两种安装方法适合有一定VPS 经验的朋友,这里还有一个适合新手朋友的安装命令:
∙
∙ yum -y install gccautomakeautoconflibtool make yum install gccgcc-c
,∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ cd /usr/local/src wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz tar -zxvf pcre-8.34.tar.gz cd pcre-8.34 ./configure make make install cd /usr/local/src wget http://zlib.net/zlib-1.2.8.tar.gz tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8 ./configure make make install cd /usr/local/src wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz tar -zxvf openssl-1.0.1c.tar.gz cd /usr/local/src wget http://nginx.org/download/nginx-1.4.2.tar.gz tar -zxvf nginx-1.4.2.tar.gz cd nginx-1.4.2 git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module.git ./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid
,∙
∙
∙
∙
∙
∙
∙
∙
∙
∙
∙ --with-http_ssl_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre=/usr/local/src/pcre-8.34 --with-zlib=/usr/local/src/zlib-1.2.8 --add-module=/usr/local/src/nginx-1.4.2/ngx_http_substitutions_filter_module --with-openssl=/usr/local/src/openssl-1.0.1c make make install /usr/local/nginx/nginx
二、Nginx 反向绑定域名:最基本的方法
1、下面是一段最基本的Nginx 反向绑定域名代码: ∙
∙
∙
∙
∙
∙
∙
∙
∙
∙
∙ server { listen 80; server_name freehao123.com; location / { proxy_pass http://www.google.com/; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
,
2、

你只需要修改server_name

和proxy_pass的值即可。保存nginx.conf ,重启Nginx ,打开你的域名,就可以看到反向的效果了。
3、如果想要反向绑定百度,直接把域名修改为百度的域名。
,4、然后打开自己的域名,就可以看到是百度了。

三、Nginx 反向绑定域名:带SSL 证书
1、为了能够保证自己反向绑定的“安全”,一般建议使用SSL 证书。SSL 证书现在购买也不是很贵,参考:Namecheap SSL 证书购买和SSL 激活安装使用方法和新GodaddyCpanel 主机安装Godaddy SSL证书方法。
2、Nginx 使用SSL 进行反向绑定域名,修改nginx.conf 如下:
∙
∙
∙
∙
∙
∙
∙
∙
∙
∙
∙
∙
∙ server { listen 80; server_name www.freehao123.com freehao123.com; location / { rewrite ^/(.*)$ https://freehao123.com$1 permanent; } } server { listen 443; server_name www.freehao123.com freehao123.com; if ($host = 'www.freehao123.com') {
,∙
∙
∙
∙
∙
∙
∙
∙
∙ ∙
∙
∙
∙
∙
∙
∙ rewrite ^/(.*)$ https://freehao123.com$1 permanent; } ssl on; ssl_certificate /root/myssl/myssl.crt; ssl_certificate_key /root/myssl/privkey.key; location / { proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header X-Forwarded-Proto https; proxy_pass https://www.google.com; proxy_set_header Host "www.google.com"; proxy_set_header Accept-Encoding ""; proxy_set_header User-Agent $http_user_agent; } }
3、代码对www 和非www 请求都统一到了Https 的非www 请求上了。ssl on 是开启SSL ,ssl_certificate 和ssl_certificate_key 是连接CRT 和Key 文件,你需要修改成你自己的路径。
,4、不想购买付费的SSL 证书的朋友,可以申请免费的StartSSL 证书,已经被90以上的浏览器所认可并支持:StartSSL 免费SSL 证书成功申请-HTTPS 让访问网站更安全

四、Nginx 反向绑定域名:subs_filter优化请求和解决Google 验证码问题
1、上面我们已经将ngx_http_substitutions_filter_module模块编译进入到了Nginx ,这个模块主要是为了将网页中的所请求都转发自己的服务器。
2、在location 中加入以下代码,类似:
∙
∙
∙
∙
∙
∙
∙
∙
∙
∙ location / { proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto https; proxy_pass https://www.google.com; proxy_set_header Host "www.google.com"; proxy_set_header Accept-Encoding ""; proxy_set_header User-Agent $http_user_agent; subs_filter www.google.com freehao123.com;
,∙
∙
∙ subs_filter ssl.gstatic.com freehao123.com; subs_filter_types text/css text/xml text/javascript; }

3、单个IP 地址如果在短时间内对Google 发送大量的IP 请求, 会被Google 判定为机器人,从而出现搜索验证码的情况,为了解决这个问题,我们可以在Http 层加入以下代码,类似于:
∙
∙
∙
∙
∙
∙
∙
∙
∙
∙
∙
∙ upstream google { server 74.125.139.1:80 max_fails=3; server 74.125.139.2:80 max_fails=3; server 74.125.139.3:80 max_fails=3; server 74.125.139.4:80 max_fails=3; server 74.125.139.5:80 max_fails=3; } server { listen 80; server_name www.freehao123.com freehao123.com; location / { rewrite ^/(.*)$ https://freehao123.com$1 permanent; } }