Devops's Blog

Nginx собираем тело из запроса

Задача

1 изменить метод запроса на POST
2 из query параметров (динамическое количество) сформировать тело POST запроса
3 проксировать траффик на другой сервер(http://router.ru:8080/httpapi)

Пример

Пришел запрос

curl --location --request GET 'http://12345678.ru/track/i.gif?user_id=12345678&type_mail=new_registration_company&user_type=contact&aa1=bb1&aa2=bb2&cc1=bb3'

Тело которое формирует nginx, где api_key и event_type постоянные значения , user_id береться из запроса, с type_mail начинается динамическое формирование тела

api_key=12345678&event={ "user_id":"12345678", "event_type":"open mail" ,"event_properties":{"user_type":"contact","aa1":"bb1","cc1":"bb3","aa2":"bb2","type_mail":"new_registration_company"}}

Устанавливаем nginx от openresty с поддержкой lua


wget https://openresty.org/package/centos/openresty.repo
mv openresty.repo /etc/yum.repos.d/
yum install openresty
yum install openresty-resty
		    

Добавляем в nginx.conf строку для формата логирования для тестирования


log_format log_body '[$time_local] "$request" "$request_body"';

nginx.conf


user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;


events {
    worker_connections 4000;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    log_format log_body '[$time_local] "$request" "$request_body"';

    access_log  /var/log/nginx/access.log  main;

    #############################################################
    open_file_cache off;
    #open_file_cache          max=1000 inactive=20s;
    #open_file_cache_valid    30s;
    #open_file_cache_min_uses 2;
    #open_file_cache_errors   on;
    #############################################################

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    types_hash_max_size 2048;

    client_body_buffer_size 1024K;
    client_max_body_size 40M;


    client_header_buffer_size 16k;
    large_client_header_buffers 8 32k;

    keepalive_timeout  30;

    gzip on;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_proxied any;
    gzip_types text/plain text/css text/javascript application/javascript application/json application/x-javascript text/xml application/xml image/svg+xml;
    proxy_max_temp_file_size 0;

    include             /usr/local/openresty/nginx/conf/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /usr/local/openresty/nginx/conf/conf.d/*.conf;

}

Основной конфиг


server {
    listen    127.0.0.1:80
    server_name  12345678.ru;
    lua_need_request_body on;


 location / {
     rewrite_by_lua_block {
         local user_id_vrb
         local body_vrb = 'api_key=12345678&event={ "user_id":"vrb1", "event_type":"open mail" ,"event_properties":{'
         local args = ngx.req.get_uri_args()
         for key, val in pairs(args) do
             if key == "user_id" then
                 user_id_vrb=val
             else
                 body_vrb = body_vrb..'"'..key..'":"'..val..'",'
             end
         end
         body_vrb = string.gsub(body_vrb, "vrb1",user_id_vrb)
         body_vrb = string.sub(body_vrb, 0, #body_vrb-1)
         body_vrb = body_vrb .. "}}"
         ngx.req.set_body_data(body_vrb)
         ngx.req.set_uri_args("")
         ngx.req.set_uri("/httpapi")
         ngx.req.set_method(ngx.HTTP_POST)
         ngx.req.set_header("Content-Type", "text/plain")
     }
     proxy_pass http://router.ru:8080;
     access_log  /opt/log/nginx/stat_profi_api_s7_ru_access.log log_body;
 }

}


Смотрим лог nginx, видим что тело формируется


[17/Feb/2023:21:58:36 +0300] "GET /track/i.gif?user_id=12345678&type_mail=new_registration_company&user_type=contact&aa1=bb1&aa2=bb2&cc1=bb3 HTTP/1.1" "api_key=12345678&event={ \x22user_id\x22:\x22QY110041\x22, \x22event_type\x22:\x22open mail\x22 ,\x22event_properties\x22:{\x22type_mail\x22:\x22new_registration_company\x22,\x22user_type\x22:\x22contact\x22,\x22aa1\x22:\x22bb1\x22,\x22cc1\x22:\x22bb3\x22,\x22aa2\x22:\x22bb2\x22}}"