Thứ Năm, 24 tháng 2, 2022

Tùy biến Nginx log và header response

Bài toàn đặt ra: Nginx default log chưa đủ để giám sát hệ thống, debug lỗi khi backend hoạt động không ổn định, hoặc chỉnh lại format để parse với ELK search log. Sau đây tôi đưa các bạn đi chỉnh sửa cơ bản nginx log và add header response.

Sơ đồ chuẩn bị:

  • Client sẽ gọi LoadBalancing ngoài cùng (thường là F5, Nginx, ALB... Có bật thêm header X-Forwarded-For để bắt IP client đang gọi)
  • Nginx hứng trước backend để tùy biến lái luồng, backend tôi sử dụng nhanh tomcat/docker

PHẦN 1: Custom log của nginx:

Đầu tiên, ta chuẩn bị 1 lệnh gọi nginx xuyên suốt quá trình để test toàn bộ các config:

 #curl -XPOST -H 'X-Forwarded-For: 199.199.199.199'  -H 'Content-Type: application/json' -H 'Host: ahihi.com' -d '{"login":"my_login","password":"my_password"}' http://localhost/index.jsp?tuanda=123

Mẫu log 1: default_log

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 with Config_1: Log ta nhận được như sau:

192.168.88.12 - - [23/Feb/2022:20:51:12 -0500] "POST /index.jsp?tuanda=123 HTTP/1.1" 200 105 "-" "curl/7.29.0" "199.199.199.199, ::1"

Mẫu log 2: request_time và response_time

  • $request_time: Thời gian phản hồi client, đơn vị milisec
  • $upstream_response_time để kiểm tra thời gian upstream phản hồi. Chỉ hiển thị khi dùng proxy_pass.
log_format  default  '$remote_addr - $remote_user [$time_local] "$request" '

                  '$status $body_bytes_sent "$http_referer" '

                  '"$http_user_agent" Request_time:$request_time Upstream_response_time:$upstream_response_time "$http_x_forwarded_for"';

Log với Config_2:

192.168.88.12 - - [23/Feb/2022:21:11:49 -0500] "POST /index.jsp?tuanda=123 HTTP/1.1" 200 4 "-" "curl/7.29.0" Request_time:0.009 UpStream_response_time:0.008 "199.199.199.199, ::1"
KQ:
- Thời gian phản hồi client là 0.009ms
- thời gian nginx đợi upstream backend trả lời là 0.008ms

Mẫu log 3: Log tiết URL và Body

log_format good_log  escape=none '$remote_addr - $remote_user [$time_iso8601] "$request" '

'$status $body_bytes_sent "$http_referer" "$http_user_agent" '

'$request_time $upstream_response_time "http_x_forwarded_for: $http_x_forwarded_for" '

'Request_body: [$request_body]';



log_format good_log2 escape=none '$remote_addr - $remote_user [$time_iso8601] '

'"$request_method $scheme://$host:$server_port$request_uri $server_protocol" '

'$status $body_bytes_sent "$http_referer" "$http_user_agent" '

'$request_time $upstream_response_time "http_x_forwarded_for: $http_x_forwarded_for" '

'Request_body: [$request_body]';

    access_log  logs/access.log  good_log;

hoặc sử dụng 
    access_log  logs/access.log  good_log2;

Log với Config_3:

192.168.88.12 - - [2022-02-23T22:21:32-05:00] "POST /index.jsp?tuanda=123 HTTP/1.1" 200 4 "-" "curl/7.29.0" 0.003 0.002 "http_x_forwarded_for: 199.199.199.199, 127.0.0.1" Request_body: [{"login":"my_login","password":"my_password"}]

với good_log2 ta có:
192.168.88.12 -  [2022-02-23T23:36:27-05:00] "POST http://ahihi.com:80/index.jsp?tuanda=123 HTTP/1.1" 200 4 "" "curl/7.29.0" 0.002 0.001 "http_x_forwarded_for: 199.199.199.199, 127.0.0.1" Request_body: [{"login":"my_login","password":"my_password"}]

Mẫu Log 4: Log as json:

