nginx

简介

nginx是一款高性能的http服务器,目前国内包括BAT在内的众多互联网企业均采用其作为反向代理服务器,一个很重要的用途就是用来实现负载均衡。其具有优异的性能和丰富的配置功能。nginx一般由一个master进程和多个worker进程组成,其中master进程主要用来管理worker进程,包含:接收来自外界的信号,向各worker进程发送信号,监控worker进程的运行状态,当worker进程退出后(异常情况下),会自动重新启动新的worker进程。worker进程主要负责处理基础的网络事件。各个worker进程之间是相互独立的,且一个网络请求的处理仅会由一个worker进程独立处理。为了避免多个worker进程竞争cpu资源,一般worker进程的数目与cpu的核数相等。

配置管理

nginx支持丰富的配置选项,包括http,server等,还是支持tcp代理。给出一个配置例子:

//定义nginx运行的用户
user flying_mgr; 
//nginx进程数
worker_processes 4;
//更改wroker进程的最大打开文件数限制,默认为操作系统的限制
worker_rlimit_nofile 262140;
//开启利用多核特性,1000表示启用第1个核
worker_cpu_affinity 1000 0100 0010 0001;
error_log logs/error.log;
pid run/nginx.pid;

events
{
  //设置IO事件模型
  use epoll;
  //worker进程能并发处理(发起)的最大连接数
  worker_connections 65535;
}

http
{
    include mime.types;
    default_type application/octet-stream;
    //aio与directio配合使用,超过512k启用directio
    aio on;
    directio 512;
    //sendfile系统调用,可以在磁盘和TCP socket之间互相拷贝数据,立即将数据从磁盘读到os缓存
    sendfile on;
    output_buffers 1 128k;
    //是否在error_log中记录不存在的错误
    log_not_found off;
    keepalive_timeout 65;
    //关闭在错误页面的nginx版本数字,提高安全性
    server_tokens off;
    //设置压缩参数
    gzip on;
    gzip_comp_level 6;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/javascript application/json;
    //日志格式
    log_format main '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$request_time" "$upstream_response_time"';
    //访问日志
    access_log logs/${server_name}.access.log main;
    //是否传递4xx和5xx错误信息到客户端
    fastcgi_intercept_errors on;
    error_page 500 502 503 504 /50x.html;
    //限制访问命令
    limit_req_zone $binary_remote_addr zone=allips:10m rate=20r/s;
    limit_conn_zone $binary_remote_addr zone=perip:10m;

    server {
        listen 80;
        server_name example.net;
        access_log /var/log/nginx/example.net.log;
        rewrite ^(.*) https://$server_name$1 permanent;
    }
    server {
        listen 443;
        server_name example.net.net;
        access_log /var/log/nginx/example.net.log;
        error_log /var/log/nginx/example.net-error.log;
        ssl_certificate /etc/nginx/example_net.crt;
        ssl_certificate_key /etc/nginx/example_net.key;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 5m;
        ssl_dhparam /etc/nginx/dhparam.pem;
        ssl_prefer_server_ciphers on;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4";
        //添加返回请求头
        add_header Strict-Transport-Security "max-age=31536000";
        add_header X-Xss-Protection 1;
        add_header X-Frame-Options DENY;
        location / {
            proxy_pass  http://127.0.0.1:3003;
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto https;
       }

      location /api {
        proxy_pass  https://127.0.0.1:8091;
        proxy_connect_timeout       120;
        proxy_send_timeout          120;
        proxy_read_timeout          120;
        send_timeout                120;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
      }

      location /api/test {
          proxy_set_header X-Forwarded-For $remote_addr;
          proxy_set_header Host $http_host;
          proxy_pass http://127.0.0.1:8080;
          proxy_redirect off;
          client_max_body_size 50M;
      }
}
}
//tcp代理
stream{
  upstream proxy_20000{
     //出现特点的状态,404,502,503,504,500,则认为后端服务区失效,失效次数超过3次,则设置30s的不可使用时间
     server 127.0.0.1:20000 weight=1 max_fails=3 fail_timeout=30s;
     server 127.0.0.2:20000 weight=1 max_fails=3 fail_timeout=30s;
  }
  server{
    listen 20000;
    allow all;
    proxy_pass proxy_20000;
    proxy_connect_timeout 24h;
    proxy_timeout 24h;
   }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121

负载均衡

nginx的负载均衡方式一共有四种: rr、ip_hash、fair、url_hash,前两种为自带,后两种为第三方插件,默认采用rr(轮询模式)

RR负载均衡模式

每个请求按时间顺序逐一分配到不同的后端服务器,如果超过了最大失败次数后(max_fails,默认1),在失效时间内(fail_timeout,默认10秒),该节点失效权重变为0,超过失效时间后,则恢复正常,或者全部节点都为down后,那么将所有节点都恢复为有效继续探测,一般来说rr可以根据权重来进行均匀分配。

IP_hash负载均衡模式

每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题,但是ip_hash会造成负载不均,有的服务请求接受多,有的服务请求接受少,所以不建议采用ip_hash模式,session共享问题可用后端服务的session共享代替nginx的ip_hash。

fair负载均衡模式

按后端服务器的响应时间来分配请求,响应时间短的优先分配.

url_hash负载均衡模式

对每个请求按url的hash结果分配,使每个URL定向到一个同 一个后端服务器,但是也会造成分配不均的问题,这种模式后端服务器为缓存时比较好。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值