当前位置:主页 > 查看内容

基于Nginx的可伸缩web平台openresty做https代理和报文日志打印服

发布时间:2021-06-15 00:00| 位朋友查看

简介:在北京生活路上遇到很多和我们擦肩而过的人哪怕看他平平无奇你记住他们一定有故事~ 大家好我是奈何老规矩我们先听故事后学知识 不知从何说起的我开始支支吾吾的嗯…~那就从这里开始吧。 来到北京后最近项目经理给了我很多需求竟然是让我干运维的活儿其实我也……

北京生活,路上遇到很多和我们擦肩而过的人,哪怕看他平平无奇,你记住,他们一定有故事~

大家好,我是奈何,老规矩,我们先听故事,后学知识!
在这里插入图片描述
不知从何说起的我,开始支支吾吾的,嗯…~(那就从这里开始吧。)

来到北京后,最近项目经理给了我很多需求,竟然是让我干运维的活儿,其实我也应该为此而感到欣慰,毕竟想做一名的优秀的Java开发也得从全栈开始学起吧(【滑稽】好像互联网大厂也要求Java开发什么都会吧~对,是的!)。

我硬着头皮接了这一系列需求,从配置测试服务器、安装docker、配置Jenkins等等开始,现在要求我去安装配置nginx的https代理。于是我大干了一场,同时踩了许多的坑,也成长了许多…(这只是故事一个简单的开始~!)

在这里插入图片描述

于是,我就开始了安装和配置nginx,这时候我高兴的配置好了nginx的https代理,感觉自己好像完成了这次任务,结果经理告诉我nginx得打印日志呀,我们得看到全部的请求和响应报文等信息的,要么以后排查问题咋整?(之前我就简单的配置了nginx的access日志)于是,我又接下来这个看似很可怕的需求,开始进行求助,于是发现了可以使用nginx配置lua脚本实现打印日志的功能。但是由于解决这个问题需要安装nginx、LuaJIT和lua-nginx-module模块,而安装的过程需要配置环境变量和nginx关联lua,这个过程很容易出错而且问题多多。通过大佬的引荐,我有幸用到了openresty。有的小伙伴会问,openresty是什么呢,接下来回答会让你充满了幸福感!


OpenResty(又称:ngx_openresty) 是一个基于 NGINX 的可伸缩的 Web 平台,是一个强大的 Web 应用服务器,在性能方面,OpenResty可以 快速构造出足以胜任 10K 以上并发连接响应的超高性能 Web 应用系统。目标是让你的 Web 服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL,PostgreSQL,~Memcaches 以及 ~Redis 等都进行一致的高性能响应。MySQL,PostgreSQL,~Memcaches 以及 ~Redis 等都进行一致的高性能响应。


看到了OpenResty的我,仿佛看到了春天,瞬间有了春风拂面的感觉,我洋溢在美好的春天一不小心划了那么一会~~~当自己反应过来的时候,时间已过了很久,撸起袖子,卸载了nginx,开始安装OpenResty!哦买噶,突然感觉幸福来的太突然了!舒服


在安装之前,首先卸载原来的nginx,如下步骤:

# 停止nginx服务
ps -ef | grep nginx 
# 进入sbin目录
cd /usr/local/nginx/sbin
./nginx -s stop
# 找到nginx相关目录文件
sudo find / -name nginx
# 根据找到的文件路径进行删除
sudo rm -rf  找到的文件路径

卸载nginx完成后开始安装openresty,如下步骤:

openresty下载地址http://openresty.org/en/download.html

PS:选择比较靠前的版本,因为nginx的版本在1.11.8支持escape=json参数,可以消除低版本的特殊符号转码问题,简单可以理解为高版本已经解决了符号乱码问题!

乱码问题的坑我已经踩了,就是nginx低于1.11.8,打印的日志特殊符号会变成\xx22等乱码,只要下载高版本的openresty,查看nginx版本是否大于此版本即可自动解决乱码问题!

开始安装

yum -y install readline-devel pcre-devel openssl-devel
tar xzvf ngx_openresty-1.15.8.2.tar.gz       # 解压
cd ngx_openresty-1.15.8.2/ 
# 如果安装需要指定目录的话,可以在如下命令后追加右侧目录 --prefix=指定目录
./configure
make 
make install

测试openresty

# mkdir /data/test
# cd /data/test/
# mkdir logs/conf/
# vim logs/conf/nginx.conf
# 文件夹和配置文件创建好,复制以下内容保存开始下一步验证

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 9000;
        location / {
            default_type text/html;
            content_by_lua '
                ngx.say("<p>Hello, World!</p>")
            ';
        }
    }
}

验证

