文章目录
- Nginx目录索引
- Nginx配置
- Nginx状态监控
- **Nginx访问控制**
- **Nginx访问限制**
- **Nginx请求限制**
- **连接限制和请求限制,哪个会更有效**
- Nginx日志配置
- log_format
- access_log
- **Nginx虚拟站点**
- Nginx Location
- Location 语法示例
- Location 语法优先级排列
- Location应用场景
Nginx目录索引
目录索引模块简述
ngx_http_autoindex_module 以/结尾的请求,生成目录列表,当ngx_http_index_module模块找不到索引文件的时候会把请求传递给ngx_http_autoindex_module
Nginx配置
NGINX默认是不允许列出整个目录进行浏览下载的
Syntax: autoindex on | off;
Default: autoindex off;
Context: http, server, location
#autoindex常用参数autoindex_exact_size off;默认为on, 显示出文件的确切大小,单位是bytes。 修改为off,显示出文件的大概大小,单位是kB或者MB或者GB。 autoindex_localtime on;默认为off,显示的文件时间为GMT时间。 修改为on, 显示的文件时间为文件的服务器时间。 charset utf-8,gbk;默认中文目录乱码,添加上解决乱码。#配置站点目录浏览功能location /{root html;autoindex on;autoindex_localtime on;autoindex_exact_size off;charset utf-8,gbk;}Nginx状态监控
ngx_http_stub_status_module 用于展示 Nginx 连接状态信息, 需要 --with-http_stub_status_module 模块支持
语法:
Syntax: stub_status;
Default: —
Context: server, location
配置Nginx status location /nginx_status{stub_status;access_log off;}访问到status的结果一般为 Active connections:2server accepts handled requests4461Reading:0Writing:1Waiting:1Active connections# 当前活动的TCP连接数accepts4# 当前的TCP总连接数handled4# 成功的TCP连接数requests61# 总的http请求数Reading# 请求Writing# 响应Waiting# 等待的请求数,开启了keepalive# 注意, 一次TCP的连接,可以发起多次http的请求, 如下配置参数可验证keepalive_timeout0;# 类似于关闭长连接keepalive_timeout65;# 65s没有活动则断开连接Nginx访问控制
基于IP的访问控制 http_access_module
基于用户登陆认证 http_auth_basic_module
#Nginx 基于 IP 的访问控制语法 Syntax: allow address|CIDR|unix:|all;Default: — Context: http, server, location, limit_except //拒绝配置语法 Syntax: deny address|CIDR|unix:|all;Default: — Context: http, server, location, limit_except 案例 访问控制配置示例, 拒绝指定的IP, 其他全部允许 location /nginx_status{stub_status;access_log off;deny10.0.0.1;allow all;}只允许谁能访问, 其它全部拒绝 location /{root html;index index.html index.htm;allow10.0.0.0/24;allow127.0.0.1;deny all;}基于用户登陆认证 //配置语法 Syntax: auth_basic string|off;Default: auth_basic off;Context: http, server, location, limit_except //用户密码记录配置文件 Syntax: auth_basic_user_filefile;Default: - Context: http, server, location, limit_except 安装依赖组件 yuminstallhttpd-tools 在http,server,location下添加如下信息 auth_basic"access auth,input your password!";auth_basic_user_file /etc/nginx/auth_conf;Nginx访问限制
服务器流量异常,负载过大等等。对于大流量恶意的攻击访问, 会带来带宽的浪费,服务器压力,影响业务,所以对同一个 IP 的连接数,并发数进行限制,ngx_http_limit_conn_module 模块可以根据定义的 key 来限制每个键值的连接数,如同一个 IP 来源的连接数。
limit_conn_module 连接频率限制
limit_req_module 请求频率限制
如果共享内存空间被耗尽,服务器将会对后续所有的请求返回503错误(Service Temporarily
Unavailable)
Nginx请求限制
语法:limit_req_zone key zone=name:size rate=rate; (http)
limit_conn zone number [burst=number] [nodelay]; (http, server, location)
连接限制和请求限制,哪个会更有效
多个请求可以建立在一次的TCP连接之上, 那么我们对请求的精度限制,当然比对一个连接的限制会更加的有效
因为同一时刻只允许一个TCP连接请求进入。
但是同一时刻多个http请求可以通过一个TCP连接进入。
所以请求限制才是比较优的解决方案
Nginx日志配置
Nginx 有非常灵活的日志记录模式。每个级别的配置可以有各自独立的访问日志。日志格式通过log_format 命令定义格式。
log_format
配置语法: 包括: error.log access.log
Syntax: log_format name [escape=default|json] string …;
Default: log_format combined “…”;
Context: http
#默认Nginx定义日志语法log_format main'$remote_addr-$remote_user[$time_local] "$request" ''$status$body_bytes_sent"$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';# Nginx日志格式允许包含的变量:$remote_addr# 记录客户端IP地址$remote_user# 记录客户端用户名$time_local# 记录通用的本地时间$time_iso8601# 记录ISO8601标准格式下的本地时间$request# 记录请求的方法以及请求的http协议$status# 记录请求状态码(用于定位错误信息)$body_bytes_sent# 发送给客户端的资源字节数,不包括响应头的大小$bytes_sent# 发送给客户端的总字节数$msec# 日志写入时间。单位为秒,精度是毫秒。$http_referer# 记录从哪个页面链接访问过来的$http_user_agent# 记录客户端浏览器相关信息$http_x_forwarded_for#记录客户端IP地址$request_length# 请求的长度(包括请求行, 请求头和请求正文)。$request_time# 请求花费的时间,单位为秒,精度毫秒# 注:如果Nginx位于负载均衡器,nginx反向代理之后, web服务器无法直接获取到客户端真实的IP地址。 $remote_addr获取的是反向代理的IP地址。反向代理服务器在转发请求的http头信息中,增加x-forwarded-for信息,用来记录客户端IP地址和客户端请求的服务器地址access_log
Example: server{... access_log /var/log/nginx/www.server.com.log;Nginx虚拟站点
所谓虚拟主机,及在一台服务器上配置多个网站,如: 公司主页、博客、论坛看似三个网站, 实则可以运行在一台服务器上。配置的方式有3种,基于不同域名,基于不同端口、基于不同的别名,具体操作可以看Nginx虚拟主机实验
Nginx Location
使用 Nginx Location 可以控制访问网站的路径, 但一个 server 可以有多个 location 配置, 多个 location 的优先级该如何区分
Location 语法示例
location [=|^||*|!|!~*|/] /uri/ { … }
Location 语法优先级排列
案例测试
[root@web01 conf.d]# cat test.confserver{listen80;server_name www.jy.com;location /{default_type text/html;return200"location /";}location=/{default_type text/html;return200"location =/";}location ~ /{default_type text/html;return200"location ~/";}}访问域名,看是哪个内容#最高优先级=[root@web01 ~]# curl www.jy.comlocation=/#注释掉精确匹配=, 重启Nginx[root@web01 ~]# cat /etc/nginx/conf.d/test.confserver{listen80;server_name www.jy.com;location /{default_type text/html;return200"location /";}# location =/ {# default_type text/html;# return 200 "location =/";# }location ~ /{default_type text/html;return200"location ~/";}}[root@web01 ~]# systemctl restart nginx[root@web01 ~]# curl www.jy.comlocation ~/[Location应用场景
1、通用匹配,任何请求都会匹配到 location /{}2、严格区分大小写,匹配以.php结尾的都走这个location