One, scene reconstruction
1, the configuration file /nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include sites-enabled/*.conf;
}
2, a single project profiles /nginx/conf/sites-enabled/jean.conf
server {
listen 80;
server_name www.jean.com;
root /mine/serve/project;
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}
access_log logs/access.jean.log main;
location / {
index index.html;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
}
location ~ /\.ht {
deny all;
}
location ~ \.sh$ {
deny all;
}
}
3, test configuration
[root@192 sbin]# ./nginx -t
nginx: the configuration file /mine/serve/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /mine/serve/nginx/conf/nginx.conf test is successful
4, access to domain name
normal output!
5, view the log
does not generate a log file
[root@192 logs]# ls
access.log error.log nginx.pid
Second, identify problems
1, when the project profiles /nginx/conf/sites-enabled/jean.conf
#可疑代码行
=-----------------------------------------------------=
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}
=-----------------------------------------------------=
Note: The above code means that all access is jpg | jpeg | gif | png, css | js | ico, html file, not written to the log, the cache is to maintain maximum!
2, problem solving
comment out the "access_log off;" this line: "# access_log off;", and then restart nginx service
and will nginx.conf file, define log_format command line to open (remove the preceding "#")
=-----------------------------------------------------------------------=
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
=-----------------------------------------------------------------------=
3, once again visit the domain name, view the log
and sure enough, log generation
[root@192 logs]# ls
access.jean.log access.log error.log nginx.pid
[root@192 logs]# ll
总用量 12
-rw-r--r--. 1 root root 194 11月 23 09:58 access.jean.log
-rw-r--r--. 1 root root 0 11月 23 03:12 access.log
-rw-r--r--. 1 root root 1442 11月 23 09:58 error.log
-rw-r--r--. 1 root root 4 11月 23 03:12 nginx.pid
The content is written! !
4, it may be argued that the "access_log logs / access.jean.log main;" put [the line] # suspicious code above, can not I? No, creates a access.jean.log file, however, does not write content!
Third, the problem summary
About log error, generally there are two problems:
1, is on the log, but did not generate a log, this situation, first of all, make sure you have not turned on the log command line, such as "access_log logs / access.jean.log main;" , there are, then look at your main configuration file nginx.conf and project profiles "/nginx/conf/sites-enabled/jean.conf" there is no "access_log off;" command line, if so, commented, restart nginx service generally no problem.
2, reported "nginx: [emerg]" log_format "directive is allowed here in /mine/serve/nginx/conf/sites-enabled/jean.conf:13 not" this error, commonly defined "log_format" must be nginx .conf file, the default is annotated to open about this format, you can also customize the configuration file in nginx.conf, then, in your project configuration file.
3 In addition, the log format "log_format" definition, can not be defined in the project configuration file, such as "/nginx/conf/sites-enabled/jean.conf", otherwise, there will be an error!