Nginx常用模块及配置示例

1. ngx_http_status_module

作用

记录Nginx客户端基本访问状态信息

指令

Syntax: stub_status;
Default:    —
Context:    server, location

配置示例

location /status {      #访问url为http://主机/status
    stub_status on;      #on开启状态记录,off关闭状态记录
    access_log off;        #on记录访问日志,off不记录访问日志
}

状态信息说明:

Active connections:2                 Nginx当前活跃连接数

server                            Nginx启动到现在共处理的连接个数。

accepts                           Nginx启动到现在共成功创建握手次数。

handled requests                   Nginx总共处理了19次请求。

Reading                          Nginx读取到客户端的 Header 信息数。

Writing                           Nginx返回给客户端的 Header 信息数。

Waiting                           Nginx开启keep-alive长连接情况下, 既没有读也没有写, 建立连接情况

2. http_autoindex_module

作用

目录浏览

指令

Syntax: autoindex on | off;
Default:    
autoindex off;
Context:    http, server, location

配置示例

location / {
    root html;
    autoindex on;            #on开启目录浏览功能,off(默认)关闭目录浏览功能
    autoindex_localtime on;    #on(默认)文件时间显示为文件服务器时间,off显示为GMT时间
    autoindex_exact_size off;  #on显示文件确切大小(bytes),off(默认)为显示大概大小(KB/MB/GB)
    #charset utf-8,gbk;      #字符集设置,使用utf-8以及gbk可以解决中文目录乱码问题
}

3. limit_conn_module

作用

并发连接数限制

指令

//全局定义连接限制
Syntax:  limit_conn_zone key zone=name:size;
Default: —
Context: http
//引用连接限制
Syntax: limit_conn zone number;
Default: —
Context: http, server, location

配置示例

http {
    limit_conn_zone $binary_remote_addr zone=conn_zone:10m;    #定义一个名为con_zone,内存限制为10m的空间($binary_remoteIP为4字节)
    ……
    server {
        location / {
            limit_conn conn_zone 500;        #引用conn_zone空间,同一时刻只允许500个IP连接
        }
    }
}

4. ngx_http_limit_req_module

作用

请求数限制

指令

//全局定义请求限制
Syntax:  limit_conn_zone key zone=name:size rate=rate;
Default: —
Context: http
//引用请求限制
Syntax: limit_conn zone number [burst=number] [nodelay];
Default: —
Context: http, server, location

配置示例

http {
    limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;    #定义一个名为req_zone,请求速率为每秒允许一个IP请求的空间
    server {
        location / {
            limit_req zone=req_zone;            #引用req_zone空间
            #limit_req zone=req_zone burst=3 nodelay;    #允许延时处理3个请求,剩余不做处理(默认返回503)
        }
    }
}

5.ngx_http_access_module

作用

访问控制,对客户端地址进行设置限制策略(客户端使用代理访问时,转发过程中如没有记录到客户端真实IP地址,则无法限制,需要在代理配置x_forwarded_for参数来同时记录客户端及代理端IP地址)

指令

Syntax: allow|deny address | CIDR | unix: | all;
Default:    —
Context:    http, server, location, limit_except

配置示例

location / {
    root   html;
    index  index.php index.html index.htm;
    allow   192.168.1.0/24;    #允许 192.168.1.0/24网段主机访问
    deny    all;            #拒绝所有
}

6. ngx_http_auth_basic_module   

作用

web认证,设置用户名和密码进行访问

指令

//配置语法
Syntax: auth_basic string| off;
Default:    auth_basic off;
Context:    http, server, location, limit_except
//用户密码记录配置文件
Syntax: auth_basic_user_file file;
Default:    -
Context:    http, server, location, limit_except

配置示例

#下载httpd-tools工具包,目的是使用其htpasswd加密工具
yum install httpd-tools
#使用htpasswd工具对admin用户进行加密,并存储在auth_conf文件中,回车后需设置admin的密码
htpasswd -c /etc/nginx/auth_conf admin
配置
location / {
        root   html;
       index  index.php index.html index.htm;
    #auth_basic "Auth access Blog Input your Passwd!";    #定义提示信息
    auth_basic_user_file /etc/nginx/auth_conf;    #指定认证的用户配置文件(内容为用户名: 密码密文)
}

