首页 > 技术文章 > Nginx 常用模块指令

ruhuanxingyun 2019-07-23 19:59 原文

1. HTTP核心模块(HTTP Core)

  A. client_body_buffer_size:指定连接请求实体的缓冲区大小,如果超了,那么这些请求实体的整体或部分将尝试写入一个临时文件,默认值是8k/16K;

       B. client_body_temp_path:指定连接请求实体试图写入的临时文件路径,默认值是client_body_temp;

  C. client_max_body_size:指定允许客户端连接的最大请求实体大小,如果超了,返回客户端“Request Entity Too Large” 413错误,默认值是1m;

  D. location六种表示

方式 描述
= URI和位置的精确匹配,如果找到完全匹配,搜索停止,优先级最高
^~ 不做正则表达式匹配,常规字符串匹配
~* 不区分大小写的正则表达式匹配
~ 区分大小写的正则表达式匹配
/ 所有的指令都不匹配时用它
@ 非常规请求,用于请求重定向

   匹配顺序:

      首先:nginx检查使用了前缀字符串定义的指令快,选中并记住具有最长匹配前缀的指令快;

      其次:按照配置文件里出现的先后顺序来匹配正则表达式,搜索在第一次匹配时终止;

      最后:使用/里的指令内容

  E. alias:静态资源代理,忽略location后面的值,只能用在location字段中;

  F. root:静态资源代理,会拼接location后面的值,可以用在http、server、location字段中;

 

2. HTTP代理模块(HTTP Proxy)

  A. proxy_pass:设置被代理服务器的地址和被映射的URI,地址可以使用主机名或IP加端口号的形式,默认值是no,使用字段在location或location中if字段;

  B. proxy_set_header:允许将发送到被代理服务器的请求头重新定义或增加一些字段,默认值是Host and Connection,使用字段在http,server,location;

    proxy_set_header X-Real-IP $remote_addr;  // 真实客户端IP

    proxy_set_header X-Forwarded-Proto $scheme;  // 访问协议

    proxy_set_header X-Forwarded-Host $host;  // 访问主机IP

    proxy_set_header X-Forwarded-Port $server_port;  // 访问主机端口

  C. proxy_connect_timeout:指定一个连接到代理服务器的超时时间,默认值是60s,使用字段在http,server,location;

     D. proxy_send_timeout:设置代理服务器转发请求的超时时间,如果超了,nginx将关闭连接,默认值是60s,使用字段在http,server,location;

   E. proxy_read_timeout:设置读取后端服务器应答的超时时间,默认值是60s,使用字段在http,server,location。

 

3. HTTP头处理模块(HTTP Headers)

  A. add_header:只会在响应头部中增加某个新字段,不能对已经定义的头进行重写,语法:add_header name value,可以设置跨域

    add_header Set-Cookie 'CookieName=CookieValue';

  B. expires:在应答中标记一个过期时间 ,语法:expires time;  

 

4. HTTP基本认证模块(HTTP Auth Basic)

  A. auth_basic:用来基于用户名和密码的HTTP基础验证,语法:auth_basic text,可以第三方接口验证

  B. auth_basic_user_file:为验证域指定了密码文件,语法:auth_basic_user_file the_file;

 

5. HTTP负载均衡模块(HTTP Upstream)

  A. upstream:为后端提供简单的负载均衡;

 

6. 默认主页设置模块(index)

  A. index:如果URL中没有指定文件,则设置一个默认主页,语法:index file-path;

 

7. SSL模块(ssl)

  A. ssl:是否开启HTTPS,语法:ssl [on|off],默认值off;

  B. ssl_certificate:指定pem格式的证书文件,一个文件可以包含其他的证书,语法:ssl_certificate file,路径可以为相对路径,证书生成可参考自签名SSL证书

  C. ssl_certificate_key:指定pem格式的秘钥,语法:ssl_certificate_key file,路径可以为相对路径;

  D. ssl_ciphers file:指定允许的密码,格式ssl_ciphers file,默认值ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

  E. ssl_protocols:指定要使用的SSL协议,语法:ssl_protocols [SSLv2] [SSLv3] [TLSv1];

  F. ssl_session_cache:设置储存SSL会话的缓存类型和大小,语法:ssl_session_cache off;

  G. ssl_session_timeout:设置客户端能否反复使用存储在缓存中的会话参数时间,语法:ssl_session_timeout time;

 

8. 字符集设置模块(charset)

  A. charset:对响应头中“Content-Type”字段使用指定的编码集,语法:charset encoding|off,默认值off;

 