log_format good_json  escape=none
  '{'
    '"time_local":"$time_iso8601",'
    '"remote_addr":"$remote_addr",'
    '"remote_user":"$remote_user",'
    '"request":"$request_method $scheme://$host$request_uri $server_protocol",'
    '"status": "$status",'
    '"body_bytes_sent":"$body_bytes_sent",'
    '"http_referer":"$http_referer",'
    '"http_user_agent":"$http_user_agent",'
    '"request_time":"$request_time",'
    '"upstream_response_time":"$upstream_response_time",'
    '"http_x_forwarded_for":"$http_x_forwarded_for",'
    '"request_body":[$request_body]'
  '}';

Kết quả log ta có như sau:

{"time_local":"2022-02-23T23:24:00-05:00","remote_addr":"192.168.88.12","remote_user":"","request":"POST http://ahihi.com/index.jsp?tuanda=123 HTTP/1.1","status": "200","body_bytes_sent":"4","http_referer":"","http_user_agent":"curl/7.29.0","request_time":"0.003","upstream_response_time":"0.001","http_x_forwarded_for":"199.199.199.199, 127.0.0.1","request_body":[{"login":"my_login","password":"my_password"}]}
--
{
  "time_local": "2022-02-23T23:24:00-05:00",
  "remote_addr": "192.168.88.12",
  "remote_user": "",
  "request": "POST http://ahihi.com/index.jsp?tuanda=123 HTTP/1.1",
  "status": "200",
  "body_bytes_sent": "4",
  "http_referer": "",
  "http_user_agent": "curl/7.29.0",
  "request_time": "0.003",
  "upstream_response_time": "0.001",
  "http_x_forwarded_for": "199.199.199.199, 127.0.0.1",
  "request_body": [
    {
      "login": "my_login",
      "password": "my_password"
    }
  ]
}

Mẫu log 5: (All) toàn bộ embed variable của nginx:

http://nginx.org/en/docs/http/ngx_http_core_module.html#variables


log_format main3 'args:$args '

' binary_remote_addr:$binary_remote_addr '

' body_bytes_sent:$body_bytes_sent '

' bytes_sent:$bytes_sent '

' connection:$connection '

' connection_requests:$connection_requests '

' connection_time:$connection_time '

' content_length:$content_length '

' content_type:$content_type '

' cookie_name:$cookie_name '

' document_root:$document_root '

' document_uri:$document_uri '

' host:$host '

' hostname:$hostname '

' http_name:$http_name '

' https:$https '

' is_args:$is_args '

' limit_rate:$limit_rate '

' msec:$msec '

' nginx_version:$nginx_version '

' pid:$pid '

' pipe:$pipe '

' proxy_protocol_addr:$proxy_protocol_addr '

' proxy_protocol_port:$proxy_protocol_port '

' proxy_protocol_server_addr:$proxy_protocol_server_addr '

' proxy_protocol_server_port:$proxy_protocol_server_port '

' query_string:$query_string '

' realpath_root:$realpath_root '

' remote_addr:$remote_addr '

' remote_port:$remote_port '

' remote_user:$remote_user '

' request:$request '

' request_body:$request_body '

' request_body_file:$request_body_file '

' request_completion:$request_completion '

' request_filename:$request_filename '

' request_id:$request_id '

' request_length:$request_length '

' request_method:$request_method '

' request_time:$request_time '

' request_uri:$request_uri '

' scheme:$scheme '

' sent_http_name:$sent_http_name '

' sent_trailer_name:$sent_trailer_name '

' server_addr:$server_addr '

' server_name:$server_name '

' server_port:$server_port '

' server_protocol:$server_protocol '

' status:$status '

' time_iso8601:$time_iso8601 '

' time_local:$time_local '

' uri:$uri ';

access_log  logs/access.log  main_z;

Log với config 5:

