zoukankan      html  css  js  c++  java
  • nginx 配置的一些参数

    /etc/nginx/nginx.conf


    worker_rlimit_nofile#;      --指定一个worker 进程所能打开的最大文件描述符数量
    worker_rlimit_sigpending#;    --指定每个用户能够发往进程的信号的数量


    性能优化相关的配置
      1.work_processes      --worker 进程的个数,通常为物理CPU核心数量减1,可以设为auto,实现自动设定
      2.worker_cpu_affinity CPUMASK CPUMASK...;
              0001
              0010
              0100
              1000
          worker_cpu_affinity 00000001 00000010 00000100;
      3. worker_priority nice ; [-20,19]

    调试,定位问题的配置
      1. daemon [off|on]      --是否以守护进程方式启动nignx
      2. master_process on|off| --是否以master/worker 模型运行nignx
      3. error_log /PATH/TO/ERROR_LOG level --错误日志文件以及级别, 出于调试的需要,可以设定为debug,但debug仅在编译时使用了"--with-debug"选项才有效

    event{}
      1.worker_connections#; 每个worker进程所能够响应的最大并发请求数量=worker_processer*work_connections
      2.use [epoll|rgsig|select|poll];定义使用的事件模型;建议让nginx 自动选择
      3.accept_mutex[on|off];  各worker 接收用户的请求的负载均衡锁,启用时,表示用于多个worker轮流地,序列化的响应新请求;
      4.lock_file /PATH/TO/LOCK_FILE;

    增加nginx path到环境变量
    cat /etc/profile.d/nginx.sh
    export PATH=/usr/local/nginx/sbin:$PATH

    . /etc/profile.d/nginx.sh
    mkdir -pv /data/www/vhost2
    nginx -s reload    -------> nginx 重起
    ss -tnl     ------->查看端口是否打开

    主机相关的指令
    1.server {}
     定义虚拟主机
     server{
          listen PORT;                   --基于port:listen 指令监听在不同的端口;
          server_name NAME;            --基于hostname server_name  指令指向在不同的主机名;
          root /path/to/documentroot;
       }
     
    2.listen
        listen address[:port]{default_sever}[ssl][http2|spdy]
        listen port{default_sever}[ssl][http2|spdy]
        default_sever :设置默认虚拟主机用于基于IP地址,或使用了任意不能对应任何一个server的name时所返回站点;
        ssl:用于限制只能通过SSL连接提供服务
        spdy:SPDY protocol (SPEEDY),在编译了spdy模块的情况下,用于支持SPDY协议
        htpp2: http version 2;
    3. server_name NAME [...];
        后可以跟多个主机名,可以使用通配符和正则表达式
        (1) 首先精匹配
        (2) 左侧通配符  *.doufupi.com
        (3) 右侧通配符  www.doufupi.*
        (4)    正则        ~^,*.doufupi.com$
        (5) default_sever
    4. tcp_nodelay on|off;    对于keepalive模式下的连接是否使用TCP_NODELAY选项
    5. tcp_nopush on|off;    是否启用TCP_NOPUSH(FREEBSE)或TCP_CORK(linux)选项,仅在sendfile 为on时有用;
    6. sendfile on|off;    是否启用sendfile 功能

    路径相关的指令
    7. root
        设置web资源的路径映射;用于指明请求的URL所对应的文档目录路径;
        server{
            ...
            root /data/www/host1;
        }
        http:www.doufupi.com/images/IMEG345.jpg-->/data/www/host1/images/IMEG345.jpg
        server{
            ...
            server name wwww.doufupi.com;
            location /images/{
            root /data/imgs/;
            }
        }
        http:www.doufupi.com/images/IMEG345.jpg-->/data/imgs/images/IMEG345.jpg
    8. location [=|-|~*|^-] url {...}
        location @name {...}
        功能:允许根据用户请求的URL来匹配定义的各个location,匹配时,此请求将被相应的location块中的配置所处理;简言之,即用于为需要用到专用配置的URL提供特定配置;
        = :URL的精确匹配
        - : 做正则表达式匹配,区分字符大小写
        -*: 做正则表达式匹配,不区分字符大小写
        ^-: URL左半部分匹配,不区分字符大小写
        匹配优先级: = > ^- > -/-*> 不带符号的URL


    9. alias
        定义路径别名,只能用用location配置段,定义路径别名
        location /images/ {
            root /data/imgs/;
        }
        location /images/ {
            alias /data/imgs/;
        }
        
        注意:
            root指令:路径为对应的location的"/"的这个URL:      /images/IMEG345.jpg -->/data/imgs/images/IMEG345.jpg
            alias指令:路径为对应的location的"/url/"的这个URL: /images/IMEG345.jpg -->/data/imgs/IMEG345.jpg


    10. index 指网站默认指定的主页
        可以根据不同的IP 给定不同的主页
        谁的使用范围小,谁的优先级别高
        
    11. error_page code...[=[response]] url;
        根据http的状态码重定向错误页面;
        error_page 404=200 /404.html; (以指定的响应码进行响应)


    12. try_file
        try_file file ... =code; 尝试查找第1至第N-1个文件,第一个是为返回给请求者的资源,若1至N-1文件都不存在,则跳转到最后一个URL(
        必须不能匹配到当前LOCATION,而应该匹配至其它LOCATION,否则会导致死循环);
        
    用户请求相关的配置:
    13.keeplive_timeout            设定keeplive连接的超时时长;0 表示禁止长连接
    14. keepalive_requests number;     在keepalive 连接上所允许请求的最大资源数量 ,default 100;
    15. keepalive_disable non|browser ...;   指明禁止为何种浏览器使用keepalive功能
    16. send_timeout#            发送报文的超时时长,deafaule 60s
    17. client_body_buffer_size size     接收客户请求报文body的缓冲区大小,deafaule16K,超出此指定大小时,其将存于磁盘上
    18. client_body_temp_path path [level [level2 [level3]]];   用于存储客户端请求body的临时存储路径及子目录结构和数量
        client_body_temp_path  /var/tmp/client_body 2 2;

    对客户端请求的进行限制
    19. limit_excepte METHOD {...}
    20. limit_rate_speed;      限制客户端每秒种所能传输的字节数,default 0 表示无限制

    文件操作优化相关的配置
    21. aio on|off
    22. directio on|off
    23. open_file_cache
        open_file_cache max=N[inactive=time];
            nginx可以缓存以下三种信息:
            (1) 文件描述符,文件大小和最近一次修改时间;
            (2) 打开的目录结构
            (3)没有找到的或者没有权限操作文件的相关信息
            max=N 可以缓存的最大条目上限,一旦达到上限,则会使用LRU算法从缓存中删除最近最少使用的缓存
            inactive=time 在此处指定的时长内没有被访问过的缓存是为非活动缓存项,因此直接删除


    24.open_file_cache_errors on|off`      是否缓存找不到其文件,或者没有权限访问的文件相关信息
    25.open_file_cache_valid time:     每隔多久检查一次缓存中缓存项的有效性 deafaule 60S
    26. open_file_cache_min_user number   缓存项在非活动期限内最少应该被访问的次数
        
    27.allow address |CDIR|unix|all;
    28.deny address |CDIR|unix|all;
        location ~ ^/1.html {
        root /soft/code;
        index index.html;
        deny 61.142.20.34;          #deny主指定iP
        allow all;
        }
        
        location ~ ^/admin {
        root /soft/code;
        index index.html;
        allow 61.142.20.34;         #allow指定iP
        deny  all;          
        }

        //配置拒绝某⼀一个IP, 其他全部允许
        location ~ ^/1.html {
            root /usr/share/nginx/html;
            index index.html;
            deny 192.168.56.1;
            allow all;
            }
        //只允许某⼀一个⽹网段访问,其它全部拒绝
        location / {
            root html;
            index index.php index.html index.htm;
            allow 192.168.56.0/24;
            deny all;
            }

    nginx_http
    htpasswd -c -m /etc/nginx/.ngxhtpasswd jerrry
    htpasswd -c -m /etc/nginx/auth_conf jerrry
    htpasswd -b -m /etc/nginx/auth_conf mark #新增
    htpasswd  -m /etc/nginx/.ngxhtpasswd jerrry
    29.auth_basic string |off $        使用http basic 对用户 需要安装包httpd-tools
    30. auth_basic_user_file file;
        文件格式 :
            name1:password1
            name1:password2:comment
        密码格式:
        (1)encrypted with crypt function;
        (2)md5加密;
           
        location /admin/ {
            auth_basic "Admin Area";
            auth_basic_user_file /etc/nginx/.ngxhtpasswd;
        }
    31. log_format
    32. access_log path[ format ]       access_log off;
    33. open_lof_file_cache max=N

       open_lof_file_cache off;

      ngx_http_stub_status


    34.stub_status:
      通过指定url输出stub status;
          location /status/ {
              auth_status;
          }
      ngx_http_referer_module模块配置(基于请求报文中的Referer首部的值做访问控制)
    35. valid_refers none|blocked|server_name|string ...;
        none:
        blocked:
        arbitrary string:
        regular expression:

    36.limit_req_zone
        limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
        server {
        listen  80;
        server_name localhost;
        location / {
            root /usr/share/nginx/html;
            index index.html index.htm;
            #limit_req zone=req_zone;
            limit_req zone=req_zone burst=3 nodelay;
            }
        }

    37.limit_conn_zone
        limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
        server {
        listen  80;
        server_name localhost;
        location / {
            root /usr/share/nginx/html;
            index index.html index.htm;
            #limit_req zone=req_zone;
            limit_conn_zone zone=conn_zone 1;
            }
        }
    连接限制没有请求限制有效?
    我们前⾯面说过, 多个请求可以建⽴立在⼀一次的TCP连接之上, 那么我们对请求的精度限制,当然⽐比对⼀一个连接的限制会更更加的有效。
    因为同⼀一时刻只允许⼀一个连接请求进⼊入。但是同⼀一时刻多个请求可以通过⼀一个连接进⼊入。所以请求限制才是⽐比较优的解决⽅方案。

  • 相关阅读:
    Android之Activity启动过程
    Android之Application进阶
    Android之Context进阶
    Thread之ThreadLocal
    Android 系统服务与Binder应用服务
    Android Binder
    Android SystemServer
    Android系统服务与服务注册
    Android Binder进阶扁一
    小米商城-题头3
  • 原文地址:https://www.cnblogs.com/tingxin/p/12984607.html
Copyright © 2011-2022 走看看