新部落格,整理一下過去東西,發現基本都是沒什麼用的東西,都是湊數的,搬過來一篇Nginx反代Google實現國內訪問,個人認為比較有用,我也不是一個煽情的人,廢話也就不說了,至於為什麼反代,我相信你自己心裡清楚。
準備材料
1、除大陸外任何一個可以訪問Google的VPS,記憶體最低32MB。
2、反代Google的網域名稱一枚。
3、SSL一枚,可以使用startssl的免費認證,或者Wosgin的免費認證,當然你也可以選擇在淘寶購買7元的SSL,當然這一項也是可選的。
安裝Nginx
在安裝之前我們需要先安裝Nginx需要的依賴。在VPS執行下面的命令。
| 代碼如下 |
複製代碼 |
yum install glib2-devel openssl-devel pcre-devel bzip2-devel gzip-devel |
然後開始安裝Nginx
| 代碼如下 |
複製代碼 |
wget http://nginx.org/download/nginx-1.7.7.tar.gz tar -zxvf nginx-1.7.7.tar.gz cd nginx-1.7.7 ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --with-http_spdy_module make && make install echo "export PATH=$PATH:/nginx/sbin:">>/etc/profile source /etc/profile |
常用命令
| 代碼如下 |
複製代碼 |
/usr/local/nginx/sbin/nginx #啟動 /usr/local/nginx/sbin/nginx -s stop #關閉 /usr/local/nginx/sbin/nginx -t #測試組態檔案是否正確 /usr/local/nginx/sbin/nginx -s reload #不關閉的情況下重新載入設定檔 |
如果是軍哥的LNMP一鍵包,那麼更簡單只需要進入LNMP的安裝目錄,然後執行
| 代碼如下 |
複製代碼 |
vi upgrade_nginx.sh 在編譯參數後面加上 --with-http_sub_module --with-http_spdy_module 然後執行 bash upgrade_nginx.sh 開始反代 在你的.conf檔案改為以下內容 proxy_cache_path /data/nginx/cache/one levels=1:2 keys_zone=one:10m max_size=10g; proxy_cache_key "$host$request_uri"; server { listen 80; server_name domain.com www.domain.com; rewrite ^(.*) https://www.domain.com/$1 permanent; } upstream google { server 74.125.224.80:80 max_fails=3; server 74.125.224.81:80 max_fails=3; server 74.125.224.82:80 max_fails=3; server 74.125.224.83:80 max_fails=3; server 74.125.224.84:80 max_fails=3; } server { listen 443; server_name domain.com www.domain.com; ssl on; ssl_certificate /usr/local/nginx/conf/ssl.crt; ssl_certificate_key /usr/local/nginx/conf/ssl.key; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-RC4-SHA:ECDHE-RSA-AES128-SHA:AES128-GCM-SHA256:RC4:HIGH:!MD5:!aNULL:!EDH:!CAMELLIA:!PSK:!SRP; ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; location / { proxy_cache one; proxy_cache_valid 200 302 1h; proxy_cache_valid 404 1m; proxy_redirect https://www.google.com/ /; proxy_cookie_domain google.com domain.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"; sub_filter www.google.com www.domain.com; sub_filter_once off; } } |
部分代碼解釋
這一句是你的SSL認證的根憑證和認證的合并體
ssl_certificate /usr/local/nginx/conf/ssl.crt;
這一句是你產生CSR的時候,一起產生的key檔案
ssl_certificate_key /usr/local/nginx/conf/ssl.key;
這裡是反代的Google的IP。可以修改的!
| 代碼如下 |
複製代碼 |
upstream google { server 74.125.224.80:80 max_fails=3; server 74.125.224.81:80 max_fails=3; server 74.125.224.82:80 max_fails=3; server 74.125.224.83:80 max_fails=3; server 74.125.224.84:80 max_fails=3; }
|
注意
請將檔案的domain.com 和 www.domain.com 替換成你自己的網域名稱。反代其他的就把文中的www.google.com 和 google.com換成你要反代的網域名稱。並替換文中的upstream中的IP