nginx之web服务器 页面配置

本文详细介绍了如何在Nginx中自定义错误页面,包括404错误、错误码指定、文件检测机制、日志位置设置、长连接优化、作为下载服务器的配置以及用户上传限制。还涉及了Nginx缓存管理的open_file_cache相关设置。
摘要由CSDN通过智能技术生成

4.3.8 自定义 错误页面

我们 可以改变 默认的错误页面,同时也可以用指定的响应状态码进行响应, 可用位置:http, server, location, if in location

格式:

error_page code ... [=[response]] uri;
页面错误代码  
error_page    固定写法
code          响应码
=             可以将响应码转换
uri           访问连接

错误页面404报错

实验1:错误页面

去真机中验证

server{
        listen 80;
        server_name www.mcb.com;
        root /data/html/;
        error_page 404 /index.html;
        location /status {
        stub_status;
        auth_basic    "welcome mcb";
        #提示信息,不是所有浏览器都有用
        auth_basic_user_file   /mnt/.nginxuser;
      }
}

实验2:自定义错误页面

server{
        listen 80;
        server_name www.mcb.com;
        root /data/html/;
        error_page 404 /40x.html;
        location = /40x.html {
        root /mnt/error;
         }
        location /status {
        stub_status;
        auth_basic    "welcome mcb";
        auth_basic_user_file   /mnt/.nginxuser;
      }
}

去真机验证

实验3:把错误码 404 指定成302  
server{
        listen 80;
        server_name www.lucky.com;
        root /data/html/;
        error_page 404 =302 /40x.html;
        location = /40x.html {
        root /mnt/error;
         }
        location /status {
        stub_status;
        auth_basic    "welcome cxk";
        auth_basic_user_file   /mnt/.nginxuser;
      }
}

4.3.10 检测文件是否存在

try_files会按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如果所有文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。只有最后一个参数可以引起一个内部重定向,之前的参数只设置内部URI的指向。最后一个参数是回退URI且必须存在,否则会出现内部500错误。

语法格式

Syntax: try_files file ... uri;
try_files file ... =code;
Default: —
Context: server, location

实验:

server{
        listen 80;
        server_name www.mcb.com;
        root /data/html/;
        location / {
        root /data;
        try_files $uri $uri.html $uri/index.html /about/default.html;
}
}

注意:兜底有固定格式 

检测

4.3.9 日志位置存放

格式:


Syntax: error_log file [level];
error_log    /apps/nginx/logs/kgc_error.log;
固定格式      文件路径                         级别(info  debug等  可以忽略不写)

注意:前面兜底会影响实验,需删除 

实验1:自定义错误日志的位置

还没有结束
实验:将两个网站的 日志分离
[root@localhost error]#vim /apps/nginx/conf.d/m.conf
server{
        listen 80;
        server_name  www.m.com;
        root /data/nginx/m/;
        error_log    /data/logs/m_error.log;
        access_log  /data/logs/m_access.log;
}
 
 
[root@localhost error]#vim /apps/nginx/conf.d/pc.conf
server{
        listen 80;
        server_name  www.pc.com;
        root /data/nginx/pc;
        error_log    /data/logs/pc_error.log;
        access_log  /data/logs/pc_access.log;
}
[root@localhost error]#mkdir /data/logs
[root@localhost error]#nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@localhost error]#nginx -s reload
 
查看日志是否生效

4.3.11 长连接

http 基于 tcp 协议 先要 三次握手然后 再传输数据

一次三次握手 下载多个资源

一次三次握手下载一个资源

相关设置:

keepalive_timeout timeout [header_timeout];  
#设定保持连接超时时长,0表示禁止长连接,默认为75s,通常配置在http字段作为站点全局配置
keepalive_requests number;  
#在一次长连接上所允许请求的资源的最大数量,默认为100次,建议适当调大,比如:500
可以加在全局或者 server 

例子

​keepalive_requests 3;
#最大下载三个资源就会断开
keepalive_timeout 60 65;   #只能有一个空格   #版本不一可能不一样时间
#开启长连接后,返回客户端的会话保持时间为60s,单次长连接累计请求达到指定次数请求或65秒就会被断开,后面的60为发送给客户端应答报文头部中显示的超时时间设置为60s:如不设置客户端将不显示超时时间。
Keep-Alive:timeout=60  #浏览器收到的服务器返回的报文

二者默认长连接

keepalive_timeout 0;

keepalive_timeout 50;

Connection:close  #浏览器收到的服务器返回的报文
#使用命令测试:telnet

B 对哪种浏览器禁用长连接

keepalive_disable none | browser ...;  
#对哪种浏览器禁用长连接

4.3.12 作为下载服务器配置

ngx_http_autoindex_module 模块处理以斜杠字符 "/" 结尾的请求,并生成目录列表,可以做为下载服务

配置使用

官方文档

