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这一级来配置
2、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
2、gzip_http_version 1.1|1.0
3、gzip_comp_level 级别
4、gzip_types image/png

   

1、http、server、location、if in location这一级来配置
2、http、server、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;
expires epoch|max|off;

   server、location、if in location这一级来配置      
 12 防盗链设置  防盗链(本质是依据refere来限制)  

valid_referers none|blocked|string...
valid_referers为关键字,后跟允许从哪些地址过来访问(即允许的refer)
none代表允许没有带refer信息的过来访问
blocked代表允许不带协议信息的refer过来访问(即cctv.com,前面没有http://信息)
string代表支持哪些refer来访问(例:refer=http://qq.com, string指的是qq.com这部分)(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(成功)
curl -e "http://www.qq.com" -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;
$http_host代表要访问的主机名
$request_uri代表要访问的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 redirect replacement

proxy_redirect default http、server、location这一级来配置  

反向代理时后端服务器发来301报文时不是把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时增加X-Real-IP头部,值为$remote_addr

在经过中间这层代理后,后端server就拿不到最初源的一些信息了(比如说真实源IP)。
为了解决这个问题可以用这个方法为数据包再加一个映射的头信息,好让后端server知道真实的源信息

代理的扩展 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这三台设备上

后端服务器在负载均衡调度中的状态:
down:当前的server暂时不参与负载均衡
backup:预留的备份服务器(其他主机全部down掉它起来)
max_fails:允许请求失败的次数(健康检查)
fail_timeout:经过max_fails失败后,服务暂停的时间
max_conns:限制后端server最大的接收连接数


调度算法(默认为轮询):
轮询:按时间顺序逐一分配到不同的后端服务器
加权轮询:weight值越大,分配到的概率越高(只要后面加了weight就自动从轮询变为加权轮询)
ip_hash:每个请求按访问的源IP的hash结果分配,这样可以保证一个源IP的每次访问固定的一台后端server
url_hash:按照访问的URL的hash结果来分配请求,每个URL定向到同一个后端服务器
hash关键数值:hash自定义的key

16 rewrite

跳转重定向
(不同于代理的跳转重定向,此处nginx不是代理服务器,而是本身就是web服务器)

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
2、访问http://Nginx地址/down时将跳转至http://Nginx地址/test/abc.html

正则表达式中()用于匹配括号之间的内容,通过$1,$2调用

flag标志位:
last:停止rewrite检测
break:停止rewrite检测
redirect:返回302临时重定向,地址栏会显示跳转后的地址
permanent:重返301永久重定向,地址栏会显示跳转后的地址(浏览器会永远记住,即使nginx服务器关闭了也还是会跳转至其他网页)(IE是这样的,但是搜狗浏览器收到301依然当做临时重定向处理)

rewrite加在不同位置时的优先级规则:server > location

17 HTTPS HTTPS

ssl on|off
ssl_certificate file
ssl_certificate_key file

  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,可以存储大约8k10k的会话

        location / {

                root /opt/app/code;

                index index.html index.htm;

 }

}

 

证书生成步骤:

一、生成秘钥
openssl genrsa -des3 -out hk.key 1024
-des3:选择3des算法
1024:加密位数

二、生成证书
openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout hk.key -out hk1.crt
-days:证书有效期
-x509:选择hash算法
rsa:2048:选择2048位加密
-keyout:key文件
-out:输出结果


苹果终端对服务端的要求:
1、服务器所有的连接使用TLS1.2以上的版本(openssl 1.0.2)
2、HTTPS证书必须使用SHA256以上的哈希算法签名
3、HTTPS证书必须使用RSA 2048位或ECC 256位以上公钥算法
4、使用前向加密技术

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/目录找,
如果这个目录没有目标文件则匹配 @code1 这个location

 

测试时候发现location里面有try_files后index语句将失效,即访问http://nginx服务器/时
也会被导向@code1

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):
vi /etc/security/limits.conf
root soft nofile 65535
root hard nofile 65535
* soft nofile 25535
* hard nofile 25535

21 CPU亲和 将所有进程均匀的分布在各个cpu核心上 worker_cpu_affinity auto;   最开头那级来配置,和worker_processes  1;是一级

user nginx;
worker_processes 24;
worker_cpu_affinity auto;

[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

原文地址:https://www.cnblogs.com/baihualin/p/10896580.html