Nginx反向綁定域名方法和詳細(xì)操作應(yīng)用實(shí)例:Google和Gravatar
Nginx 反向綁定域名方法和詳細(xì)操作應(yīng)用實(shí)例:Google 和Gravatar 反向綁定域名,即將域名B 綁定到域名A 上,用戶只要訪問B 就等同于進(jìn)入A ,內(nèi)容都是由A 提供,它有點(diǎn)像建立了一個(gè)A
Nginx 反向綁定域名方法和詳細(xì)操作應(yīng)用實(shí)例:
Google 和Gravatar 反向綁定域名,即將域名B 綁定到域名A 上,用戶只要訪問B 就等同于進(jìn)入A ,內(nèi)容都是由A 提供,它有點(diǎn)像建立了一個(gè)A 的鏡像。什么時(shí)候要用到反向綁定域名?服務(wù)器集群和網(wǎng)站負(fù)載均衡時(shí),把用戶訪問請(qǐng)求發(fā)送不同的服務(wù)器上。
關(guān)于反向綁定域名的方法部落之前也分享過好幾次,本篇文章就來詳細(xì)介紹一下Nginx 反向綁定域名方法。之所以要用Nginx ,主要在于Nginx 在反向綁定域名有著天然的優(yōu)勢(shì),并且功能強(qiáng)大,可以滿足我們更多更高的應(yīng)用需求。
平常我們都是用Nginx 反向綁定域名來搞定無法綁定域名的空間,這次來分享一下搞定Google 和Gravatar 兩個(gè)網(wǎng)站訪問的問題,更多的有關(guān)于反向綁定域名的方法還有:
1. 多種應(yīng)用:反向綁定域名的方法-強(qiáng)制綁定域名,實(shí)現(xiàn)負(fù)載均衡,域名內(nèi)網(wǎng)轉(zhuǎn)發(fā);
2. 簡(jiǎn)單操作:反向綁定域名方法-Nginx 反向配置和kangle 服務(wù)器反向設(shè)置;
3. 虛擬主機(jī):7ghost 基于PHP 的網(wǎng)站反向綁定域名程序無需.htaccess 的URL 重寫。 注意:上面提到的Nginx 反向綁定域名都需要用到VPS 主機(jī),如果你只有虛擬主機(jī),則可以試試7ghost 。
Nginx 反向綁定域名方法和詳細(xì)操作應(yīng)用實(shí)例:Google和Gravatar
一、Nginx 安裝和基本操作命令
1、Nginx 可以直接使用LNMP 這樣的一鍵安裝包,例如:LNMP 新版VPS 主機(jī)控制面板安裝。
2、如果你是用一個(gè)專門的服務(wù)器來作反向綁定域名用,則只需要安裝一個(gè)Nginx 即可,為VPS 主要省點(diǎn)資源。命令:
?
?
? 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、如果在執(zhí)行以上命令遇到./configure: error: the HTTP rewrite module requires the PCRE library.錯(cuò)誤提示,運(yùn)行:yum -y install pcre-developensslopenssl-devel
4、執(zhí)行以下命令,把ngx_http_substitutions_filter_module模塊編譯進(jìn)去,主要為了反向綁定域名過濾到頁面的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 用戶,啟動(dòng)Nginx 服務(wù)。
?
?
? /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 這個(gè)目錄下nginx.conf 。
8、修改了
nginx.conf 文件后,記得先檢測(cè)一下語法是否正常:
/usr/local/nginx/sbin/nginx -t,防止重啟Nginx 后服務(wù)器不正常。
9、Nginx 重啟命令:/usr/local/nginx/sbin/nginx -s reload 或者 kill -HUP `cat /usr/local/nginx/logs/nginx.pid` 或者 service nginxresatrt
10、上面介紹的兩種安裝方法適合有一定VPS 經(jīng)驗(yàn)的朋友,這里還有一個(gè)適合新手朋友的安裝命令:
?
? 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 證書現(xiàn)在購買也不是很貴,參考:Namecheap SSL 證書購買和SSL 激活安裝使用方法和新GodaddyCpanel 主機(jī)安裝Godaddy SSL證書方法。
2、Nginx 使用SSL 進(jìn)行反向綁定域名,修改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、代碼對(duì)www 和非www 請(qǐng)求都統(tǒng)一到了Https 的非www 請(qǐng)求上了。ssl on 是開啟SSL ,ssl_certificate 和ssl_certificate_key 是連接CRT 和Key 文件,你需要修改成你自己的路徑。
,4、不想購買付費(fèi)的SSL 證書的朋友,可以申請(qǐng)免費(fèi)的StartSSL 證書,已經(jīng)被90以上的瀏覽器所認(rèn)可并支持:StartSSL 免費(fèi)SSL 證書成功申請(qǐng)-HTTPS 讓訪問網(wǎng)站更安全
四、Nginx 反向綁定域名:subs_filter優(yōu)化請(qǐng)求和解決Google 驗(yàn)證碼問題
1、上面我們已經(jīng)將ngx_http_substitutions_filter_module模塊編譯進(jìn)入到了Nginx ,這個(gè)模塊主要是為了將網(wǎng)頁中的所請(qǐng)求都轉(zhuǎn)發(fā)自己的服務(wù)器。
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、單個(gè)IP 地址如果在短時(shí)間內(nèi)對(duì)Google 發(fā)送大量的IP 請(qǐng)求, 會(huì)被Google 判定為機(jī)器人,從而出現(xiàn)搜索驗(yàn)證碼的情況,為了解決這個(gè)問題,我們可以在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; } }