Nginx反向綁定域名方法和詳細操做應用實例:Google和Gravatar

反向綁定域名,即將域名B綁定到域名A上,用戶只要訪問B就等同於進入A,內容都是由A提供,它有點像創建了一個A的鏡像。何時要用到反向綁定域名?服務器集羣和網站負載均衡時,把用戶訪問請求發送不一樣的服務器上。javascript

關於反向綁定域名的方法部落以前也分享過好幾回,本篇文章就來詳細介紹一下Nginx反向綁定域名方法。之因此要用Nginx,主要在於Nginx在反向綁定域名有着自然的優點,而且功能強大,能夠知足咱們更多更高的應用需求。css

日常咱們都是用Nginx反向綁定域名來搞定沒法綁定域名的空間,此次來分享一下搞定Google和Gravatar兩個網站訪問的問題,更多的有關於反向綁定域名的方法還有:html

注意:上面提到的Nginx反向綁定域名都須要用到VPS主機,若是你只有虛擬主機,則能夠試試7ghost。c++

Nginx反向綁定域名方法和詳細操做應用實例:Google和Gravatargit

1、Nginx安裝和基本操做命令github

一、Nginx能夠直接使用LNMP這樣的一鍵安裝包,例如:LNMP新版VPS主機控制面板安裝瀏覽器

二、若是你是用一個專門的服務器來做反向綁定域名用,則只須要安裝一個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

三、若是在執行以上命令遇到./configure: error: the HTTP rewrite module requires the PCRE library.錯誤提示,運行:yum -y install pcre-devel openssl openssl-devel

四、執行如下命令,把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

五、最後添加www用戶,啓動Nginx服務。

/usr/sbin/groupadd -f www
/usr/sbin/useradd -g www www
/usr/local/nginx/sbin/nginx

六、或者,你也能夠直接使用如下命令:

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

七、Nginx的配置文件通常是在:/usr/local/nginx/conf 這個目錄下nginx.conf。

八、修改了nginx.conf文件後,記得先檢測一下語法是否正常:/usr/local/nginx/sbin/nginx -t,防止重啓Nginx後服務器不正常。

Nginx檢查語法是否正確

九、Nginx重啓命令:/usr/local/nginx/sbin/nginx -s reload 或者 kill -HUP `cat /usr/local/nginx/logs/nginx.pid` 或者 service nginx resatrt

十、上面介紹的兩種安裝方法適合有必定VPS經驗的朋友,這裏還有一個適合新手朋友的安裝命令:

yum -y install gcc automake autoconf libtool make
yum install gcc gcc-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

2、Nginx反向綁定域名:最基本的方法

一、下面是一段最基本的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;
    }
}

Nginx最基本的方法

二、你只須要修改server_name和proxy_pass的值便可。保存nginx.conf,重啓Nginx,打開你的域名,就能夠看到反向的效果了。

Nginx看到效果

三、若是想要反向綁定百度,直接把域名修改成百度的域名。

Nginx更換爲了百度的域名

四、而後打開本身的域名,就能夠看到是百度了。

Nginx綁定百度

3、Nginx反向綁定域名:帶SSL證書

一、爲了可以保證本身反向綁定的「安全」,通常建議使用SSL證書。SSL證書如今購買也不是很貴,參考:Namecheap SSL證書購買和SSL激活安裝使用方法新Godaddy Cpanel主機安裝Godaddy SSL證書方法

二、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;     
        }
    }

三、代碼對www和非www請求都統一到了Https的非www請求上了。ssl on是開啓SSL,ssl_certificate 和ssl_certificate_key 是鏈接CRT和Key文件,你須要修改爲你本身的路徑。

Nginx使用SSL證書

四、不想購買付費的SSL證書的朋友,能夠申請免費的StartSSL證書,已經被90%以上的瀏覽器所承認並支持:StartSSL免費SSL證書成功申請-HTTPS讓訪問網站更安全

Nginx使用StartSSL

4、Nginx反向綁定域名:subs_filter優化請求和解決Google驗證碼問題

一、上面咱們已經將ngx_http_substitutions_filter_module模塊編譯進入到了Nginx,這個模塊主要是爲了將網頁中的所請求都轉發本身的服務器。

二、在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;  
        }

Nginx替換關鍵詞

三、單個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;
        }
    }

四、upstream google 寫了Google的服務器IP地址,若是請求量很是大的話,建議多寫一些。

5、Nginx反向綁定域名:使用Nginx緩存來加速訪問請求

一、nginx 自帶的 proxy_cache 模塊能夠實現訪問緩存,即第二次訪問能夠直接從本身的服務器讀取相應的數據了,而不須要再來一次中轉請求了。

二、先在Http層加入如下代碼,相似:

    proxy_cache_path   /home/cache/freehao123  levels=1:2   keys_zone=one:10m max_size=10g;
    proxy_cache_key    "$host$request_uri";
    server
    {
        listen 80;
        server_name www.freehao123.com freehao123.com;
        location / {
              rewrite ^/(.*)$ https://freehao123.com$1 permanent;
        }
    }

三、proxy_cache_path 是緩存目錄路徑,你須要提早建立好,並設置好讀寫權限。

四、接着在location中加入如下代碼,相似於:

 location / {
		  proxy_cache one;
                proxy_cache_valid  200 302 1h;
                proxy_cache_valid  404 1m;                
                proxy_redirect https://www.google.com/ /;                
                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;     
        }

