openresty正向代理安装部署

第一部分 安装openrestry

下载安装包:https://github.com/openresty/openresty/releases
直接下载最新稳定版本:openresty-1.15.8.3.tar.gz

1)初始化安装
[root@www ~]# wget https://github.com/openresty/openresty/releases/download/v1.15.8.3/openresty-1.15.8.3.tar.gz
[root@www ~]# tar -xf openresty-1.15.8.3.tar.gz
[root@www ~]# cd openresty-1.15.8.3

[root@www openresty-1.15.8.3]# yum install readline-devel pcre pcre-devel openssl openssl-devel gcc curl GeoIP-devel
[root@www openresty-1.15.8.3]# ./configure --help 查看安装模块
可以编译安装,平滑升级。

[root@www openresty-1.15.8.3]# ./configure --prefix=/usr/local/openresty --with-luajit --with-pcre --with-http_gzip_static_module --with-http_realip_module --with-http_geoip_module --with-http_ssl_module --with-http_stub_status_module

--with-http_gzip_static_module #静态文件压缩
--with-http_stub_status_module #监控nginx状态
--with-http_realip_module #通过这个模块允许我们改变客户端请求头中客户端IP地址值(例如X-Real-IP 或 X-Forwarded-For),意义在于能够使得后台服务器记录原始客户端的IP地址
--with-pcre #设置PCRE库(pcre pcre-devel)
--with-http_ssl_module #使用https协议模块。(openssl openssl-devel)
--with-http_geoip_module #增加了根据ip获得城市信息,经纬度等模块 (GeoIP-devel)

[root@www openresty-1.15.8.3]# make && make install
[root@www openresty-1.15.8.3]# /usr/local/openresty/nginx/sbin/nginx -t

2)安装成功后,默认会在/usr/local/openresty/
目录下
luajit 是采用C语言写的Lua代码的解释器 ----just in time 即时解析
lualib 是编辑好的lua类库
nginx,其实我们openResty就是nginx,只是做了一些模块化工作;所以启动openResty就是启动nginx,我们可以到 cd nginx/sbin/,直接运行 ./nginx

3)设置环境变量
# vi /etc/profile
export NGINX_HOME=/usr/local/openresty/nginx
export PATH=$PATH:$NGINX_HOME/sbin
# source /etc/profile ##生效

第二部分 openrestry的lua模块
helloworld 入门

lua脚本基础
(py36env) [root@www luajit]# cat helloworld.lua
print("hello world..")
(py36env) [root@www luajit]# /usr/local/openresty/luajit/bin/luajit helloworld.lua
hello world..

(py36env) [root@www luajit]# cat helloworld.lua
function sayHello()
print("hello world..")
end
sayHello();
(py36env) [root@www luajit]# /usr/local/openresty/luajit/bin/luajit helloworld.lua
hello world..


第三部 Nginx结合lua

正向代理:

(py36env) [root@www ~]# cat /usr/local/openresty/nginx/conf/nginx.conf
user root;
worker_processes 1;
events {
    worker_connections 1024;
}
http {
    include mime.types;
    default_type application/octet-stream;
    log_format main '$remote_addr - $remote_user [$time_local] "$request" $server_addr '
        '$status $body_bytes_sent "$http_referer" "$http_host" '
        '"$http_user_agent" "$http_x_forwarded_for"';
    access_log logs/access.log main;
    sendfile on;
    keepalive_timeout 65;
    lua_shared_dict balck_list 1m;
    server {
        resolver 114.114.114.114; #指定DNS服务器IP地址
        listen 80;
        location / {
            access_by_lua_file ../luajit/blacklist_url_list.lua;
            proxy_pass http://$host$request_uri; #设定代理服务器的协议和地址
            proxy_set_header HOST $host;
            proxy_buffers 256 4k;
            proxy_max_temp_file_size 0k;
            proxy_connect_timeout 30;
            proxy_send_timeout 60;
            proxy_read_timeout 60;
            proxy_next_upstream error timeout invalid_header http_502;
           }
    }
}
(py36env) [root@www ~]# cat /usr/local/openresty/luajit/blacklist_url_list.lua
local redis = require "resty.redis"
local red = redis:new()
local ok, err = red:connect('127.0.0.1', 6379)
if not ok then
    ngx.log(ngx.INFO, "err:" ..err)
end

