1. First check the nginx version. I am using version 1.9.7. The installation directory is /application/nginx-1.9.7
[root@ansheng ~]# /application/nginx-1.9.7/sbin/nginx -v nginx version: nginx/1.9.7 built by gcc 4.4.7 20120313 (red hat 4.4.7-16) (gcc) configure arguments: --prefix=/application/nginx-1.9.7 --user=nginx --group=nginx --with-http_stub_status_module
2. Check Grammar and start nginx
[root@ansheng ~]# /application/nginx-1.9.7/sbin/nginx -t nginx: the configuration file /application/nginx-1.9.7/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.9.7/conf/nginx.conf test is successful [root@ansheng ~]# /application/nginx-1.9.7/sbin/nginx
3. Delete the redundant attention lines and blank lines in the nginx configuration file
[root@ansheng ~]# cd /application/nginx-1.9.7/conf/ [root@ansheng conf]# egrep -v "#|^$" nginx.conf.default worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; 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; } } } [root@ansheng conf]# egrep -v "#|^$" nginx.conf.default nginx.conf
4. In nginx Add the following tags and content to the server tag of the configuration file
location /logs { alias /application/nginx-1.9.7/logs; #nginx日志目录 autoindex on; #打开目录浏览功能 autoindex_exact_size off; #默认为on,显示出文件的确切大小,单位是bytes #显示出文件的大概大小,单位是kb或者mb或者gb autoindex_localtime on; #默认为off,显示的文件时间为gmt时间。 #改为on后,显示的文件时间为文件的服务器时间 add_header cache-control no-store; #让浏览器不保存临时文件 }
5. Enable the log file to be opened in the browser. If it is not enabled, it will be downloaded instead of opened when you click the file
[root@ansheng conf]# vim mime.types types { text/html html htm shtml; text/log log; text/css css; text/xml xml; .............
6. Check the syntax, then let the nginx configuration take effect, view it in the browser
[root@ansheng conf]# /application/nginx-1.9.7/sbin/nginx -t nginx: the configuration file /application/nginx-1.9.7/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.9.7/conf/nginx.conf test is successful [root@ansheng conf]# /application/nginx-1.9.7/sbin/nginx -s reload
Open the browser and enter the domain name or IP, add logs after it, and then click The file can be opened. Is it unsafe if the log can be viewed by others casually, so we need to add a layer of nginx user authentication.
##7. Install httpd-tools for account and password generation
[root@ansheng ~]# yum -y install httpd-tools
8. Create a certified account
[root@ansheng ~]# htpasswd -c /application/nginx-1.9.7/conf/loguser loguser new password: re-type new password: adding password for user loguser #密码需要输入两次
9. Edit the nginx configuration file and add the following content to the logs location
location /logs { ...... alias path; autoindex on; autoindex_exact_size off; autoindex_localtime on; add_header cache-control no-store; auth_basic "restricted"; #nginx认证 auth_basic_user_file /application/nginx-1.9.7/conf/loguser; #认证账号密码保存的文件 }
10 , and then when you open it again, you will be prompted to enter your account number and password, and you can view it only after logging in.
The above is the detailed content of How does Nginx enable the browser to view access logs in real time?. For more information, please follow other related articles on the PHP Chinese website!