args:tuanda=123  binary_remote_addr:\xC0\xA8X\x0C  body_bytes_sent:4  bytes_sent:237  connection:71  connection_requests:1  connection_time:0.005  content_length:45  content_type:application/json  cookie_name:-  document_root:/opt/nginx/html  document_uri:/index.jsp  host:ahihi.com  hostname:worker-node1  http_name:-  https:  is_args:?  limit_rate:0  msec:1645669915.708  nginx_version:1.21.6  pid:87196  pipe:.  proxy_protocol_addr:-  proxy_protocol_port:-  proxy_protocol_server_addr:-  proxy_protocol_server_port:-  query_string:tuanda=123  realpath_root:/opt/nginx/html  remote_addr:192.168.88.12  remote_port:41914  remote_user:-  request:POST /index.jsp?tuanda=123 HTTP/1.1  request_body:{\x22login\x22:\x22my_login\x22,\x22password\x22:\x22my_password\x22}  request_body_file:-  request_completion:OK  request_filename:/opt/nginx/html/index.jsp  request_id:b666a73ccf0cafb68efbe7128bd9c3c3  request_length:341  request_method:POST  request_time:0.004  request_uri:/index.jsp?tuanda=123  scheme:http  sent_http_name:-  sent_trailer_name:-  server_addr:192.168.88.13  server_name:localhost  server_port:80  server_protocol:HTTP/1.1  status:200  time_iso8601:2022-02-23T21:31:55-05:00  time_local:23/Feb/2022:21:31:55 -0500  uri:/index.jsp

PHẦN 2: Debug với response header của Nginx:

Ngoài việc show các trường trong log. Ta có thể đính kèm header response cho client, bằng việc thêm header vào location như sau:


location /test/ {

add_header x0-NGINX-DEBUG '=========================================';

add_header x1-NGINX-http_user_agent $http_user_agent;

add_header xA-NGINX-http_cookie $http_cookie;

add_header xB-NGINX-request $request;

add_header xC-NGINX-request_body $request_body;

add_header xD-NGINX-request_method $request_method;

add_header xE-NGINX-request_time $request_time;

add_header xF-NGINX-request_uri $request_uri;

add_header xG-NGINX-scheme $scheme;

add_header xH-NGINX-request_server_name $server_name;

add_header xI-NGINX-request_server_port $server_port;

add_header xJ-NGINX-uri $uri;

add_header xK-NGINX-args $args;

add_header xL-NGINX-is_args $is_args;

add_header xM-NGINX-request_filename $request_filename;

add_header xN-NGINX-pid $pid;

add_header xO-NGINX-document_root $document_root;

add_header xP-NGINX-document_uri $document_uri;

add_header xQ-NGINX-host $host;

add_header xR-NGINX-hostname $hostname;

add_header xS-NGINX-proxy_protocol_addr $proxy_protocol_addr;

add_header xT-NGINX-proxy_protocol_port $proxy_protocol_port;

add_header xU-NGINX-query_string $query_string;

add_header xV-NGINX-realpath_root $realpath_root;

add_header xW-NGINX-remote_addr $remote_addr;

add_header xX-NGINX-remote_port $remote_port;

add_header xY-NGINX-remote_user $remote_user;

add_header xZ-NGINX-DEBUG '=========================================';

 proxy_pass http://127.0.0.1:8080/test/;

}

Ta thực hiện vào Browser Chrome, chọn F12 debug, chọn Network . ấn F5 để refresh lại trang. Và click vào Header để đọc:



Nguồn tham khảo:

http://nginx.org/en/docs/http/ngx_http_core_module.html#variables

https://azimut7.com/blog/nginx-variables-debug

https://nginxconfig.io/

Ngoài ra các bạn có thể bắt toàn bộ request/response giống với theo bài sauhttps://viblo.asia/p/huong-dan-cai-dat-nginx-luajit-bat-headerbody-cua-request-response-4P856LyaZY3 (tương tự với dùng tcpdump)

Thứ Hai, 15 tháng 11, 2021

Convert Excel sang CSV với xlsx2csv (có hỗ trợ UTF8)

Mình bị dí cho import vài triệu row excel tiếng Việt vào database Oracle để xử lý (Oracle Developer hỗ trợ chưa tốt phần excel với file row + column lớn). Có nhiều cách để import, dạo quanh 1 hồi chị Google đc 1 bài viết khá hay. Đã test thử, toàn bộ định dạng UTF-8 đều được bảo toàn. Bước tiếp theo import CSV vào Oracle thì dễ rồi, trong linux thì csv càng thiên biến vạn hóa hơn.

python3 -m venv venv

source venv/bin/activate