五、proxy_cache 中的值要與前面的keys_zone值相同。重啓Nginx後,可使用Https訪問了。

Nginx使用SSL訪問

六、同時打開緩存目錄,能看到生成了緩存數據了。

Nginx看到緩存數據

6、Nginx反向綁定域名:解決Google和Gravatar沒法訪問的問題

一、上面的代碼都是基於反向D理Google的,如下就是經部落測試有效的代碼,你只須要將域名、upstream IP地址、證書路徑、緩存目錄等改本身的內容便可:

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    proxy_connect_timeout      5;
    proxy_read_timeout         60;
    proxy_send_timeout         5;
    proxy_buffer_size          16k;
    proxy_buffers              4 64k;
    proxy_busy_buffers_size    128k;
    proxy_temp_file_write_size 128k;
    proxy_temp_path            /usr/local/src/cache/temp;
    proxy_cache_path /usr/local/src/cache/one  levels=1:2   keys_zone=one:10m inactive=7d max_size=10g;
    proxy_cache_key "$host$request_uri";
	upstream google {
                server 64.15.24.122:80 max_fails=3;
                server 92.19.28.214:80 max_fails=3;
                server 64.150.13.60:80 max_fails=3;       
        }
    server {
        listen 80;
        server_name www.mmtaoyi.com mmtaoyi.com;
        rewrite ^/(.*)$ https://mmtaoyi.com$1 permanent;
        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
	
	server {
         listen 443;
        server_name www.mmtaoyi.com mmtaoyi.com;

        if ($host = 'www.mmtaoyi.com') {
            rewrite ^/(.*)$ https://mmtaoyi.com$1 permanent;
        }
        ssl on;
        ssl_certificate /usr/local/src/myssl/myssl.crt;
        ssl_certificate_key /usr/local/src/myssl/privkey.key;
        location / {
                proxy_cache one;
                proxy_cache_valid  200 302 1h;
                proxy_cache_valid  404 1m;
		  proxy_cache_valid 301 3d;
		  proxy_cache_valid any 1m;
		  proxy_cache_use_stale invalid_header error timeout http_502;
                proxy_redirect https://www.google.com/ /;
                proxy_cookie_domain google.com mmtaoyi.com;
                proxy_pass http://google;
                proxy_set_header Host "www.google.com";
                proxy_set_header Accept-Encoding "";
                proxy_set_header User-Agent $http_user_agent;
                proxy_set_header Accept-Language "zh-CN";
                proxy_set_header Cookie "PREF=ID=047808f19f6de346:U=0f62f33dd8549d11:FF=2:LD=zh-CN:NW=1:TM=1325338577:LM=1332142444:GM=1:SG=2:S=rE0SyJh2w1IQ-Maw";
		 subs_filter_types text/css text/xml text/javascript;
		 subs_filter ssl.gstatic.com mmtaoyi.com;
		 subs_filter www.google.com mmtaoyi.com ;
				
                }
				 location /gb {
        
                 proxy_pass http://ssl.gstatic.com/gb/;
                 proxy_set_header Accept-Encoding "";
                  }
		
        }

}

Nginx綁定Google

二、解決Gravatar頭像沒法顯示的問題也是同樣的原理,咱們只須要將反向綁定的域名換成Gravatar的secure.gravatar.com就好了。

三、解決Gravatar頭像不顯示的代碼,部落測試有效的以下:

    proxy_cache_path   /home/cache/mmtaoyi  levels=1:2   keys_zone=one:10m max_size=10g;
    proxy_cache_key    "$host$request_uri";
    server
    {
        listen 80;
        server_name www.mmtaoyi.com mmtaoyi.com;
        location / {
              rewrite ^/(.*)$ https://mmtaoyi.com$1 permanent;
        }
    }

	
	server
    {
        listen 443;
        server_name www.mmtaoyi.com mmtaoyi.com;

        if ($host = 'www.mmtaoyi.com') {
            rewrite ^/(.*)$ https://mmtaoyi.com$1 permanent;
        }
        ssl on;
        ssl_certificate /root/myssl/myssl.crt;
        ssl_certificate_key /root/myssl/privkey.key;
       

        location / {
		proxy_cache one;
                proxy_cache_valid  200 302 1h;
                proxy_cache_valid  404 1m;
		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://secure.gravatar.com;
                proxy_set_header Host "secure.gravatar.com";
                proxy_set_header Accept-Encoding "";
                proxy_set_header User-Agent $http_user_agent;     
        }
    }

Nginx替換相應的域名

三、訪問本身的域名後,會看到是Gravatar網站的內容。

Nginx看到網站的內容

四、將本身的域名替換爲Gravatar頭像的域名,就可以正常顯示圖片了。

Nginx正常顯示圖片

7、Nginx反向綁定域名方法小結

一、上面介紹了三種單獨安裝Nginx的方法,都是通過部落測試經過的,啓動了Nginx後就能夠用你的瀏覽器打開IP地址訪問到有Nginx標誌的頁面了。若是沒法訪問,建議檢查VPS主機的防火牆有沒有開啓80和443端口,開啓方法:

/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 443 -j ACCEPT
/etc/init.d/iptables save 
/etc/init.d/iptables restart

二、ngx_http_substitutions_filter_module這個模塊是用來替換反向綁定域名頁面的關鍵詞,在第三種安裝Nginx的方法中增長了這個模塊。像Google這樣的的加載了ssl.gstatic.com這個網址的內容,咱們也都一併將其替換了。

相關文章
相關標籤/搜索