电子商务网站建设试题,wordpress耍留言,海外平台推广,经开区网站建设文章目录Nginx目录索引Nginx配置Nginx状态监控**Nginx访问控制****Nginx访问限制****Nginx请求限制****连接限制和请求限制#xff0c;哪个会更有效**Nginx日志配置log_format access_log**Nginx虚拟站点**Nginx LocationLocation 语法示例Location 语法优先级排列Location应用…文章目录Nginx目录索引Nginx配置Nginx状态监控**Nginx访问控制****Nginx访问限制****Nginx请求限制****连接限制和请求限制哪个会更有效**Nginx日志配置log_formataccess_log**Nginx虚拟站点**Nginx LocationLocation 语法示例Location 语法优先级排列Location应用场景Nginx目录索引目录索引模块简述ngx_http_autoindex_module 以/结尾的请求生成目录列表当ngx_http_index_module模块找不到索引文件的时候会把请求传递给ngx_http_autoindex_moduleNginx配置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_basicaccess 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 TemporarilyUnavailableNginx请求限制语法limit_req_zone key zonename:size raterate; httplimit_conn zone number [burstnumber] [nodelay]; http, server, location连接限制和请求限制哪个会更有效多个请求可以建立在一次的TCP连接之上, 那么我们对请求的精度限制当然比对一个连接的限制会更加的有效因为同一时刻只允许一个TCP连接请求进入。但是同一时刻多个http请求可以通过一个TCP连接进入。所以请求限制才是比较优的解决方案Nginx日志配置Nginx 有非常灵活的日志记录模式。每个级别的配置可以有各自独立的访问日志。日志格式通过log_format 命令定义格式。log_format配置语法: 包括: error.log access.logSyntax: log_format name [escapedefault|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_logExample: server{... access_log /var/log/nginx/www.server.com.log;Nginx虚拟站点所谓虚拟主机及在一台服务器上配置多个网站如: 公司主页、博客、论坛看似三个网站, 实则可以运行在一台服务器上。配置的方式有3种基于不同域名基于不同端口、基于不同的别名具体操作可以看Nginx虚拟主机实验Nginx Location使用 Nginx Location 可以控制访问网站的路径, 但一个 server 可以有多个 location 配置, 多个 location 的优先级该如何区分Location 语法示例location [|^||*|!|!~*|/] /uri/ { … }Location 语法优先级排列案例测试[rootweb01 conf.d]# cat test.confserver{listen80;server_name www.jy.com;location /{default_type text/html;return200location /;}location/{default_type text/html;return200location /;}location ~ /{default_type text/html;return200location ~/;}}访问域名看是哪个内容#最高优先级[rootweb01 ~]# curl www.jy.comlocation/#注释掉精确匹配, 重启Nginx[rootweb01 ~]# cat /etc/nginx/conf.d/test.confserver{listen80;server_name www.jy.com;location /{default_type text/html;return200location /;}# location / {# default_type text/html;# return 200 location /;# }location ~ /{default_type text/html;return200location ~/;}}[rootweb01 ~]# systemctl restart nginx[rootweb01 ~]# curl www.jy.comlocation ~/[Location应用场景1、通用匹配任何请求都会匹配到 location /{}2、严格区分大小写匹配以.php结尾的都走这个location