在北京生活,路上遇到很多和我們擦肩而過的人,哪怕看他平平無奇,你記住,他們一定有故事~
大家好,我是奈何,老規矩,我們先聽故事,后學知識!

不知從何說起的我,開始支支吾吾的,嗯…~(那就從這里開始吧,)
來到北京后,最近專案經理給了我很多需求,竟然是讓我干運維的活兒,其實我也應該為此而感到欣慰,畢竟想做一名的優秀的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://www.uj5u.com/qita/275096.html
標籤:其他
上一篇:【Linux 2】常用指令
