1

Using the FastCGI module allowed me to pass on server variables directly onto my PHP application through the use of param. What I'm hoping to do next is record one of these params onto my NGINX access logs.

I've tried directly assigning this to the log format by the param's name, but it seems to result as a blank value. Later I've tried passing this param into an NGINX variable but no luck there as well.

In the following config, I've defined my own NGINX variable $logparam to store the value declared on my own custom FastCGI param LOG_PARAM, which does not seem to work that way.

If there's any possible solution to handle this, it would be very much appreciated.

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 Answer 1

0

I believe you could add the variable to a response header and log this response header.

Add variable to response header:

location ~ [^/]\.php(/|$) { add_header x-my-param "$logparam"; ... } 

And Change your 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" "$sent_http_x_my_param"'; 

I hope this works.

See also:

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

1
  • Hi @ampularius, your answer does not directly comply with my question, I was hoping to find a way of passing a FastCGI param LOG_PARAM's value into the $logparams Nginx variable, the approach you're going with doesn't cover how to pass it from a FastCGI param. And since its a FastCGI param, it's intended to be used as a server variable, so I don't wish to use a header in this scenario. Commented Mar 17, 2022 at 12:26

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.