nginx--自定义日志&&跳转&&长连接&&文件缓存&&状态页

自定义日志服务

[root@localhost ~]# cat /apps/nginx/conf/conf.d/pc.conf 
server {
  listen 80;
  server_name www.fxq.com;
  error_log /data/nginx/logs/fxq-error.log info;
  access_log /data/nginx/logs/fxq-access.log main;
  location / {
     root /data/nginx/html/pc;
     index index.html;
  }
}

 要打开main

 生成日志

 页面不存在跳转服务

直接跳转到首页

[root@localhost ~]# cat /apps/nginx/conf/conf.d/pc.conf
server {
  listen 80;
  server_name www.fxq.com;
  location / {
     root /data/nginx/html/pc;
     index index.html;
     try_files $uri /index.html;
  }
}

 

 匹配uri目录下面的index.html文件

[root@localhost ~]# cat /apps/nginx/conf/conf.d/pc.conf
server {
  listen 80;
  server_name www.fxq.com;
  location / {
     root /data/nginx/html/pc;
     index index.html;
     try_files $uri $uri/index.html /index.html;
  }
}

 

补全文件名

[root@localhost ~]# cat /apps/nginx/conf/conf.d/pc.conf
server {
  listen 80;
  server_name www.fxq.com;
  location / {
     root /data/nginx/html/pc;
     index index.html;
     try_files $uri $uri.html /index.html;
  }
}

 返回状态码

[root@localhost ~]# cat /apps/nginx/conf/conf.d/pc.conf
server {
  listen 80;
  server_name www.fxq.com;
  location / {
     root /data/nginx/html/pc;
     index index.html;
     try_files $uri  =123;
  }
}

长连接配置

keepalive_timeout number; 设定保持连接超时时⻓长,0表示禁止长连接,默认为75s,通常配置在http字段作为站点全局配置
keepalive_requests number; #在一次长连接上所允许请求的资源的最大数量,默认为100次

keepalive_requests 3;
keepalive_timeout 65 65;

 开启长连接后,返回客户端的会话保持时间为65s,单次长连接累计请求达到指定次数请求或65秒就会被断开,后面的65s为发送给客户端应答报文头部中显示的超时间设置为65s:如不设置客户端将不显示超时时间

 使用telnet测试

telnet www.fxq.com 80

 2次后断开

 文件缓存

可配置在http,server,location

open_file_cache off; 是否缓存打开过的文件信息

open_file_cache max=N [inactive=time];

max=N:可缓存的缓存项上限数量;达到上限后会使用LRU(Least recently used,最近最少使⽤)算法实现管理理
inactive=time:缓存项的非活动时⻓长,在此处指定的时长内未被命中的或命中的次数少于
open_file_cache_min_uses指令所指定的次数的缓存项即为⾮非活动项,将被删除

nginx可以缓存以下三种信息:
(1) 文件元数据:⽂文件的描述符、文件大小和最近⼀一次的修改时间
(2) 打开目录结构
(3) 没有找到的或者没有权限访问的文件的相关信息

open_file_cache_errors on | off;是否缓存查找时发生错误的文件一类的信息 默认值为off

open_file_cache_min_uses number;
open_file_cache指令的inactive参数指定的时长内,至少被命中此处指定的次数方可被归类为活动项默认值为1

open_file_cache_valid time;缓存项有效性的检查验证频率,默认值为60s

[root@localhost ~]# cat /apps/nginx/conf/conf.d/pc.conf
server {
  listen 80;
  server_name www.fxq.com;
  open_file_cache max=10000 inactive=60s; #最⼤大缓存10000个⽂文件,⾮非活动数据超时时⻓长60s
  open_file_cache_valid 60s; #每间隔60s检查⼀一下缓存数据有效性
  open_file_cache_min_uses 5; #60秒内⾄至少被命中访问5次才被标记为活动数据
  open_file_cache_errors on; #缓存错误信息
  server_tokens off; #隐藏Nginx server版本。
  location / {
     root /data/nginx/html/pc;
     index index.html;
     allow 12.168.33.179;
  }
}

状态页

基于nginx模块ngx_http_stub_status_module实现,在编译安装nginx的时候需要添加编译参数--with-http_stub_status_module,否则配置完成之后监测会是提示语法错误。

location /nginx_status {
           stub_status;
           allow 192.168.13.118;
           allow 127.0.0.1;
           deny all;
        } 

 

Active connections: 当前处于活动状态的客户端连接数,包括连接等待空闲连接数。
accepts:统计总值,Nginx自启动后已经接受的客户端请求的总数。
handled:统计总值,Nginx自启动后已经处理完成的客户端请求的总数,通常等于accepts,除非有因worker_connections限制等被拒绝的连接。
requests:统计总值,Nginx自启动后客户端发来的总的请求数。
Reading:当前状态,正在读取客户端请求报文首部的连接的连接数。
Writing:当前状态,正在向客户端发送响应报文过程中的连接数。
Waiting:当前状态,正在等待客户端发出请求的空闲连接数,开启 keep-alive的情况下,这个值等于active – (reading+writing),

状态⻚页⽤用于输出nginx的基本状态信息:
输出信息示例例:
Active connections: 291
server accepts handled requests
16630948 16630948 31070465
上⾯面三个数字分别对应accepts,handled,requests三个值
Reading: 6 Writing: 179 Waiting: 106

相关推荐

  1. nginx日志定义和统计处理

    2024-05-02 15:54:06       15 阅读
  2. Android 带html标签文本添加定义超链接

    2024-05-02 15:54:06       18 阅读
  3. 【Golang】定义logrus日志保存为日志文件

    2024-05-02 15:54:06       29 阅读
  4. Nginx定义状态码499出现原因

    2024-05-02 15:54:06       12 阅读
  5. Nginx 配置,定义日志格式 log_format

    2024-05-02 15:54:06       19 阅读
  6. 查看nginx日志文件

    2024-05-02 15:54:06       22 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-02 15:54:06       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-02 15:54:06       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-02 15:54:06       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-02 15:54:06       20 阅读

热门阅读

  1. Nuxt3路由跳转

    2024-05-02 15:54:06       12 阅读
  2. docker 搭建 zipkin

    2024-05-02 15:54:06       11 阅读
  3. 复现论文5:UMI

    2024-05-02 15:54:06       8 阅读
  4. 2024-05-02 问AI: 介绍一下IMDB数据集

    2024-05-02 15:54:06       12 阅读
  5. 2024-5-2——雇佣 K 名工人的最低成本

    2024-05-02 15:54:06       10 阅读
  6. Web Workers 介绍

    2024-05-02 15:54:06       12 阅读
  7. 贪心算法基础题(第三十四天)

    2024-05-02 15:54:06       9 阅读
  8. 8、Python:字符串类型

    2024-05-02 15:54:06       14 阅读
  9. STM32学习和实践笔记(24):PWM输出实验:呼吸灯

    2024-05-02 15:54:06       11 阅读
  10. 函数式接口 Consumer、Function、Supplier、Predicate

    2024-05-02 15:54:06       10 阅读
  11. systemctl开启自动启动特定docker服务

    2024-05-02 15:54:06       9 阅读
  12. mac上 完全清除新安装的python3环境

    2024-05-02 15:54:06       12 阅读