9. URL重写模块(rewrite)

  A. break:完成当前设置的规则,停止执行其他的重写命令;

  B. if:判断条件成立与否,从而是否执行大括号内的语句;

  C. return:结束执行配置语句并为客户端返回状态代码,可以使用下列的值204,400,402-406,408,410, 411, 413, 416与500-504;

  D. rewrite:实现URL重定向,根据正则表达式来匹配内容跳转到replacement,结尾是flag标记,语法为:rewrite regex replacement [flag];

    rewrite ^/(.*) http://www.baidu.com/ permanent;

    last:本条规则匹配完成后继续向下匹配新的location URI规则;

    break:本条规则匹配完成后终止,不再匹配任何规则;

    redirect:返回302临时重定向;

    permanent:返回301永久重定向;

  E. set:设置一个变量并为其赋值,语法:set variable value;

 

10. nginx.conf配置文件

  A. 配置文件组成:全局块+events块+http块

  B. work数应该和CPU数相等;

  C. master是管理员,work是具体的工作进程,一个master多个work可以使用热部署;

#user  nobody;
worker_processes  1;

# 错误日志
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    
    # 开启HTTPS
    ssl on;
    
    # 为这个虚拟主机指定PEM格式的证书文件,注意windows下绝对路径有问题 
    ssl_certificate server.crt;
    
    # 为这个虚拟主机指定PEM格式的秘钥,注意windows下绝对路径有问题 
    ssl_certificate_key server.key;
    
    # 指定要使用的SSL协议
    ssl_protocols SSLv2 SSLv3 TLSv1;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
    # 日志格式
    log_format  main '["$remote_addr","$http_x_forwarded_for","$request","$request_body","$status","$body_bytes_sent","$bytes_sent","$msec","$http_user_agent","$request_length","$request_time","$upstream_response_time","$time_local","$upstream_addr"]'
    
    # 访问日志
    #access_log  logs/access.log  main;
    
    # 使用sendfile系统调用来传递文件
    sendfile        on;
    #tcp_nopush     on;
    
    # 指定客户端与服务器长连接的超时时间,超时将关闭连接
    keepalive_timeout  65;

    #gzip  on;
    
    # 设置一群服务器,提供负载均衡
    upstream address {
        # 指定后端服务器的名称和一些参数,可以使用域名,IP,端口,其中weight指权重
        server localhost:8083 weight=1;
        server localhost:8084 weight=1;
        server localhost:8085 weight=1;
    }
    
    # 包含虚拟主机的配置
    server {
        # 被访问的ip地址及端口号,可以只指定一个IP,一个端口,或者一个可解析的服务器名
        listen 4202;
        
        # 将HTTP请求的主机头与这指定的参数进行匹配
        server_name localhost;
        
        # 指定连接请求实体的缓冲区大小,如果超过,那么请求实体的整体或部分将尝试写入一个临时文件
        client_body_buffer_size 256K;
        
        # 指定连接请求实体试图写入的临时文件路径
        # client_body_temp_path client_body_temp;
        
        # 匹配查询,根据URI的不同需求来进行配置
        location / {
            # 请求达到后文件的根目录
            root dist;
            
            # 如果URL中没有指定文件。则设置一个默认主页
            index index.html;
        }
        
        location /manage {
            # 指定允许客户端连接的最大请求实体大小(文件上传需要增大,默认1m)
            client_max_body_size 500m;
            
            # 设置被代理服务器的地址和被映射的URI,地址可以使用主机名或IP加端口号的形式(最后面斜杠必须写)
            proxy_pass  http://localhost:8082/;
            
            # 设置代理服务器转发请求的超时时间,超时nginx将关闭(默认是60s)
            proxy_send_timeout 240;
            
            # 读取后端服务器应答的超时时间(默认是60s)
            proxy_read_timeout 240;
        }
        
        location /business {
       if ($request_method = 'OPTIONS') {
         add_header 'Access-Control-Allow-Origin' '*';
         add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTION';
         add_header 'Access-Control-Allow-Headers' 'DNT,X-Custom-Header,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,Content-Length';
          
         return 204;
       } client_max_body_size 100m; proxy_pass http:
//localhost:8081/; proxy_send_timeout 240; proxy_read_timeout 240; } location /oauth { proxy_pass http://localhost:9001/; } location /address { proxy_pass http://address; } } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }

 

可参考:Nginx中文手册

    Nginx的变量参数

 

https://www.jianshu.com/p/a8261a1a64f8

https://www.cnblogs.com/nhdlb/p/12380003.html

 

https://blog.csdn.net/djrm11/article/details/106652742/

https://blog.csdn.net/ffzhihua/article/details/90202924

https://blog.csdn.net/yanggd1987/article/details/41542785/

推荐阅读