使用快速CGI模块允许我通过使用参数。我接下来希望将其中一个参数记录到我的 NGINX 访问日志中。
我尝试直接通过参数名称将其分配给日志格式,但结果似乎为空值。后来我尝试将此参数传递给 NGINX 变量,但同样没有成功。
在以下配置中,我定义了自己的 NGINX 变量$log 参数存储在我自己的自定义 FastCGI 参数上声明的值日志参数,但这种方法似乎行不通。
如果有任何可能的解决方案来解决这个问题,我们将非常感激。
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Adding nginx variable to access log format
log_format docker '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$logparam"';
include conf.d/*.conf;
map $http_upgrade $connection_upgrade {
default "upgrade";
}
server {
listen 80 default_server;
access_log /var/log/nginx/access.log docker;
# Declared variable to store fastcgi param
set $logparam "";
client_header_timeout 60;
client_body_timeout 60;
keepalive_timeout 60;
gzip off;
gzip_comp_level 4;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
location / {
try_files $uri $uri/ /index.php?_url=$uri&$args;
}
location ~ [^/]\.php(/|$) {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index /index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
include /etc/nginx/fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Declared new param with custom value and passing onto nginx variable
fastcgi_param LOG_PARAM "testing";
set $logparam LOG_PARAM;
}
location ~ /\.ht {
deny all;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
access_log off;
}
}
}
答案1
我相信您可以将变量添加到响应头并记录该响应头。
将变量添加到响应头:
location ~ [^/]\.php(/|$) {
add_header x-my-param "$logparam";
...
}
并更改您的日志格式:
log_format docker '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$sent_http_x_my_param"';
我希望这能起作用。
也可以看看:
https://nginx.org/en/docs/http/ngx_http_log_module.html#log_format
https://nginx.org/en/docs/http/ngx_http_core_module.html#variables