# cd /data/test
# /usr/local/openresty/nginx/sbin/nginx -p `pwd`/ -c conf/nginx.conf
# 默认情况下 openresty 安装在 /usr/local/openresty 目录中 -p 指定我们的项目目录,-c 指定配置文件。没有任何输出,说明启动成功
# curl http://localhost:9000/
<p>Hello, World!</p>      # 证明服务正常
或者浏览器访问:http://ip:9000,看是结果是否为:Hello, World!

看到如此熟悉的Hello,World!是不是倍感亲切呢,那么恭喜你安装成功啦!


接下来开始配置https代理和报文日志打印【划重点咯!】

nginx做https代理与打印报文日志的配置如下:(进到nginx里,去编辑nginx.conf,配置如下,步骤我有加注释)

PS:你可以根据原来nginx的默认配置而做修改,这样可保证nginx配置不会乱,而且你也能很清楚的知道自己加了什么配置,加的配置是做什么的!尽量不要直接COPY如下配置,可以用什么COPY什么!【推荐】

#user  nobody; # 用户组
worker_processes  1;

# 打开错误日志和pid注释
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;
    
    # 配置打印日志的格式,为了更好的排查问题打印的日志内容做了精简,这里我只留下了排查问题必不可少的日志内容
    log_format main  escape=json '{ "@timestamp": "$time_local", '
                         '"upstream_addr": "$upstream_addr",'
                         '"request_time": "$request_time", '
                         '"status": "$status", '
                         '"request": "$request", '
                         '"host":""$host",'
                         '"http_uri": "$uri",'
                         '"请求报文":"$request_body",'
                         '"响应报文":"$resp_body" }'

    # 开启日志缓存以免过多的使用内存
    open_log_file_cache max=1000 inactive=20s valid=1m min_use=2;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
   
    # 以下解决request_body为空问题 
    # 指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答
    fastcgi_buffers 32 8k;
    # 缓冲区代理缓冲用户端请求的最大字节数
    client_body_buffer_size 1024k;

    server {
        listen       80; # 端口
        server_name  xxx.com; # 域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        
        # 添加以下配置
        charset utf-8;
        set $resp_body "";
        access_log  /data/openresty-1.15/nginx/logs/nginx.log  main; # 配置日志路径

        location / {
            #root   html;
            #index  index.html index.html;
            
            # 开启强制获取请求报文日志
            lua_need_request_body on;
            log_escape_non_ascii off;
            # lua
            body_filter_by_lua '
                local resp_body = string.sub(ngx.arg[1], 1, 1000)
                ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
                if ngx.arg[2] then
                    ngx.var.resp_body = ngx.ctx.buffered
                end
            '; 
 
            # 以下为以下配置均为超时配置
            proxy_connect_timeout 300;
            proxy_read_timeout 300;
            proxy_send_timeout 300;
            
            proxy_pass https://IP:端口/index.jsp; # 代理转发的地址或域名
        }

        #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;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    server {
        listen       443 ssl; # https默认端口
        server_name  xxx.com; # 域名
		
		# 以下配置为证书配置
        ssl_certificate      /data/openresty-1.15/nginx/conf/server.pem;
        ssl_certificate_key  /data/openresty-1.15/nginx/conf/server.key;

		# 缓存和超时时间配置(我没有打开注释,因为不需要https缓存)
        #ssl_session_cache    shared:SSL:1m;
        #ssl_session_timeout  5m;

    	# 以下为证书套件等配置
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        
        # 添加以下配置
        charset utf-8;
        set $resp_body "";
        access_log  /data/openresty-1.15/nginx/logs/nginx.log  main; # 配置日志路径

        location / {
            #root   html;
            #index  index.html index.htm;
            
            # 开启强制获取请求报文日志
            lua_need_request_body on;
            log_escape_non_ascii off;
            # lua
            body_filter_by_lua '
                local resp_body = string.sub(ngx.arg[1], 1, 1000)
                ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
                if ngx.arg[2] then
                    ngx.var.resp_body = ngx.ctx.buffered
                end
            ';
            
            # 以下配置均为请求超时配置
            proxy_connect_timeout 300;
            proxy_read_timeout 300;
            proxy_send_timeout 300;       
            
            proxy_pass https://IP:端口/index.jsp; # 代理转发的地址或域名
        }
    }

}

配置好后执行以下命令重启nginx(重新加载nginx的配置文件)并实时查看日志

# 在nginx的sbin目录下重启
sudo ./nginx -s reload
# 使用Xshell等工具,再另开一个窗口,去nginx下的logs目录监控nginx.log日志
tail -f nginx.log

访问你配置的域名看是否代理转发到配置的地址,然后再查看日志是否监控到最新的报文信息!

如果你访问你的域名并成功的跳转到了配置转发的地址,并在日志中看到了所有报文信息,那么恭喜你,你出师了!快去大展宏图吧~

;原文链接:https://blog.csdn.net/weixin_44170221/article/details/115595499
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