pip3 install xlsx2csv

xlsx2csv -h

xlsx2csv -d '|' file_gốc.xlsx tên_file_được_chuyển.csv


Nguồn: https://github.com/dilshod/xlsx2csv

Nguồn: https://enesi.no/2019/01/import-large-excel-files-with-sql-developer/

Thứ Bảy, 25 tháng 4, 2020

Hướng dẫn cài đặt Nginx + LuaJIT bắt Header/Body của Request-Response

Hướng dẫn cài đặt Nginx + LuaJIT bắt Header #tuanduong122
Bài toán đặt ra của mình: Nhiều lúc mình ko biết thằng vẹo Internet nào nó chèn header bậy vào hệ thống. Hoặc gặp mấy cái API đắng được bàn giao - ông đối tác bảo em cũng mới nhận code, mà ko biết nó hoạt động như nào. Mình sẽ chèn nginx (đứng trước/đứng sau) để bắt xem Header, Body chúng nó hoạt động ra sao. Từ đó ông Sys sẽ hiểu hệ thống hoạt động hơn (khổ).
Mình xin đưa nguồn tham khảo trước, mình dựa vào các hướng dẫn để tối ưu vào cài đặt của mình.
1. https://cuongquach.com/cau-hinh-nginx-ghi-log-thong-tin-header-http.html #Cường Quách , có rất nhiều bài viết hay
2. Tham khảo cấu hình: https://www.hardill.me.uk/wordpress/2018/03/14/logging-requests-and-response-with-nginx/
3. Tham khảo cài đặt: https://linuxscriptshub.com/nginx-httpguard-to-block-cc-attack-centos-6-9/  #trang web nhiều cái hay :D
4. Tham khảo cài đặt: https://tarunlalwani.com/post/building-nginx-with-lua/
Version mình sử dụng:
- Nginx-1.17.10 (Bản stable mới nhất tính đến 04.2020)
- LuaJIT-2.0.5
- lua-nginx-module-0.10.13
- ngx_devel_kit-0.3.1rc1
- Áp dụng cho cả Centos6/7. Ở đây mình đang test trên bản 7 (CentOS 6 mình cũng cài đặt thành công)

Bước 1: Cài đặt thư viện cơ bản hỗ trợ nginx:

yum install wget readline-devel pcre pcre-devel openssl openssl-devel zlib zlib-devel gcc 

Bước 2: Cài đặt / Chuẩn bị LuaJIT

- Tải LuaJIT về và giải nén
cd /opt
wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
tar zxf LuaJIT-2.0.5.tar.gz
cd LuaJIT-2.0.5

- Chèn export ở trên đầu, lưu file lại
vim Makefile
export PREFIX= /opt/luajit-2.0.5

- Thực hiện Compile LuaJIT
make 
make install
Set biến môi trường tạm thời
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0

Bước 3: Cài đặt nginx và các thư viện:

cd /opt
wget http://nginx.org/download/nginx-1.17.10.tar.gz
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.13.tar.gz
wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1rc1.tar.gz
tar -xvzf nginx-1.17.10.tar.gz 
tar -xvzf v0.10.13.tar.gz 
tar -xvzf v0.3.1rc1.tar.gz 

cd nginx-1.17.10
mkdir -p /opt/nginx
mkdir /opt/nginx/logs/
mkdir /run

- Thực hiện Compile Nginx, ở đây mình đặt Nginx ở thư mục OPT ưa thích (không phải /etc)

./configure  --sbin-path=/usr/bin/nginx --prefix=/opt/nginx --conf-path=/opt/nginx/nginx.conf --error-log-path=/opt/nginx/logs/error.log --http-log-path=/opt/nginx/logs/access.log --with-pcre --pid-path=/var/run/nginx.pid --with-http_ssl_module --with-http_realip_module --with-http_stub_status_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module  --with-ld-opt="-Wl,-rpath,/usr/local/lib" --add-module=/opt/lua-nginx-module-0.10.13 --add-module=/opt/ngx_devel_kit-0.3.1rc1
make
make install
Tạo file khởi động cho nginx (init file)
touch /etc/init.d/nginx
chmod 755 /etc/init.d/nginx
vim /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
nginx="/usr/bin/nginx"
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/opt/nginx/nginx.conf"
 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 
