利用nginx搭建谷歌反向代理

谷歌技术 优优 930℃ 0评论

由于某些众所周知的原因,谷歌在国内一直处于抽风状态,那么我么是否应该做点什么来正常访问谷歌呢?

没错,第一种方法就是翻墙。如果你想让你的同学朋友也能够访问谷歌,但又怕vpn账号分享多了会不安全,肿么办?自个儿搭建一个google吧!

为了更有说服力,我自个儿就搭建了一个:https://ggso.in(谷歌) https://ggso.in/scholar(谷歌学术) 速度有点慢,大家见谅,主要是为了给大家演示,所以就随便找了个VPS搭建的

演示系统:Ubuntu 1404

一、安装编译nginx

1、首先更新系统

sudo apt-get update && sudo apt-get upgrade

2、安装的 Nginx 需要的包以及 Git

sudo apt-get install libpcre3 libpcre3-dev zlib1g-dev libssl-dev build-essential git

3、新建立个 Nginx 目录,方便管理

mkdir nginx && cd nginx

 

下载 Nginx 最新稳定版,目前的版本是 1.6.2,用 Git 克隆两个 Nginx 模块,一个是 wen.lu 开源ngx_http_google_filter_module,另一个是 Nginx 替换关键词模块ngx_http_substitutions_filter_module

wget http://nginx.org/download/nginx-1.6.2.tar.gz
tar -xvf nginx-1.6.2.tar.gz
git clone https://github.com/cuber/ngx_http_google_filter_module
git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module

4、进入 Nginx 目录并创建个 Nginx 临时文件夹

cd nginx-1.6.2
mkdir /var/tmp/nginx

5、使用下面的参数开始编译

./configure \
--prefix=/usr --conf-path=/etc/nginx/nginx.conf --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi --http-scgi-temp-path=/var/tmp/nginx/scgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module \
--add-module=/root/nginx/ngx_http_google_filter_module \
--add-module=/root/nginx/ngx_http_substitutions_filter_module

6、没问题以后直接用 make 安装

make && make install


二、开启 Nginx 服务
1、进入系统的 /etc/init.d 目录

cd /etc/init.d/


2、新建并编辑一个 nginx 文件

vi nginx

认这么安装好以后每次检查配置、重启之类的操作略麻烦,所以我们模仿 Ubuntu 14.04 官方源,给系统设置个 nginx 服务,方便我们检查配置、启动重启关闭 Nginx 以及开机自动启动 Nginx

3、具体内容如下

#!/bin/sh
### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx
# Include nginx defaults if available
if [ -f /etc/default/nginx ]; then
   . /etc/default/nginx
fi
test -x $DAEMON || exit 0
set -e
. /lib/lsb/init-functions
test_nginx_config() {
   if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1; then
      return 0
   else
      $DAEMON -t $DAEMON_OPTS
      return $?
   fi
}
case "$1" in
   start)
      echo -n "Starting $DESC: "
      test_nginx_config
      # Check if the ULIMIT is set in /etc/default/nginx
      if [ -n "$ULIMIT" ]; then
         # Set the ulimits
         ulimit $ULIMIT
      fi
      start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
          --exec $DAEMON -- $DAEMON_OPTS || true
      echo "$NAME."
      ;;
   stop)
      echo -n "Stopping $DESC: "
      start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
          --exec $DAEMON || true
      echo "$NAME."
      ;;
   restart|force-reload)
      echo -n "Restarting $DESC: "
      start-stop-daemon --stop --quiet --pidfile \
          /var/run/$NAME.pid --exec $DAEMON || true
      sleep 1
      test_nginx_config
      # Check if the ULIMIT is set in /etc/default/nginx
      if [ -n "$ULIMIT" ]; then
         # Set the ulimits
         ulimit $ULIMIT
      fi
      start-stop-daemon --start --quiet --pidfile \
          /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
      echo "$NAME."
      ;;
   reload)
      echo -n "Reloading $DESC configuration: "
      test_nginx_config
      start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
          --exec $DAEMON || true
      echo "$NAME."
      ;;
   configtest|testconfig)
      echo -n "Testing $DESC configuration: "
      if test_nginx_config; then
         echo "$NAME."
      else
         exit $?
      fi
      ;;
   status)
      status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
      ;;
   *)
      echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
      exit 1
      ;;
esac
exit 0

4、赋予权限并增加到系统服务

sudo chmod +x ./nginx
sudo update-rc.d nginx defaults

三、修改默认的 nginx.conf 配置文件

默认官方的配置文件写的很简单,这里我们也模仿 Ubuntu 14.04 官方源修改一个适合我们的 Nginx 配置

1、编辑 /etc/nginx/nginx.conf

vi /etc/nginx/nginx.conf

具体内容如下:

worker_processes 4;
pid /var/run/nginx.pid;
events {
	worker_connections 768;
}
http {
	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;
	server_tokens off;
	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;
	proxy_temp_file_write_size 128k;
	proxy_temp_path   /var/cache/nginx/temp;
	proxy_cache_path  /var/cache/nginx/cache levels=1:2 keys_zone=cache_one:100m inactive=7d max_size=10g;
	gzip_static on;
	gzip on;
	gzip_disable "msie6";
	include /etc/nginx/sites-enabled/*;
}

2、新建几个必要的文件夹

其中 /etc/nginx/sites-enabled 用来放我们的网站配置文件,/var/log/nginx 用来放 log 日志文件,/var/cache/nginx/cache 和/var/cache/nginx/temp 则是 Nginx 反代缓存文件夹。

mkdir -p /etc/nginx/sites-enabled
mkdir -p /var/log/nginx
mkdir -p /var/cache/nginx/cache
mkdir -p /var/cache/nginx/temp

3、检查 Nginx 配置

直接运行 nginx -t 如果输出如下提示,则一切正常

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

4、接着我们新建一个配置文件,用来反代 Google

server {
  server_name 你的域名;
  listen 80;
  resolver 8.8.8.8;
  location / {
    google on;
  }
}

5、保存后重启下 Nginx

service nginx restart

好了,访问你的域名,试试看能否打开谷歌了

转载请注明:优优 » 利用nginx搭建谷歌反向代理

喜欢 (2)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
HTTP/1.1 502 Bad Gateway Content-Type: text/html Connection: close Content-Length: 303 Date: Mon, 16 Jan 2017 22:53:34 GMT X-Via: 1.1 VM_36_107_centos (random:423408 Fikker/Webcache/3.7.1) 502 Bad Gateway

502 Bad Gateway - Cann't Connect To Upstream Server By SSL Read

Server: VM_36_107_centos
Date: 2017-01-16 22:53:34

Fikker/Webcache/3.7.1