7. ngx_http_fastcgi_module

作用

动态应用相关的模块,如PHP

指令

//指定fascgi服务器地址
Syntax:    fastcgi_pass address;
Default:    —
Context:    location, if in location
//指定fastcgi默认首页
Syntax:    fastcgi_index name;
Default:    —
Context:    http, server, location
//指定fastcgi变量
Syntax:    fastcgi_param parameter value [if_not_empty];
Default:    —
Context:    http, server, location

PHP配置示例

location ~ \.php$ {            #定义匹配规则为.php资源
    fastcgi_pass   127.0.0.1:9000;    #转发到的PHP服务地址
    fastcgi_index  index.php;    #定义fastcgi首页文件
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;    #设置变量传递到fastcgi服务器
    include        fastcgi_params;    #引用变量文件
}

8. ngx_http_gzip_module

作用

压缩模块,对Nginx返回的数据压缩,可有效地节约带宽,并提高响应至客户端的速度。属性能优化模块。

指令

Syntax: gzip on | off;
Default: gzip off;
Context: http, server, location, if in location

配置示例

location ~ .*\.(jpeg|gif|png)$ {    #定义访问资源(与gzip-types一致)
    gzip on;            #on开启压缩功能,off(默认)不开启压缩功能
    #gzip_comp_level 1;        #定义压缩级别,数值越高压缩比越大,越消耗服务器性能
    gzip_http_version 1.1;    #协议版本(1.0|1.1)
    gzip_types text/plain image/jpeg image/gif image/png;    #定义匹配需压缩资源类型(mime-type中定义)
    #gzip_static off;        #on开启预读功能,off(默认)不开启预读功能,预先在服务器对资源进行压缩,响应时优先发送预压缩资源
}

9.  ngx_http_proxy_module

作用

代理模块,正反向代理

指令

Syntax: proxy_pass URL;
Default:    —
Context:    location, if in location, limit_except

配置示例

location / {
    proxy_pass http://192.168.1.6;            #代理的后端服务地址

    proxy_redirect default;
    proxy_set_header Host $http_host;        #将用户请求的主机信息添加到头部信息,必加参数
    proxy_http_version 1.1;            #代理使用的http协议
    proxy_set_header X-Real-IP $remote_addr;        #记录代理信息,多级代理下会丢失中间代理信息,建议使用x-forwarded-for
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    #将用户真实IP地址添加到头部信息,必加参数

    proxy_connect_timeout 30;            #代理服务器与后端服务器的连接超时时间
    proxy_send_timeout 60;            #后端服务器回传数据代理服务器的超时时间
    proxy_read_timeout 60;            #代理服务器等待后端服务器应响的超时时间

    proxy_buffering on;                #开启代理缓存,边接收到缓存边返回给用户
    proxy_buffers 4 128k;                #代理缓冲区大小
    proxy_buffer_size 32k;            #缓冲区中保存用户头信息缓存冲大小
    proxy_busy_buffers_size 256k;            
    proxy_max_temp_file_size 256k;
    
    #include proxy_params            #建议将代理相关参数单独写在一个文件中,然后通过include进行引用
}

10. ngx_http_upstream_module

作用

实现七层负载均衡,需结合代理模块进行使用

指令
//定义后端服务资源池
Syntax:    upstream name { ... }
Default:    —
Context:    http

配置示例

#定义后端服务资源池
upstream backend {
    server backend1.example.com weight=5;    #默认调度算法是轮询,weight选项为设置调度权重,其他调度算法还有ip_hash、url_hash、least_conn等
    server 127.0.0.1:8080       max_fails=3 fail_timeout=30s;    #max_fails选项为允许请求的失败次数,fails为达到允许失败的次数后,服务暂停的时间
    server unix:/tmp/backend3;
    server backup1.example.com  backup;        #down表示不参与负载均衡,backup表示为预留的备份服务器,当其他所有均不可用时才进行调度
}
#代理访问
server  {
    server_name www.example.com;
    listen 80;
    location / {
        proxy_pass http://backend;
        include proxy_params;
    }
}

11. ngx_stream_core_module

作用