lockfile=/var/lock/subsys/nginx
 
make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
 
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
#    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
 
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
 
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
 
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
 
force_reload() {
    restart
}
 
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
 
rh_status() {
    status $prog
}
 
rh_status_q() {
    rh_status >/dev/null 2>&1
}
 
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac
Test nginx syntax
[root@localhost setup]# nginx -t
nginx: the configuration file /opt/nginx/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/nginx.conf test is successful
#>>>>>>> NGON, Tạm ổn
Add nginx vào systemctl và restart
/etc/init.d/nginx status
Sẽ lỗi: Unit nginx.service could not be found.
--
systemctl daemon-reload
/etc/init.d/nginx restart
Dọn rác khi cài:
mkdir /opt/setup
cd /opt
mv LuaJIT-2.0.5                 /opt/setup/
mv LuaJIT-2.0.5.tar.gz          /opt/setup/
mv lua-nginx-module-0.10.13     /opt/setup/
mv nginx-1.17.10                /opt/setup/
mv nginx-1.17.10.tar.gz         /opt/setup/
mv ngx_devel_kit-0.3.1rc1       /opt/setup/
mv v0.10.13.tar.gz              /opt/setup/
mv v0.3.1rc1.tar.gz             /opt/setup/

Bước4 : Cấu hình test LUA

(Có thể bỏ qua bước này và đi luôn vào BƯỚC 5 - ở đây mình muốn test lua hoạt động oke chưa)
Thêm location này vào nginx (có thể bỏ qua bước này và copy nguyên file config của mình ở bước dưới - BƯỚC 5):
vim /opt/nginx/nginx.conf
 location /sum {
   content_by_lua_block {
     local args = ngx.req.get_uri_args();
     ngx.say(args.a + args.b)
   }
 }
Test syntax nginx và reload lại
[root@localhost opt]# nginx -t
nginx: the configuration file /opt/nginx/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/nginx.conf test is successful

[root@localhost opt]# /etc/init.d/nginx reload
Reloading nginx configuration (via systemctl):             [  OK  ]
TEST LUA:
curl "http://localhost/sum/?a=10&b=20"
Kết quả ra : 30, vậy là Lua đã hoạt động - OK :heart_eyes:

Bước 5 : Cấu hình log Header

Cấu hình full nginx.conf
#user  nobody;
worker_processes  auto;

events {
    worker_connections  1024;
}

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

log_format log_req_resp '$remote_addr - $remote_user [$time_local] '
' "$request" $status $body_bytes_sent ${request_time}ms '
'| PRINT_REQUEST_BODY: $request_body '
'| PRINT_REQUEST_HEADER:"$req_header" '
'| PRINT_RESPONSE_HEADER:"$resp_header" '
'| PRINT_RESPONSE_BODY:"$resp_body" ';
                      
    access_log  logs/access.log log_req_resp;

    sendfile        on;
    keepalive_timeout  65;

