一聚教程网:一个值得你收藏的教程网站

热门教程

Ubuntu 14.04利用Nginx反代理Google

时间:2022-06-30 19:15:37 编辑:袖梨 来源:一聚教程网


1.安装相关软件

#
# 安装 gcc & git
apt-get install build-essential git gcc g++ make

# 下载Nginx最新版源码
# http://nginx.org/en/download.html
wget "http://nginx.org/download/nginx-1.9.2.tar.gz"

# 下载最新版 pcre
# http://www.pcre.org/
wget "ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz"

# 下载最新版 openssl
# https://www.openssl.org/
wget "https://www.openssl.org/source/openssl-1.0.2c.tar.gz"

# 下载最新版 zlib
# http://www.zlib.net/

wget "http://zlib.net/zlib-1.2.8.tar.gz"

# 下载本扩展
git clone https://github.com/cuber/ngx_http_google_filter_module

# 下载 substitutions 扩展
git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module

# 解压缩
tar xzvf nginx-1.9.2.tar.gz
tar xzvf pcre-8.36.tar.gz
tar xzvf openssl-1.0.2c.tar.gz
tar xzvf zlib-1.2.8.tar.gz


# 进入 nginx 源码目录
cd nginx-1.9.2

# 设置编译选项

./configure
  --prefix=/opt/nginx-1.9.2
  --with-pcre=../pcre-8.36
  --with-openssl=../openssl-1.0.2c
  --with-zlib=../zlib-1.2.8
  --with-http_ssl_module
  --add-module=../ngx_http_google_filter_module
  --add-module=../ngx_http_substitutions_filter_module

make
sudo make install

# 启动Nginx
sudo /opt/nginx-1.9.2/sbin/nginx
2.配置Nginx

编辑/opt/nginx-1.9.2/conf/nginx.conf

我的配置如下,红色部分为修改/追加于默认配置的内容

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

server {
#强制http跳转https
        listen 80;
        server_name 域名;
        rewrite ^/(.*) https://域名/$1 permanent;
}

    server {
        listen       443;
        server_name  域名;

        ssl on;
        ssl_certificate 证书.crt路径;
        ssl_certificate_key 证书.key路径;

        resolver 8.8.8.8;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
                #添加Google和Google学术支持,重要!
                google on;
                google_scholar on;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

}
最后重载一下Nginx的配置,应该就能看到Google的页面了。

/opt/nginx-1.9.2/sbin/nginx -s reload

热门栏目