nginx典型官方模块解释
模块名称 | 作用 | 语法 | 默认 | 配置位置 | 配置举例 | 结果验证 | 备注 |
1 --with-http_stub_status_module | 监控Nginx的服务器连接状态 | stub_status | server、location这一级来配置 | location = /mystatus{ stub_status; } |
此时访问http://127.0.0.1/mystatus 即可查看现在有nginx现在有多少连接了 |
||
2 --with-http_random_index_module | 目录中选择随机一个页面作为主页 | random_index on|off | location这一级来配置 | location / { root /usr/share/nginx/html; #index index.html index.htm; random_index on; } |
不会随机到隐藏文件 | ||
3 --with-http_sub_module | HTTP内容替换 | 1、sub_filter '要替换的字符' '替换后的字符' 2、sub_filter_last_modified on|off 3、sub_filter_once on|off |
http、server、location这一级来配置 | location / { root /usr/share/nginx/html; #index index.html index.htm; random_index on; sub_filter '1' '2'; sub_filter_once off; } |
2、用于服务端和浏览器端进行每一次请求的时候校验服务端是否有发生过变更 3、为on时只替换第一个匹配到的; 为off时替换全部匹配到的 |
||
4 -limit_conn_module | 连接频率限制 |
1、limit_conn_zone key zone=name:size; key:以key为依据来限制频率(例:key为源IP) name:保存连接数需要一块内存,name为这块内存地址 size:name这块内存的大小 其余为关键字 2、limit_conn zone number; zone:为1中的name number:限制的大小(例:限制源IP为1.1.1.1的并发为1000) |
1、http这一级来配置 2、http、server、location这一级来配置 |
http{ limit_conn_zone $remote_addr zone=abc:10m; server { location / { root /usr/share/nginx/html; index index.html index.htm; limit_conn zone zone=bcd 1; } } } |
实验没做成功,NND | 一个TCP连接为一个连接,在一个TCP连接里可以有多个HTTP连接 (即:TCP三次握手一次后就可以发送多个http请求了。即:单路复用) |
|
5 -limit_req_module | 请求频率限制 |
1、limit_req_zone key zone=name:size rate=rate key、name、size同limit_conn_module rate:以秒为单位的频率(例:每秒100次请求) 2、limit_req zone=name [burst=number][nodelay] name:为1中的name [burst=number]:选填,number为客户端在超过速率后的前number个放到下一秒执行 [nodelay]:选填,超过速率的直接返回503 |
1、http这一级来配置 2、http、server、location这一级来配置 |
http{ limit_req_zone $remote_addr zone=bcd:1m rate=1r/s; server { location / { root /usr/share/nginx/html; index index.html index.htm; limit_req zone=bcd burst=3 nodelay; } } } |
限定每秒请求为1时,在1秒内访问2次的话第二次返回503. (小米抢手机估计就是用的这套路) |
||
6 -http_access_module | 基于IP的访问控制 | 1、allow address|CIDR|unix:|all 2、deny address|CIDR|unix:|all |
http、server、location、limit_except这一级来配置 | location ~ ^/admin.html { root /var/log/html/; deny 172.20.163.127; allow all; } |
172.20.163.127访问主页时会返回403,其他正常 | ~ ^/admin.html:代表要访问admin.html这个页面时去/var/log/html/取找。 管理后台只对指定IP开放可以用这个方法 |
|
7 -http_auth_basic_module | 基于用户的信任登陆 | 1、auth_basic string|off; 2、auth_basic_user_file file; |
http、server、location、limit_except这一级来配置 |
location / { root /usr/share/nginx/html; index index.html index.htm; auth_basic 'input password:'; auth_basic_user_file /passwd.txt; } htpasswd -c /passwd.txt liwei ====>使用htpasswd生成保存账号的文件,普通文本nginx不识别 |
auth_basic string|off;这里的string输入一个字符串后也当on使, 输入一个字符串后在登陆提示框会将该字符串显示出来(相当于提示语) |
||
8 文件读取 | 文件读取 | sendfile on|off ===>(提高读取静态文件效率。直接通过系统内核将文件放入socket,不必再打开一遍) | http、server、location、if in location这一级来配置 |
location / { root /usr/share/nginx/html; index index.html index.htm; sendfile on } |
|||
9 数据传输 | 数据传输 |
1、tcp_nopush on|off ===>(senffile开启的情况下,提高数据包的传输效率。即:攒够一定量的包再一起发送,而不是来一个包发一个包) 2、tcp_nodelay on|off ===>(长连接下(keepalive),提高数据包传输实时性。即:来一个包发一个包。适用于对网络实时性要求比较高的场景) |
1、http、server、location这一级来配置 |
location / { root /usr/share/nginx/html; index index.html index.htm; tcp_nopush on tcp_nodelay on } |
|||
10 数据压缩 | 数据压缩 |
1、gzip on|off |
1、http、server、location、if in location这一级来配置 |
location ~ .*\.(jpg|gif|png)$ { gzip off; gzip_http_version 1.1; gzip_comp_level 2; gzip_types application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; root /opt/app/code/images; } |
1、2、3、4步组合成一个功能 | ||
11 缓存时间设置 | 缓存时间设置 |
expires [modified] time; |
server、location、if in location这一级来配置 | ||||
12 防盗链设置 | 防盗链(本质是依据refere来限制) |
valid_referers none|blocked|string... |
server、location这一级来配置 |
location /download { gzip_static off; tcp_nopush off; alias /opt/app/code; valid_referers none blocked; if ($invalid_referer){ return 403; } } |
curl -e "http://172.20.163.127" -I http://172.20.163.99/download/2.txt(成功) |
$invalid_referer:如果valid_referers后面没有值则$invalid_referer为真 | |
13 代理服务 | 反向代理 | proxy_pass URL | location、if in location、limit_except这一级来配置 |
server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location ~ \.php{ proxy_pass http://172.20.163.135:80; root index.html; } |
此时访问http://nginx服务器/1.php 相当于访问了http://172.20.163.135/1.php | ||
14 代理服务 | 正向代理 |
proxy_pass http://$http_host$request_uri; |
resolver 114.114.114.114; location / { proxy_pass http://$http_host$request_uri; } |
此时挂好代理就可以访问http的网页了,但是不能访问https网页 | |||
代理的扩展 | 缓冲区 | proxy_buffering on|off | proxy_buffering on | http、server、location这一级来配置 |
location / { proxy_pass http://127.0.0.1:8080; proxy_redirect default; proxy_set_header Host $http_host; ===>要访问的目的主机 proxy_set_header X-Real-IP $remote_addr; ===>客户端真实IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ==>如果使用代理访问的话使用此方式可获取代理链 proxy_connect_timeout 30; proxy_send_timeout 60; proxy_read_timeout 60; proxy_buffer_size 32k; proxy_buffering on; proxy_buffers 4 128k; proxy_busy_buffers_size 256k; proxy_max_temp_file_size 256k; } |
尽可能的将请求信息接收完再将数据包统一转发出去 | |
代理的扩展 | 跳转重定向 |
proxy_redirect default|off |
proxy_redirect default | http、server、location这一级来配置 |
反向代理时后端服务器发来301报文时不是把301转发给客户端,而是根据301再去访问被重定向到的地址,拿到最终数据后再返回给客户端 |
||
代理的扩展 | 修改头信息 |
proxy_set_header field value 扩展:proxy_hide_header、proxy_set_body |
http、server、location这一级来配置 |
proxy_set_header X-Real-IP $remote_addr; |
在经过中间这层代理后,后端server就拿不到最初源的一些信息了(比如说真实源IP)。 |
||
代理的扩展 | Nginx作为代理到后端server的超时 |
proxy_connect_timeout time 扩展:proxy_read_timeout、proxy_send_timeout |
proxy_connect_timeout 60s | http、server、location这一级来配置 |
TCP连接超时 扩展:TCP已经建立的基础上等待回应的时间 测试的时候没测出来有什么效果 |
||
15 负载均衡 |
upstream name{ server IP|域名 端口 属性; } location / { proxy_pass http://name; } |
http这一级来配置 |
upstream imooc { ip_hash; server 172.20.163.135:80 weight=5; server cctv.com:80; server 172.20.163.126:80 backup; server 172.20.163.123:80 down; server 172.20.163.111:80 max_fails 5; server 172.20.163.33:80 fail_timeout 60s; server 172.20.163.42:80 max_conns 1024; } location / { proxy_pass http://imooc; } |
访问http://Nginx地址/时流量会负载均衡到135、123、126这三台设备上 |
后端服务器在负载均衡调度中的状态: 调度算法(默认为轮询): |
||
16 rewrite |
跳转重定向 |
rewrite 正则表达式 replacement[flag] | server、location、if一级来配置 |
1、 location /down { rewrite ^/down http://www.cctv.com permanent; } 2、 location / { rewrite ^/down /test/abc.html permanent; root /opt/work; } |
1、访问http://Nginx地址/down时将跳转至http://www.cctv.com |
正则表达式中()用于匹配括号之间的内容,通过$1,$2调用 flag标志位: rewrite加在不同位置时的优先级规则:server > location |
|
17 HTTPS | HTTPS |
ssl on|off |
http、server这一级来配置 |
server{ listen 443; server_name hk.com; keepalive_timeout 100; ===>优化之使用长连接,100s ssl on; ssl_certificate /etc/nginx/ssl_key/hk1.crt; ssl_certificate_key /etc/nginx/ssl_key/hk.key; ssl_session_timeout 10m; ===>优化之SSL会话过期,10分钟 ssl_session_cache shared:SSL:10m; ===>优化之使用SSL缓存,大小为10M,可以存储大约8k到10k的会话 location / { root /opt/app/code; index index.html index.htm; } } |
证书生成步骤: 一、生成秘钥 二、生成证书 苹果终端对服务端的要求: |
||
18 try_files | 相当于if语句(如果这个路径下找不到则匹配另一个location) | try_files $uri @其他location | location一级来配置 |
location / { root /usr/share/nginx/html; index index.html index.htm; try_files $uri @code1; } location @code1 { proxy_pass http://www.cctv.com; } |
此时访问http://nginx服务器/1.html时会先去/usr/share/nginx/html/目录找, |
测试时候发现location里面有try_files后index语句将失效,即访问http://nginx服务器/时 |
|
19 worker_processes | 制定nginx的work进程数(不包含manage进程) | worker_processes 进程数 | 最开头那级来配置 |
worker_processes 10 |
建议和物理CPU核心数相同 | ||
20 worker_rlimit_nofile | 修改nginx最大句柄 | worker_rlimit_nofile 句柄数 | 最开头那级来配置,和worker_processes 1;是一级 | worker_rlimit_nofile 65535; |
文件句柄的理解:程序打开一个本地文件产生一个文件句柄,默认操作系统只允许一个应用程序打开最多1024个文件 感觉只有nginx做web服务器时才需要调文件句柄,因为做反向代理的时候也不总是打开本地文件啊 linux修改文件句柄最大数(默认为1024): |
||
21 CPU亲和 | 将所有进程均匀的分布在各个cpu核心上 | worker_cpu_affinity auto; | 最开头那级来配置,和worker_processes 1;是一级 |
user nginx; |
[root@localhost conf.d]# ps -eo pid,args,psr | grep [n]ginx 9062 nginx: master process nginx 19 9371 nginx: worker process 0 9372 nginx: worker process 1 9373 nginx: worker process 2 9374 nginx: worker process 3 9375 nginx: worker process 4 9376 nginx: worker process 5 9377 nginx: worker process 6 9378 nginx: worker process 7 9379 nginx: worker process 8 9380 nginx: worker process 9 9381 nginx: worker process 10 9382 nginx: worker process 11 9383 nginx: worker process 12 9384 nginx: worker process 13 9385 nginx: worker process 14 9386 nginx: worker process 15 9387 nginx: worker process 16 9388 nginx: worker process 17 9389 nginx: worker process 18 9390 nginx: worker process 19 9391 nginx: worker process 20 9392 nginx: worker process 21 9393 nginx: worker process 22 9394 nginx: worker process 23 |
没有配置worker_cpu_affinity auto;时的CPU占用情况: [root@localhost conf.d]# ps -eo pid,args,psr | grep [n]ginx 9062 nginx: master process nginx 22 9406 nginx: worker process 5 9407 nginx: worker process 0 9408 nginx: worker process 9 9409 nginx: worker process 2 9410 nginx: worker process 1 9411 nginx: worker process 3 9412 nginx: worker process 11 9413 nginx: worker process 0 9414 nginx: worker process 12 9415 nginx: worker process 6 9416 nginx: worker process 2 9417 nginx: worker process 5 9418 nginx: worker process 15 9419 nginx: worker process 9 9420 nginx: worker process 17 9421 nginx: worker process 0 9422 nginx: worker process 7 9423 nginx: worker process 2 9424 nginx: worker process 8 9425 nginx: worker process 9 9426 nginx: worker process 5 9427 nginx: worker process 0 9428 nginx: worker process 6 9429 nginx: worker process 2 |
nginx典型官方模块解释的更多相关文章
- Nginx详解七:Nginx基础篇之Nginx官方模块
Nginx官方模块 --with-http_stub_status_module:Nginx的客户端状态,用于监控连接的信息,配置语法如下:配置语法:stub_status;默认状态:-配置方法:se ...
- nginx利用geo模块做限速白名单以及geo实现全局负载均衡的操作记录
geo指令使用ngx_http_geo_module模块提供的.默认情况下,nginx有加载这个模块,除非人为的 --without-http_geo_module.ngx_http_geo_modu ...
- Nginx 的 RTMP 模块的在线统计功能 stat 在 multi-worker 模式下存在 Bug
< 让你的 Nginx 的 RTMP 直播具有统计某频道在线观看用户数量的功能>一文介绍了 Nginx 的在线统计模块. 我们的在线直播服务使用了 Nginx 的 Rtmp ...
- nginx上传模块nginx_upload_module和nginx_uploadprogress_module模块进度显示,如何传递GET参数等。
ownload:http://www.grid.net.ru/nginx/download/nginx_upload_module-2.2.0.tar.gzconfigure and make : . ...
- Nginx配置文件具体配置解释
Nginx配置文件具体配置解释 #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8; #全局错 ...
- Nginx访问限制模块limit_conn_zone 和limit_req_zone配置使用
nginx可以通过limit_conn_zone 和limit_req_zone两个组件来对客户端访问目录和文件的访问频率和次数进行限制,另外还可以善用进行服务安全加固,两个模块都能够对客户端访问进行 ...
- Nginx HttpSubModule sub_filter模块的过滤功能
Nginx HttpSubModule sub_filter模块的过滤功能 发表于2年前(2013-08-05 10:39) 阅读(1481) | 评论(0) 0人收藏此文章, 我要收藏 赞0 5 ...
- Debian 为nginx增加第三方模块
为nginx增加第三方模块需要重新编译nginx的,但是debian在安装nginx的时候做了很多事情,比如systemd,/etc/nginx/里的各种文件,所以我们最好在debian源代码包的基础 ...
- Nginx:HTTP过滤模块
参考资料<深入理解Nginx> HTTP过滤模块也是一种HTTP模块,与普通HTTP处理模块不同在于: 1.一个请求仅由一个HTTP处理模块处理,而可以被任意个HTTP过滤模块处理 2.普 ...
随机推荐
- z390 m.2 接口插上sata 硬盘后,机械硬盘不识别;HDD 硬盘不识别;z390 m.2和 SATA 硬盘安装组合;
今天,在集成z390芯片组的主板上,安装了一块m.2支持 sata协议的ssd时,发现安装上ssd后,之前机械硬盘不识别了:还以为机械硬盘烧了: 在网上查找相关博客可以发现,是M.2 SATA 和 S ...
- JUC-1-volatile
什么是volatile关键字 volatile是轻量级同步机制,与synchronized相比,他的开销更小一些,同时安全性也有所降低,在一些特定的场景下使用它可以在完成并发目标的基础上有一 ...
- poj 3069 Saruman's Army 贪心 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=3069 题解 题目可以考虑贪心 尽可能的根据题意选择靠右边的点 注意 开始无标记点 寻找左侧第一个没覆盖的点 再来推算既可能靠右的标记点为一 ...
- 一个页面从输入url到加载到内容,这个过程经历了什么
首先,当浏览器接收到url,会查看本地缓存(浏览器缓存-系统缓存-路由器缓存)中是否有,有则直接显示 没有则进行DNS域名解析,将域名解析成IP地址,通过ip地址去访问相应的服务器, 浏览器访问服务器 ...
- JavaScript中一个对象数组按照另一个数组排序
JavaScript中一个对象数组按照另一个数组排序 需求:排序 const arr1 = [33, 11, 55, 22, 66]; const arr2 = [{age: 55}, {age: 2 ...
- commons-httpclient 和 httpclient 区别
今天在看项目的pom的时候,发现里面有这么两个包依赖. <dependency> <groupId>commons-httpclient</groupId> < ...
- sql server 2008清除日志
先改成简单模式,再清除日志,再改回原来模式 USE [oms20190322]GOALTER DATABASE oms20190322 SET RECOVERY SIMPLE WITH NO_WAIT ...
- java(一)基础知识
常见DOS命令: dir:列出当前目录下的文件以及文件夹 md:创建目录 rd: 删除目录 cd:进入指定目录 cd .. :返回上一级目录 cd \:返回根目录 del:删除文件 exit:退出do ...
- Vs Code 2019软件安装教程及常用的入门设置
小编认为VsCode是一款非常好用的编辑器,插件丰富,支持的语言种类非常多.我所使用VsCode主要打一些前端的代码,自己感觉very good. 点击运行. 按图所示操作. 安装教程很简单的,主要是 ...
- Apollo的基本概念和集成实战
基本概念 使用场景 是一个分布式的配置中心.适用于微服务: 核心功能 集中管理不同环境,不同集群的配置: 配置修改后可以实时推送到应用端: 具备规范的权限,流程治理特性: 开发技术 服务端使用spri ...