http://nginx.org/en/docs/http/ngx_http_autoindex_module.html

配置:

[root@localhost ~]#cd /opt
[root@localhost opt]#ls
image  nginx-1.18.0  nginx-1.18.0.tar.gz  rh
[root@localhost opt]#cd nginx-1.18.0/
[root@localhost nginx-1.18.0]#./configure --help |grep auto
  --without-http_autoindex_module    disable ngx_http_autoindex_module

精心一记

autoindex on | off;
#自动文件索引功能,默为off
autoindex_exact_size on | off;  
#计算文件确切大小(单位bytes),off 显示大概大小(单位K、M),默认on
autoindex_localtime on | off ; 
#显示本机时间而非GMT(格林威治)时间,默认off
autoindex_format html | xml | json | jsonp; 
#显示索引的页面文件风格,默认html
limit_rate rate; 
#限制响应客户端传输速率(除GET和HEAD以外的所有方法),单位B/s,即bytes/second,默认值0,表示无限制,此指令由ngx_http_core_module提供
set $limit_rate
#变量提供 限制   变量优先级高

总结看错误日志

实验1:

server{
   listen 80;
   server_name www.lucky.com;
   root /data/html/;
   location /download {
   autoindex on;
   root /mnt/;
}
}

 

目录添加文件

用谷歌浏览器检测访问:

实验2:给文件内容加单位大小
server{
   listen 80;
   server_name www.mcb.com;
   root /data/html/;
   location /download {
   autoindex on;
   autoindex_exact_size off;
   root /mnt/;
}
}

用谷歌浏览器检测

location /download {
       autoindex on;
       #开启下载服务器
       autoindex_exact_size on;
       #开启确切大小不建议开启
       autoindex_localtime on;
       #使用当地时间
       limit_rate 1024k;
       #所有人限速1024k,默认单位是字节数
       set $limit_rate 2M;
       #谁先生效
       alias /opt/download;
  }
实验3:显示索引的页面文件风格,默认html   把它修改为 json
server{
   listen 80;
   server_name www.mcb.com;
   root /data/html/;
   location /download {
   autoindex on;
   autoindex_exact_size off;
   autoindex_format json;
   root /mnt/;
}
}

验证:

4.3.13 用户上传资料

上传需要借助开发小的程序, 并且程序 5M 和 nginx 10M 都会限制。 两者取最小

client_max_body_size 1m; 
#设置允许客户端上传单个文件的最大值,默认值为1m,上传文件超过此值会出413错误
client_body_buffer_size size; 
#用于接收每个客户端请求报文的body部分的缓冲区大小;默认16k;超出此大小时,其将被暂存到磁盘上的由下面client_body_temp_path指令所定义的位置
client_body_temp_path path [level1 [level2 [level3]]];
#设定存储客户端请求报文的body部分的临时存储路径及子目录结构和数量,目录名为16进制的数字,使用hash之后的值从后往前截取1位、2位、2位作为目录名
 
上传文件大于限制  错误代码413

4.3.14 其他设置

directio size | off;

#操作完全和aio相反,aio是读取文件而directio是写文件到磁盘,启用直接I/O,默认为关闭,当文件大于等于给定大小时,例如:directio 4m;同步(直接)写磁盘,而非写缓存。
 
直接 写入 磁盘     还是等待一定数据量写入磁盘
open_file_cache off;  #是否缓存打开过的文件信息
open_file_cache max=N [inactive=time];
#nginx可以缓存以下三种信息:
(1) 文件元数据:文件的描述符、文件大小和最近一次的修改时间
(2) 打开的目录结构
(3) 没有找到的或者没有权限访问的文件的相关信息 
max=N:#可缓存的缓存项上限数量;达到上限后会使用LRU(Least recently used,最近最少使用)算法实现管理
inactive=time:#缓存项的非活动时长,在此处指定的时长内未被命中的或命中的次数少于
 
 
 
open_file_cache_min_uses    
#指令所指定的次数的缓存项即为非活动项,将被删除 
open_file_cache_valid time; 
#缓存项有效性的检查验证频率,默认值为60s 
open_file_cache_errors on | off; 
#是否缓存查找时发生错误的文件一类的信息,默认值为off
open_file_cache_min_uses number; 
#open_file_cache指令的inactive参数指定的时长内,至少被命中此处指定的次数方可被归类为活动项,默认值为1
 
范例:
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;
#缓存错误信息
 
 
 
limit_except method ... { ... },仅用于location
#限制客户端使用除了指定的请求方法之外的其它方法 
method:GET, HEAD, POST, PUT, DELETE,MKCOL, COPY, MOVE, OPTIONS, PROPFIND, 
PROPPATCH, LOCK, UNLOCK, PATCH
limit_except GET {
 allow 192.168.91.101;
 deny all;
}
#除了GET和HEAD 之外其它方法仅允许192.168.1.0/24网段主机使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值