English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
1. First, check the nginx version, I use1.9.7version, installed 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
Two, check the syntax 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
Three, delete the extra comment 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
Four, add the following tags and content within the server tag of the nginx configuration file
location /logs { alias /application/nginx-1.9.7/logs; # Nginx log directory autoindex on; # Open directory browsing function autoindex_exact_size off; # Default is on, show the exact size of the file, the unit is bytes # Show the approximate size of the file, the unit is kB or MB or GB autoindex_localtime on; # Default is off, the displayed file time is GMT time. # Set to on after, the displayed file time is the server time of the file add_header Cache-Control no-store; # Make the browser not save temporary files }
Five, enable opening log files in the browser. If not enabled, clicking on the file will download instead of opening
[root@AnSheng conf]# vim mime.types types { text/html html htm shtml; text/log log; text/css css; text/xml xml; .............
Six, check the syntax and then make the nginx configuration take effect, and 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, followed by logs, and then click the file to open it. If the logs can be viewed so casually by others, isn't it very unsafe? Therefore, we need to add an additional layer of Nginx user authentication.
Chapter 7: Install httpd-tools, used for account and password generation
[root@AnSheng ~]# yum -y install httpd-tools
Chapter 8: Create an authenticated account
[root@AnSheng ~]# htpasswd -c /application/nginx-1.9.7/conf/loguser loguser New password: Re-type new password: Adding password for user loguser #Password needs to be entered twice
Chapter 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 Authentication auth_basic_user_file /application/nginx-1.9.7/conf/loguser; #File for saving authentication account and password }
Chapter 10: When you open it again, it will prompt you to enter the account and password, and you can view it after logging in.
Chapter 11: Summary
This is the complete procedure to enable real-time browser access to Nginx logs, hoping it will be helpful for your learning or work. If you have any questions, you can leave comments for communication.