Nginx基础之日志相关/访问控制/虚拟机配置

nginx 日志文件详解

​ nginx 日志文件分为 log_formataccess_log 两部分

​ log_format 定义记录的格式,其语法格式为

​ log_format 样式名称 样式详情

配置文件中默认有

log_format  main  'remote_addr - remote_user [time_local] "request" '
                  'status body_bytes_sent "$http_referer" '
                  '"http_user_agent" "http_x_forwarded_for"';
[root@localhost ~]# vim /etc/nginx/nginx.conf
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;
#ng配置文件里面有一段是可以自定义日志输出的内容.
点击这里 点击这里
变量 说明
$remote_addr和$http_x_forwarded_for 客户端的ip
$remote_user 客户端的名称
$time_local 访问时的本地时间
$request 请求的URL和http协议
$status 访问的状态码
$body_bytes_sent 发送给客户端的主体内容大小
$http_referer 记录客户端是从哪个页面链接访问过来的,若没有链接,则访问‘-’
$http_user_agent 记录客户端使用的浏览器的相关信息
[root@localhost ~]# tailf /var/log/nginx/access.log 
#打开ng的访问日志文件.
192.168.10.1 - TNT [19/May/2020:16:05:57 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"
#这里面的日志内容输出的格式就是上面配置文件里面定义的.
#意思是日志输出显示的内容可以自定义.

6、nginx 高级应用 访问控制

1、使用 alias 实现虚拟目录
location /tiger {
    
    	alias /var/www/tiger; 
    	index index.html;    #访问http://x.x.x.x/tiger时实际上访问是/var/www/tiger/index.html
[root@localhost ~]# vim /etc/nginx/nginx.conf

  location /tiger {
   
          alias /var/www/tiger;
          index index.html index.htm;
        }

#打开配置文件.
#里面新增一段访问路径,然后浏览器访问的时候访问http://x.x.x.x/tiger时实际上访问是/var/www/tiger/index.html

[root@localhost ~]# cat /var/www/tiger/index.html 
111111111111111111111:

在这里插入图片描述

2、通过 stub_status 模块监控 nginx 的工作状态
1、通过 nginx -V 命令查看是否已安装 stub_status 模块
2、编辑 /etc/nginx/nginx.conf 配置文件
#添加以下内容~~ 
location /nginx-status {
    
      stub_status on; 
      access_log    /var/log/nginx/nginxstatus.log;    #设置日志文件的位置 
      auth_basic    "nginx-status";    #指定认证机制(与location后面的内容相同即可) 
      auth_basic_user_file    /etc/nginx/htpasswd;     #指定认证的密码文件 
      }      
3、创建认证口令文件并添加用户 lzs 和 zsgg,密码用md5加密

安装htpasswd: yum -y install httpd-tools

htpasswd -c -m /etc/nginx/htpasswd tiger # -c 创建解密文件,-m MD5加密
htpasswd -m /etc/nginx/htpasswd zsgg

htpasswd 是开源 http 服务器 apache httpd 的一个命令工具,用于生成 http 基本认证的密码文件。

4、重启服务
5、客户端访问 http://x.x.x.x/nginx-status 即可
[root@localhost ~]# vim /etc/nginx/nginx.conf
 location /nginx-status {
   
                stub_status on;
                access_log    /var/log/nginx/nginxstatus.log;    #设置日志文件的位置 
                auth_basic    "nginx-status";    #指定认证机制(与location后面的内容相同即可) 
                auth_basic_user_file    /etc/nginx/htpasswd;     
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值