环境:Ubuntu 14.04 (32bit或64bit)
本文核心内容来自https://ttt.tt/162,感谢作者。也非常感谢感谢wen.lu的开源。
在看这篇文章之前,首先你得准备一台墙外的服务器或 VPS,本文默认用 root 用户,并操作在系统的 /root 目录下,为了安全考虑,请自行更换目录。

一、编译安装Nginx
1、安装前的准备:更新系统

apt-get update && apt-get upgrade

2、安装Nginx需要的包和Git

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

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

mkdir nginx && cd nginx

4、Nginx 从1.9.5开始支持http/2协议,我们选择Stable version,截至到写这篇教程,Stable version版本最新为1.10.1。下载openssl,至少需要1.0.2。如果实际版本与本文不符,请注意修改命令中的路径(会在涉及到的步骤中提醒)

wget http://nginx.org/download/nginx-1.10.1.tar.gz
wget https://www.openssl.org/source/openssl-1.0.2h.tar.gz
tar -xvf nginx-1.10.1.tar.gz
tar -xvf openssl-1.0.2h.tar.gz

5、用Git克隆两个Nginx模块,一个是wen.lu开源的ngx_http_google_filter_module,另一个是 Nginx 替换关键词模块 ngx_http_substitutions_filter_module

git clone https://github.com/cuber/ngx_http_google_filter_module
git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module

6、进入 Nginx 目录并创建个 Nginx 临时文件夹,请注意路径

cd nginx-1.10.1
mkdir /var/tmp/nginx

7、使用下面的参数开始编译,请注意结尾openssl的路径

./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 --add-module=/root/nginx/ngx_http_google_filter_module --add-module=/root/nginx/ngx_http_substitutions_filter_module --with-http_ssl_module --with-http_v2_module --with-openssl=/root/nginx/openssl-1.0.2h

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

make && make install

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

cd /etc/init.d/

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

vi 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 auto; //根据CPU核心数填写,不确定就auto
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

四、开启 Nginx SSL 支持(使用Let’s Encrypt免费证书请参考http://www.isway.cn/?p=352)
1、为了管理方便,我们建立个 ssl 目录

mkdir -p /root/ssl && cd /root/ssl

2、运行下面的命令,生成 example.com.key 和 example.com.csr(请自行将example.com改成自己的域名)

openssl req -new -newkey rsa:2048 -nodes -out example.com.csr -keyout example.com.key -subj "/C=US/ST=CA/L=Los Angeles/O=Example Inc./OU=Web Security/CN=example.com"

如果是泛域名证书,最后的域名改成 *.example.com
3、然后把 csr 文件提交给你的 SSL 证书商,验证好域名以后会颁发给你一个 .crt 文件,我们命名为 example.com.crt
4、接着我们新建一个配置文件,用来反代 Google:请自行更换配置文件里的域名和证书文件名

vi /etc/nginx/sites-enabled/google.conf

具体配置如下

server {
listen 80;
server_name example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl http2;
server_name example;
ssl on;
ssl_certificate /root/ssl/example.com.crt;
ssl_certificate_key /root/ssl/example.com.key;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers EECDH+aRSA+AES;
keepalive_timeout 70;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
resolver 8.8.8.8;
location / {
google on;
google_scholar on;
}
}

5.       保存后重启下 Nginx

service nginx restart

OK,大功告成!浏览器里访问 https://example.com/ 看看是否已经可以反代 Google 了。
【完】