实现四层负载均衡,需结合upstream、proxy_pass模块使用,只能在main段中配置

配置示例
stream {
        upstream ssh_proxy {
                hash $remote_addr consistent;    #客户端IP固定访问同一台服务器
                server 192.168.56.103:22;
        }
        upstream mysql_proxy {
                hash $remote_addr consistent;
                server 192.168.56.103:3306;
        }
    server {
        listen 6666;
        proxy_connect_timeout 1s;
        proxy_timeout 300s;
        proxy_pass ssh_proxy;
    }
    server {
        listen 5555;
        proxy_connect_timeout 1s;
        proxy_timeout 300s;
        proxy_pass mysql_proxy;
    }
}

12. ngx_http_rewrite_module

作用

实现对url地址的重写

指令

Syntax:    rewrite regex replacement [flag];
Default:    —
Context:    server, location, if

配置示例

location / {
    rewrite ^(.*)$  /page/maintain.html break;        #以任意字符开头及结尾的均改写为 /page/maintain.html 
}

flag标记说明:

  • last  本条规则匹配完成后停止后续匹配,
  • break        匹配规则同last,区别在于匹配到规则后last会对server标签重新发起rewrite跳转内容的请求,而break则会在本地路径目录中寻找rewrite跳转内容请求文件。
  • redirect    返回302临时重定向,地址栏显示跳转后的地址,
  • permanent       返回301永久重定向,地址栏显示跳转后的地址,两者的主要区别在于redirect在每次跳转时都会询问服务器,如果服务器不可用,则跳转失败,而permanent则只在第一次跳时询问服务器,后续均不再询问,直接通过浏览器中的缓存进行跳转。

常用的标记只有后两种,主要用于http跳转https,前两者开发使用较多

13. nginx_http_ssl_module

作用

ssl认证模块,用于加密的http连接(https)

指令

Syntax:    ssl on | off;
Default:    ssl off;
Context:    http, server

Syntax: ssl_certificate file;
Default: —
Context: http, server

Syntax: ssl_certificate_key file;
Default: —
Context: http, server

配置示例

server {
    listen 443;
    server_name www.welcome.com;
    ssl on;                #on开启ssl安全认证,off不开启ssl安全认证
    certificate ssl_key/welcome.crt;        #指定ssl证书公钥文件
    certificate_key ssl_key/welcome.key;    #指定ssl证书私钥文件
    #ssl_session_cache    shared:SSL:1m;    #指定ssl会话缓存的类型和大小
    #ssl_session_timeout  5m;        #指定ssl会话超时时间
    #ssl_ciphers  HIGH:!aNULL:!MD5;        #指定ssl的加密套件
    #ssl_prefer_server_ciphers  on;        #指定协商加密算法时,优先使用服务端的加密套件,而非客户端浏览器
    
    location / {
        root /usr/share/nginx/html
        index index.html index.php
    }
}

14. ngx_http_log_module

作用

访问日志模块,以指定的格式记录Nginx客户访问日志等信息

指令

//定义日志格式
Syntax:    log_format name [escape=default|json|none] string ...;
Default:    
log_format combined "...";
Context:    http
//开启日志
Syntax:    access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
    access_log off;
Default:    access_log logs/access.log combined;
Context:    http, server, location, if in location, limit_except

配置示例

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '    #定义名为main的格式模板
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
  log_format access_json '{"@timestamp":"$time_iso8601",'     #定义名为access_json的格式模板
      '"host":"$server_addr",'
      '"clientip":"$remote_addr",'
      '"size":$body_bytes_sent,'
      '"responsetime":$request_time,'
      '"upstreamtime":"$upstream_response_time",'
      '"upstreamhost":"$upstream_addr",'
      '"http_host":"$host",'
      '"url":"$uri",'
      '"domain":"$host",'
      '"xff":"$http_x_forwarded_for",'
      '"referer":"$http_referer",'
      '"status":"$status"}';
    #access_log /var/log/nginx/access.log access_json;   #引用名为json的格式模板 access_log
/var/log/nginx/access.log main; #引用main格式模板 #access_log off #不记录日志 }

 

posted @ 2023-04-17 17:29  isxiefeng  阅读(449)  评论(0)    收藏  举报