zoukankan      html  css  js  c++  java
  • nginx安装以及常用配置

    nginx的源码安装

    0 安装相关软件:yum -y install pcre-devel zlib-devel openssl-devel
    1 下载  nginx-1.14.0.tar.gz 
    2 安装 tar xf nginx-1.14.0.tar.gz
        cd nginx-1.14.0/
        ./config --help
        ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf 
        --user=nginx --group=nginx 
        --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log 
        --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock
         --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module 
        --with-debug
    
    3 启动: /usr/local/nginx/sbin/nginx 
    4 查看:ss -tlnp  查看启动端口      ps aux  查看主进程和工作进程
    nginx特性:支持epoll,aio,mmap
        master/worker,支持平滑升级

    使用配置

    vim /etc/profile.d/nginx.sh
         export PATH=/usr/local/nginx/sbin:$PATH
    . /etc/profile.d/nginx.sh
    nginx -t
    nginx -s reload
    nginx -h 查看命令用法

     正常运行必备的配置

        1 user USERNAME【GROUPNAME】;指定worker进程的用户和组
            user nginx nginx;
        2 pid /path/to/pid_file #pid文件
    
        3 worker_rlimit_nofile #;   一个工作(worker)进程打开的最大文件描述符数量
    
        4 worker_rlimit_sigpending#;  每个用户能够发往worker进程的信号的数量

    性能优化相关的配置

    1 worker_processes

    1 worker_processes #; worker进程的个数,通常应该为物理cpu核心数量减1;一个留给内核使用
              可以配置为 worker_processes auto; 启动后工作进程等于cpu的个数,低版本不支持
            每个worker进程所能够响应的最大并发请求数量:
            worker_process * worker_connections

    2 work_cpu_affinity

    2 work_cpu_affinity cpumask cpumask...; 绑定cpu
            cupmask: 0001 0010 0100 1000
            worker_cpu_affinity 00000001 00000010 00000100 --->八颗cpu情况

    3worker_priority

    3 worker_priority nice:  #nice优先级
            [-20,19] 默认为0

    4accept_mutex

    4 accept_mutex[on|off];各worker接收用户的请求的负载均衡锁,
            为on时让多个worker轮流的系列化的响应新请求;

    5lock_file 

    5 lock_file /path/to/lock_file 锁文件的位置

    调试,定位问题的配置:

    1daemon

    1 daemon off|on
        是否以守护方式启动nginx;

    2 master_process

    2 master_process on|off;
        是否以master/worker模型运行nginx;

    3 error_log

    3 error_log  /path/to/error_log level;
        错误日志文件以及级别;出于调式的需要,可以设定为debug;在编译的时候要加上--with-debug,才有效

    套接字和主机相关的指令

    1 server{}定义一个虚拟主机

    server{
        listen PORT;
        server_name NAME;
        root /path/to/documentroot;
    }
    注意:
         1:基于port: 监听不同的端口
          2:基于hostname    server_name指令指向不同的主机名;

    2 listen

      default_server:设置默认虚拟主机;用于基于IP地址,
            或使用了任意不能对应于任何一个server的name时,所返回的站点
        例如:listen 80 default_server;
        ssl:用于限制只能通过ssl连接提供服务
        spdy:spdy protocol(speedy)编译时必须加上spdy模块
        http2:http version 2;基于spdy开发的

    3 server_name NAME[...];可以同时指定n个servername,名称可以用通配符*和正则表达式(~)

        匹配顺序:
        1 先做精确匹配:例如:www.example.com
        2 左侧通配符:例如:*.example.com
        3右侧通配符:例如:www.example.*
        4 正则表达式,例如:~^.*.example.com$
        4 default_server    

    4 tcp_nodelay on|off; 对keepalive模式下的连接是否使用tcp_nodelay选项,默认不启用

    5 tcp_nopush on|off; 是否启用tcp_nopush(freebse) 或tcp_cork(Linux);仅在sendfile为on时有用;默认off

    6 sendfile on |off ;直接在内核空间中响应报文,要为on

    路径相关的指令

    7 root  设置web资源的路径映射;用于指明请求的url所对应的文档的目录路径

        server{
            ...
            root /data/www/vhost1;例如:http://www.example.com/images/logo.jpg-->/data/www/vhost1/images/logo.jpg
        }
        server{
            ...
            location /images/{
                root /data/imgs/;  //例如:http://www.example.com/images/logo.jpg-->/data/imgs/images/logo.jpg
            }
        }

    8 location[=|~|~*|^~]url{...} 允许用户请求的uri来匹配定义的各location

    location @name{...}
    
    =:URI的精确匹配  
    ~:做正则表达式匹配,区分字符大小写
    ~*:做正则表达式匹配,不区分字符大小写
    ^~:URI的左半部分匹配,不区分字符大小写
    
    匹配优先级顺序:精确匹配 ,^~ ,~或~* ,不带符号的URL;

    9 alias 别名配置,定义路径别名

        只能用于location配置段
        location /images/{
             root /data/imgs/;
        }
        location /images/{
            alias /data/imgs/;
        }
         注意:
        root指令:路径为对应的location的“/”这个URL;
            /images/test.jpg  -->/data/imgs/images/test.jpg
        alias指令:路径为对应的location的“/url/”这个URL;
            /images/test.jpg -->/data/imgs/test.jpg

    10 index 一般指明首页文件,还可以带变量,比如说不同语言的首页,index file ...

    11 error_page code ... 自定义错误页面,根据http的状态码重定向错误页面

       error_page 404   /404.html;
        error_page 404  =200 /404.html;  注意root路径,其中200就是指定相应码

    12 try_files

    try_files file... url;
    try_files file... =code;
    尝试查找第1到第N-1个文件,第一个即为返回给请求者的资源:如果1到N-1文件都不存在,则跳转到最后一个uri;
    这个uri由其他location定义,避免造成死循环
    例如:注意下面的连个location
    root /data/imgs/;
    index index.html;
    location /images/{
        try_files index.html  /images/test1.html  /images/test2.html  /images/test3.html  //最后一个必须存在
    }
    location=/images/test3.html{
        expires 10s;
    }

    用户请求相关的配置

    13 keepalive_timeout;设定keepalive连接的超时时长,0表示禁止长连接;默认75秒

    14 keepalive_requests number;在keepalived连接上所允许请求的最大资源连接数量;默认为100;

    15 keepalive_disable指明禁止何种浏览器使用keepalive功能

    16 send_timeout发送相应报文的超时时长,默认为60s; 精确的说时两次写操作之间的时长

    17 client_body_buffer_size接收客户端请求报文body的缓冲区大小,默认为16K(32位系统为8k);超出此指定大小时,其将被移存到磁盘上,官方文档说是写在temporary file,临时文件上

     18   client_body_temp_path path [level1 [level2 [level3]]]; 设定用于存储客户端请求body的临时文件存放的地方以及数量

        client_body_temp_path  /var/tmp/client_body 2 2;

    对客户端请求的限制

    19 limit_except METHOD{...}  对指定范围之外的其他方法进行访问控制

    limit_except GET{
    
      allow 172.16.0.0/16;
    
      deny all;
    
    }

    20 limit_rate speed;表示客户端每秒能传输的字节数,默认为0表示无限制

    文件操作优化相关的配置

    21  aio  on|off; 是否启用异步io模式

    22 directio   size|off    

    23 open_file_cache off  

    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;

    每隔多久检查一次缓存中的缓存项的有效性,默认为60s;

    26  open_file_cache_min_uses number;缓存项在非活动期限内的最小应该被访问的次数;

     ngx_http_access_module模块的配置(基于ip的访问控制)

    27 allow address |CIDR |unix: | all;  允许访问的地址

    28 deny address |CIDR |unix: | all;   禁止访问的地址

    ngx_http_auth_basic_module模块的设置(basic认证)

    29 auth_basic string|off; 使用http basic认证协议对用户进行认证

    30 auth_basic_user_file;实现用户认证的账号文件;

    文件格式:
     name1:password1
     name2:password2:comment
    
    密码格式:
     1 encrypted with the crypt() function;
     2 md5加密;

    例如:

    配置文件改为:
    
    location /admin/{
        auth_basic  "Admin Area"; 
      auth_basic_user_file /etc]/nginx/.ngxpasswd # 指定验证文件的位置 }
    yum install httpd -y #不用启动httpd服务
    htpasswd -c -m /etc/nginx/.ngxhtpasswd tom #第一次要加 -c
    htpasswd  -m /etc/nginx/.ngxhtpasswd jerry
    nginx -s reload

     ngx_http_log_module: 模块的配置(访问日志)

    31 log_format 定义日志格式及其名称,日志格式一般通过内置变量来定义

    32 access_log path [format[buffer=size[flush=time]]];

    access_log off;

    访问日志文件路径,格式名称以及缓存大小和刷写时间间隔;建议定义缓冲以提升性能;

    33 open_log_file_cache max=N [inactive=time][min_uses=N][valid=time];

    ngx_http_stub_status_module模块设置: 只能用作server和location中

    34 stub_status; 通过指定的uri输出 stub_status;

    location /status/ {
       stub_status;  
    }
    内部含有敏感信息,一般不设置,要么加上访问控制
    输出:
    active connections: 291 当前活动的客户端连接数
    server accepts handle requests: 已经接受的客户端连接总数量 已经处理过后客户端连接总数量 客户端总的请求数据
    accepts handled requests
    Reading 6 Writing 179 Waiting 106 正在读取的客户端请求的数量 正向其发送响应报文的连接数量 等待其发出请求的空闲连接数量;

     ngx_http_referer_module模块配置(基于请求报文中的Rerefer首部做访问控制)

    35 valid_referers none|blocked|server_names|string...   

    none:请求报文不存在referer首部

    blocked:请求报文中存在referer首部,但其没有有效值,或者非以http://或https:// 开头

    server_names: 其值为一个主机名;

    arbitrary string;直接字符串,可以使用*通配符

    regular expression:以~起始的正则表达式

    内置变量:$invalid_referer(所有不能符合valid_referer指定定义的引用请求均为不合法引用

    示例:防盗链

    valid_referers none blocked server_names  *.example.com  example.*   www.example.org/galleries/   ~.google;
    
    if($invalid_referer){
    
      return 403;
    
    }

    防盗链的例子

    location ~* .(gif|jpg|png|jpeg)$ {       #针对防盗链的文件类型
        expires     30d;
        valid_referers *.hugao8.com www.hugao8.com m.hugao8.com *.baidu.com *.google.com;   #白名单,允许访问的域名
        if ($invalid_referer) {
        rewrite ^/ http://www.hehe/404.jpg;  #让盗链者链接到其他地方
        
        }
        }

     ngx_http_proxy_module模块:

    server{

      listen

      server_name

      location / {

        proxy_pass   http://localhost:8000/;

        proxy_set_header  Host   $host;  

        proxy_set_header X-Real_IP  $remote_addr;  #真正访问的客户端ip

      }

    }

    格式:

      location  /url {          #这个url的路径可以不存在

        proxy_pass http://back_server:port/newurl;

    }

  • 相关阅读:
    Spring service本类中方法调用另一个方法事务不生效问题(转载)
    JVM垃圾收集器
    LInkedHashMap实现最近被使用(LRU)缓存
    HTML模板与iframe框架
    Mybatis中常用sql语句
    从零到一: 后端接口文档
    Mysql日期处理
    Java-集合框架与数组的实际应用-组装Json字符串
    Mysql查询之 指定顺序排序
    Eclipse中复制项目后,怎么更改项目名等相关配置?
  • 原文地址:https://www.cnblogs.com/mmyy-blog/p/9638656.html
Copyright © 2011-2022 走看看