local urllist = ngx.var.http_host
local exists, err = red:sismember("blacklist_url_list", urllist)
if err then
    return ngx.exit("aaaaaaaaaaaa..")
end

if exists ~= 1 then
    return ngx.exit(ngx.HTTP_FORBIDEN)
end
(py36env) [root@www ~]#

(py36env) [root@www ~]# redis-cli
127.0.0.1:6379> KEYS *
127.0.0.1:6379> SMEMBERS blacklist_url_list
127.0.0.1:6379> sadd blacklist_url_list www.baidu*.com 添加目标url key下面的某个值
127.0.0.1:6379> SREM blacklist_url_list www.baidu1.com 删除目标url key下面的某个值

操作key,访问验证。
[root@localhost ~]# curl -x 192.168.1.118:80 www.baidu.com

问题:
1、只能精确匹配到目标URL,不能模糊匹配;(redis key是否满足, 或在lua上面下手)
2、只能访问目标URL,匹配http开头的, https暂时不支持。(再启动一个https server,或添加模块)

(py36env) [root@www ~]# cd openresty-1.15.8.3/
(py36env) [root@www openresty-1.15.8.3]# git clone https://github.com/chobits/ngx_http_proxy_connect_module.git
(py36env) [root@www openresty-1.15.8.3]# cd /root/openresty-1.15.8.3
(py36env) [root@www openresty-1.15.8.3]# yum install -y patch
参考文档:https://blog.csdn.net/joshua317/article/details/123242002
https://blog.csdn.net/sleepIII/article/details/100787646
(py36env) [root@www openresty-1.15.8.3]# ./configure --add-module=/root/openresty-1.15.8.3/ngx_http_proxy_connect_module
(py36env) [root@www openresty-1.15.8.3]# patch -d build/nginx-1.15.8/ -p1 </root/openresty-1.15.8.3/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_101504.patch
(py36env) [root@www openresty-1.15.8.3]# make
(py36env) [root@www openresty-1.15.8.3]# cp /usr/local/openresty/nginx/sbin/nginx /usr/local/openresty/nginx/sbin/nginx-20220807
(py36env) [root@www openresty-1.15.8.3]# cp -rfp build/nginx-1.15.8/objs/nginx /usr/local/openresty/nginx/sbin/nginx

nginx官方模块仅支持做http正向代理,而项目需要使用到https正向代理。
ngx_http_proxy_connect_module模块主要用于隧道SSL请求的代理服务器。通过配置,可以通过HTTP连接隧道获得任何https网站。

proxy_connect; 启用“连接”http方法支持
proxy_connect_allow; 指定代理连接方法可以连接的端口号或者范围的列表。默认情况只启用默认的https端口( 443 ) 和默认的snews端口( 563 )
proxy_connect_connect_time; 定义与代理服务器建立连接的超时时间。
proxy_connect_read_time; 定义从代理服务器读取响应的超时时间。

如下配置,支持https格式,但是前面的黑白名单乱了。
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" $server_addr '
'$status $body_bytes_sent "$http_referer" "$http_host" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
lua_shared_dict balck_list 1m;
server {
resolver 114.114.114.114; #指定DNS服务器IP地址
listen 80;
proxy_connect;
proxy_connect_allow 443 563;
proxy_connect_connect_timeout 30s;
proxy_connect_read_timeout 30s;
proxy_connect_send_timeout 30s;
location / {
access_by_lua_file ../luajit/blacklist_url_list.lua;
proxy_pass http://$host$request_uri; #设定代理服务器的协议和地址
proxy_set_header HOST $host;
proxy_buffers 256 4k;
proxy_max_temp_file_size 0k;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_next_upstream error timeout invalid_header http_502;
}
}
}

根据日志查看,http_host字段,带有端口,所以黑白名单配置不支持了。
$http_host始终等于HTTP_HOST请求标头。
$host等于$http_host,小写且没有端口号(如果存在),除非HTTP_HOST缺少或为空值.在这种情况下,$host等于server_name处理请求的服务器的指令的值。
此时需要修改lua脚本里面的参数变量,就支持https目标了,但是黑白名单https协议的还是不支持,需做另一步排查。

修改之后不启作用,需要排除。

 参考:http://tengine.taobao.org/document_cn/proxy_connect_cn.html

posted @ 2022-08-07 21:40  wang_wei123  阅读(2065)  评论(0编辑  收藏  举报