#Cấu hình web_server listen Port_80
    server {
        listen       80;
        server_name  _;
 
 #Step1: Test Lua Sum
 location /sum {
    content_by_lua_block {
      local args = ngx.req.get_uri_args();
      ngx.say(args.a + args.b)
   }
 }



    location / {
        #Step2: Config REPSONSE_BODY
        lua_need_request_body on;
      
        set $resp_body "";
        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
        ';
      
        #Step3: Config REQUEST_HEADER, RESPONSE_HEADER
        set $req_header "";
        set $resp_header "";
        header_filter_by_lua ' 
          local h = ngx.req.get_headers()
          for k, v in pairs(h) do
              ngx.var.req_header = ngx.var.req_header .. k.."="..v.." "
          end
          local rh = ngx.resp.get_headers()
          for k, v in pairs(rh) do
              ngx.var.resp_header = ngx.var.resp_header .. k.."="..v.." "
          end
        ';  
        
         proxy_pass http://localhost:8080/;   # ở đây tôi proxy vào 1 con tomcat8 test     
        }



        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

Kiểm tra test lại nginx #nginx_syntax
[root@localhost nginx]# nginx -t
nginx: the configuration file /opt/nginx/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/nginx.conf test is successful 

[root@localhost opt]# /etc/init.d/nginx reload
Reloading nginx configuration (via systemctl):             [  OK  ]
Bước: Test POST chả hạn
[root@localhost ROOT]# curl -X POST  -H "Host:tuanduong122.wordpress.com" -H "Tuanda:hehehe" -d '{"DUONGANHTUAN_BODY":"123"}' localhost
Kết quả: APACHE TOMCAT8 BODY
vào đọc log accesss:
tail /opt/nginx/logs/access.log
127.0.0.1 - - [24/Apr/2020:19:04:26 -0400]  "POST / HTTP/1.1" 200 20 0.346ms | PRINT_REQUEST_BODY: {\x22DUONGANHTUAN_BODY\x22:\x22123\x22} | PRINT_REQUEST_HEADER:"host=tuanduong122.wordpress.com content-type=application/x-www-form-urlencoded tuanda=hehehe accept=*/* content-length=27 user-agent=curl/7.29.0 " | PRINT_RESPONSE_HEADER:"content-length=20 set-cookie=JSESSIONID=C94254D1F47723462F60222FE865D9FB; Path=/; HttpOnly content-type=text/html;charset=ISO-8859-1 connection=keep-alive " | PRINT_RESPONSE_BODY:"APACHE TOMCAT8 BODY\x0A"
Phân tích Kết quả:
PRINT_REQUEST_BODY: {\x22DUONGANHTUAN_BODY\x22:\x22123\x22}
PRINT_REQUEST_HEADER:"host=tuanduong122.wordpress.com content-type=application/x-www-form-urlencoded tuanda=hehehe accept=/ content-length=27 user-agent=curl/7.29.0 "
PRINT_RESPONSE_HEADER:"content-length=20 set-cookie=JSESSIONID=C94254D1F47723462F60222FE865D9FB; Path=/; HttpOnly content-type=text/html;charset=ISO-8859-1 connection=keep-alive "
PRINT_RESPONSE_BODY:"APACHE TOMCAT8 BODY\x0A"

Bonus_1: Lua Show Request Body as Response

     location = /request_body {
         client_max_body_size 50k;
         client_body_buffer_size 50k;

         content_by_lua_block {
             ngx.req.read_body()  -- explicitly read the req body
             local data = ngx.req.get_body_data()
             if data then
                 ngx.say("body data:")
                 ngx.print(data)
                 return
             end

             -- body may get buffered in a temp file:
             local file = ngx.req.get_body_file()
             if file then
                 ngx.say("body is in file ", file)
             else
                 ngx.say("no body found")
             end
         }
     }
[root@tuanda conf]# curl -X GET -d '{test:123}' localhost/request_body
body data:
{test:123}

Thứ Hai, 11 tháng 11, 2019

[Other] 4 giây - 2 phút - 72 giờ và 21 ngày: Công thức kì diệu giúp bạn đạt mọi mục tiêu và không bao giờ bị trì hoãn

Trước khi bắt đầu đọc bài viết này, bạn hãy dành một chút thời gian để suy nghĩ xem: Đã bao nhiêu lần bạn lập ra danh sách các mục tiêu mà mình muốn thực hiện? Chắc chắn, số lần đó luôn nhiều hơn những hành động thực tế bạn làm để đạt được mục tiêu.
Luật 4 giây
Lời khuyên: Hãy hít thở thật sâu và chậm 4 giây trước khi bạn hành động hoặc đưa ra bất cứ quyết định quan trọng nào.
Trong cuộc sống , chúng ta thường có xu hướng trì hoãn việc đưa ra những quyết định quan trọng. Điều này có vẻ như một gánh nặng quá lớn, do vậy chúng ta muốn chờ thêm thời gian. Cho đến khi bắt buộc phải đưa ra quyết định, chúng ta sẽ quyết định nhất thời và thông thường sau đó sẽ là sự hối tiếc.
Nguyên tắc 4 giây sẽ giúp bạn đưa ra những quyết định tốt hơn. Peter Bregman, tác giả của cuốn sách “Luật 4 giây” cho rằng bạn nên hít thở thật đều và sâu trong vòng 4 giây; sau đó bạn có thể hành động.
Tại sao việc này quan trọng? Đó chính là nghệ thuật tự kiểm soát. Hít thở sâu sẽ tránh cho bạn khỏi việc đưa ra các quyết định vội vã và mang lại cho bạn thời gian để đánh giá kết quả của mỗi hành động.
Luật 2 phút

Lời khuyên: Nếu việc nào đó chỉ tốn chưa đến 2 phút để hoàn thành, bạn hãy thực hiện nó ngay lập tức.
Rất nhiều nhiệm vụ chúng ta trì hoãn thường không khó để hoàn thành. Chúng ta thường tránh làm chúng cho đến hạn chót, mặc dù nó không đòi hỏi kỹ năng hay kiến thức đặc biệt. Chẳng hạn, bạn cần phải gọi điện cho đối tắc hoặc gửi một email. Việc này chỉ mất 1 đến 2 phút nhưng bạn lại trì hoãn cho đến phút cuối cùng. Nhiệm vụ đơn giản này làm bạn bị chùn bước và mất tập trung.
Nguyên tắc 2 phút này sẽ giúp bạn đạt được những mục tiêu lớn. Mỗi mục tiêu đi kèm với một danh sách các hành động nhỏ. Chẳng hạn, bạn phải đọc 1 cuốn sách khoảng 2.000 trang, việc này sẽ khiến bạn mất vài tháng. Nếu như bạn tiếp tục nhìn cuốn sách như một tổng thể, bạn sẽ thấy nó khó mà hoàn thành, do đó bạn dễ bỏ cuộc. Nhưng nếu như bạn chia nhỏ cuốn sách thành từng trang và đặt mục tiêu đọc từng trang một, bạn sẽ mất chưa đến 2 phút cho một trang sách.
Luật 72 giờ

Lời khuyên: Khi bạn đã có 1 ý tưởng, hãy thực hiện nó trong vòng 3 ngày (72 giờ).
Bodo Schaefer – tác giả sách, doanh nhân kiêm diễn giả nổi tiếng người Đức cho rằng nguyên tắc đơn giản này sẽ giúp bạn đẩy lùi trì hoãn: Đừng bao giờ đặt ra các nhiệm vụ kéo dài quá 72 giờ. Nếu bạn trì hoãn hành động này, ý tưởng của bạn sẽ mãi nằm trên giấy mà thôi.
Luật 21 ngày

Lời khuyên: Hãy cho bản thân 21 ngày để phát triển một thói quen
Khi bạn muốn đạt được một mục tiêu nào đó, bạn phải biến các hành động thành thói quen hàng ngày. Hãy lập lại danh sách mục tiêu một lần nữa, tập trung vào các mục tiêu đơn lẻ và biến nó thành hành động, sau đó thực hiện đều đặn mỗi ngày. Đó có thể là viết blog, ngồi thiền, chạy bộ hay học tiếng Anh… bất cứ việc gì.
Trong những ngày đầu tiên, bạn sẽ cần rất nhiều nỗ lực. Nhưng một khi đã quen dần với nó, bạn sẽ cảm thấy điều này như một phần cuộc sống.
Nguyên tắc 10.000 giờ

Lời khuyên: Khi bạn cố gắng để thành thạo trong lĩnh vực nào đó, bạn cần dành khoảng 10.000 giờ thực hành việc đó.
Trong cuốn sách “Outliers: The Story of Succes” (Tạm dịch: Những kẻ xuất chúng), tác giả người Canada Malcolm Gladwell đã đề cập đến nguyên tắc 10.000 giờ thực hành đóng góp vào thành công của hầu hết các doanh nhân, triệu phú trên thế giới.
Để thực hiện quy tắc này, bạn hãy lựa chọn lĩnh vực mà mình yêu thích và lập kế hoạch để thực hành mỗi ngày. Hãy theo dõi thời gian mà bạn thực hành việc đó để đảm bảo bạn thực hiện đủ 10.000 giờ.

Debug exec pod with no any command support

  Một ngày đẹp trời pod bị lỗi. Bạn thử ngay lệnh "kubectl exec -it ..." vào pod kiểm tra. Nhưng quãi đạn, pod không hỗ trợ bất kỳ...