nginx 反向代理搭建教程

前言

因为要做Google学术反向代理,正好nginx有个对应的模块,但版本不支持最新版nginx所以选择了旧版nginx 1.78版本,系统版本centos7。

安装Nginx

安装依赖

yum install gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openssl-devel  git make

下载nginx

wget "http://nginx.org/download/nginx-1.7.8.tar.gz"

下载google 扩展

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.7.8.tar.gz
cd nginx-1.7.8

编译安装

./configure \
  --prefix=/etc/nginx \--with-http_ssl_module \
--with-http_stub_status_module \
--add-module=/root/ngx_http_google_filter_module \ --add-module=/root/ngx_http_substitutions_filter_module

配置环境变量

vim /etc/profile  //编辑文件
 
export PATH=$PATH:/usr/sbin/nginx/sbin  //添加该行

source ~/.bashrc  //生效

 nginx管理

nginx //启动
nginx -s stop  //停止
nginx -s reload //重启

修改nginx配置文件

server {

    listen 443 ssl;
    server_name 域名;
    ssl_certificate /etc/nginx/ssl/xx.pro_chain.crt;
    ssl_certificate_key /etc/nginx/ssl/xx.pro_key.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    resolver 8.8.8.8;
    
    location / {
        subs_filter_types *;
        google on;
        google_scholar on;
    }
}

该模块是将学术和搜索合并在一起的,通过两个域名实现url重写将学术绑定独立域名

    server {
        listen 443 ssl;
        server_name xx.pro;  #域名
        ssl_certificate /etc/nginx/ssl/xx.pro_chain.crt;
        ssl_certificate_key /etc/nginx/ssl/xx.pro_key.key; 
        
        location / {

        if ($request_uri ~* "/scholar\?"){
            proxy_pass https://www.xx.pro/scholar?$args;
            break;
        }
            proxy_pass https://www.xx.pro/scholar/;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/hlikex/p/12729246.html