Opened 3 years ago

Closed 2 years ago

#1051 closed defect (worksforme)

Problem with access_log whose names contain variables!

Reported by: chenjianbin@… Owned by:
Priority: major Milestone:
Component: other Version: 1.8.x
Keywords: access_log variables Cc:
uname -a: Linux localhost.localdomain 3.10.0-327.13.1.el7.x86_64 #1 SMP Thu Mar 31 16:04:38 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
nginx -V: nginx version: nginx/1.8.1 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure arguments: --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-md5=/usr/lib --with-sha1=/usr/lib --with-http_gzip_static_module --with-http_stub_status_module --with-http_realip_module --with-debug

Description

When we configure access_log directive contain variables (like this: access_log /data0/logs/nginx/$Site.access.log main_debug;) and if a request meet some conditions and uri will be rewritten to others, then this request will not be logged to access logfile. Look at following example

Here are the details of the configuration
server{

set $Site www.test.com;
listen 80;
server_name www.test.com test.com;
index index.php index.html index.htm;
root /data0/web/$Site;

location / {

if (!-e $request_filename) {

rewrite /(.*)$ /index.php/$1 last;

}

}

location ~ .+\.php($|/) {

fastcgi_pass unix:/dev/shm/php-cgi-$Site.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_split_path_info (.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;

}
access_log /data0/logs/nginx/$Site.access.log main_debug;

}

For exmaple, nginx handles a http request like http://www.test.com/aaaa ,if web root directory doesn't have aaaa file or aaaa directory,then the request uri will be rewritten and pass to PHP. whatever status code that PHP return to nginx and nginx return to user. this request can't be logged to access logfile. so if access_log directive contain variables and request rewritten,then request will not be logged.
I want to use access_log directive with variables to convenient configuration and I also need to log all request, so I don't know how to do?
My English is so bad, I don't know if you know what I mean.

Change History (9)

comment:2 in reply to: ↑ 1 Changed 3 years ago by chenjianbin@…

Replying to vbart:

JFYI: http://nginx.org/en/docs/faq/variables_in_config.html

I know use variables is costly,but I see the documentation http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log ,we can use open_log_file_cache to reduce frequency that open log file. and I also use "include" to make the configuration simple,but I also want to use variables to make configuration more simple, My true configuration is like this.

nginx.conf
server
{

set $Site www.test.com;
listen 80;
server_name www.test.com test.com;
index index.php index.html index.htm;
root /data0/web/$Site;

location / {

if (!-e $request_filename) {

rewrite /(.*)$ /index.php/$1 last;

}

}
include setup/common/*.conf;
include setup/php/thinkphp.conf;

}

setup/common/access_log.conf
access_log /data0/logs/nginx/$Site.access.log main_debug;

this "inclue"+"variables" can make the project deployment more simple,I just want to change the $Site and the server_name,I will be able to deploy a new project.

the following is error_log messages,the "/data0/web/www.test.com" is real existence and directory permissions is true. I'm thinking this is not a bug? if you need, I can paste the debug log.
2016/08/16 18:08:44 [error] 93364#0: *17 testing "/data0/web/www.test.com" existence failed (2: No such file or directory) while logging request, client: 192.168.15.1, server: www.test.com, request: "GET /asdfasdfsd HTTP/1.1", upstream: "fastcgi://unix:/dev/shm/php-cgi-www.test.com.sock", host: "www.test.com"

comment:3 follow-up: Changed 3 years ago by vbart

Could you show your full configuration, not just the server part?

comment:4 in reply to: ↑ 3 Changed 3 years ago by chenjianbin@…

Replying to vbart:

Could you show your full configuration, not just the server part?

OK,Thanks,following is my full configuration.

nginx.conf
user www www;

worker_processes auto;

pid /run/nginx.pid;
error_log /dev/null;
worker_rlimit_nofile 102400;

events {

use epoll;

worker_connections 102400;
multi_accept on;

}

http {

include mime.types;
default_type application/octet-stream;
charset utf-8;

#Logs Options

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 main_proxy '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$host"';

log_format main_product '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$host" "$request_time" "$request_length" '
'"$upstream_addr" "$upstream_response_time" "$upstream_status"';

log_format main_debug '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$host" "$request_time" "$request_length" '
'"$upstream_addr" "$upstream_response_time" "$upstream_status" "$request_body"' ;

access_log off;

#General Options

server_names_hash_bucket_size 128;
server_names_hash_max_size 256;

server_tokens off;
server_name_in_redirect off;
sendfile on;

#Keepalive Options

keepalive_timeout 60s;
keepalive_requests 100000;

#Proxy Options

proxy_read_timeout 300s;
proxy_send_timeout 300s;

#Fastcgi Options

fastcgi_connect_timeout 300s;
fastcgi_send_timeout 300s;
fastcgi_read_timeout 300s;

#Safe Options

disable_symlinks on;
client_header_timeout 20s;

#Size Limit Options

client_max_body_size 100m;

#Gzip Options

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 9;
gzip_types text/css text/xml text/plain

text/javascript application/x-javascript application/javascript
application/xml application/json;

gzip_vary on;

#Temp Opthons

proxy_temp_path /dev/shm/proxy_temp;
fastcgi_temp_path /dev/shm/fastcgi_temp;
client_body_temp_path /dev/shm/client_body_temp;
scgi_temp_path /dev/shm/scgi_temp;
uwsgi_temp_path /dev/shm/uwsgi_temp;

include vhosts/*.conf;

}

vhosts/www.test.com.conf
server
{

set $Site www.test.com;
listen 80;
server_name www.test.com test.com;
index index.php index.html index.htm;
root /data0/web/$Site;

location / {

if (!-e $request_filename) {

rewrite /(.*)$ /index.php/$1 last;

}

}
include setup/common/*.conf;
include setup/php/thinkphp.conf;

}

setup/common/access_log.conf
access_log /data0/logs/nginx/$Site.access.log main_product;

setup/php/thinkphp.conf
location ~ .+\.php($|/) {

fastcgi_pass unix:/dev/shm/php-cgi-$Site.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_split_path_info (.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;

}

comment:5 follow-up: Changed 3 years ago by vbart

Please, provide also the output of namei -nl /data0/web/www.test.com command.

comment:6 in reply to: ↑ 5 Changed 3 years ago by chenjianbin@…

Replying to vbart:

Please, provide also the output of namei -nl /data0/web/www.test.com command.

[root@localhost nginx]# namei -nl /data0/web/www.test.com
f: /data0/web/www.test.com
dr-xr-xr-x root root /
drwxr-xr-x www www data0
drwxr-xr-x www www web
drwxr-xr-x www www www.test.com

comment:7 follow-up: Changed 3 years ago by vbart

I've tested almost the same configuration and it worked fine. Please, make sure that it's not your SELinux policy prevents nginx from accessing this folder. Try to disable SELinux and test again.

comment:8 in reply to: ↑ 7 Changed 3 years ago by chenjianbin@…

Replying to vbart:

I've tested almost the same configuration and it worked fine. Please, make sure that it's not your SELinux policy prevents nginx from accessing this folder. Try to disable SELinux and test again.

I've disabled SELinux. Can you show me your configuration? I'd like to use your configuration to test it again.

comment:9 Changed 2 years ago by mdounin

  • Resolution set to worksforme
  • Status changed from new to closed

Closing this, this doesn't looks like a bug in nginx. From the logs it is clear that nginx cannot find the directory specified. It is up to debugging a particular system to find out why this happens. Likely there is either a typo hidden by trying to anonymize configuration files provided, or something weird happening at OS level.

Note: See TracTickets for help on using tickets.