#4 惊群与 accept_mutex

2017-04-16

多个进程共享一个 bind socket 时,会发生这样的情况:一个连接进来,所有相关进程都从睡眠状态被唤醒。然后其中一个进程拿到这个连接,其他进程就继续 sleep。
这种现象就叫做惊群。好处就是如果来了大量连接的话,大家一起干活(可能可以算是正向的影响),坏处就是,如果只来了少量的连接,白白吵醒一众进程,消耗 CPU 资源。
这个好处像没有一样,资源的浪费确是实打实的。

注意,新版本 Nginx 上不需要互斥锁,默认没有惊群现象。

accept_mutex

Syntax: accept_mutex on | off;
Default: accept_mutex off;
Context: events

If accept_mutex is enabled, worker processes will accept new connections by turn. Otherwise, all worker processes will be notified about new connections, and if volume of new connections is low, some of the worker processes may just waste system resources.

There is no need to enable accept_mutex on systems that support the EPOLLEXCLUSIVE flag (1.11.3) or when using reuseport.

Prior to version 1.11.3, the default value was on.

如果 accept_mutex 启用,worker 进程会按顺序接受连接。否则,所有 worker 进程都会收到新连接通知,也就是惊群了。

1.11.3 之前,accept_mutex 默认是 on。为什么现在默认 off 了呢?
文档有写, 因为 1.11.3 之后,支持 EPOLLEXCLUSIVE 标记的平台,默认就不会发生惊群了,不需要 accept_mutex。

此外:如果使用了 TCP reuseport(需要 Linux 内核支持), 就不需要启用 accept_mutex。

如果 reuseport 参考: 2019/02/05, reuseport: 端口复用
PS: 2015/05 发布的 1.9.1 及之后版本,Nginx 引入了 reuseport 指令。

accept_mutex_delay

Syntax: accept_mutex_delay time;
Default: accept_mutex_delay 500ms;
Context: events

If accept_mutex is enabled, specifies the maximum time during which a worker process will try to restart accepting new connections if another worker process is currently accepting new connections.

如果 accept_mutex 启用,那么这个参数指定了 worker 进程申请 accept_mutex 锁失败之后的 sleep 时长。

也就是说如果出现竞争,没有抢到锁的进程就要睡眠一段时间,避免无效的反复申请锁。不过,感觉默认的这个 0.5 秒感觉有点长,50ms 应该就够了吧。

实现

参考:深入浅出 Linux 惊群:现象、原因和解决方案

附: 睡眠队列(等待队列)

#3 Nginx 连接处理方法

2017-02-07

Syntax: use method;
Default: —
Context: events

Specifies the connection processing method to use. There is normally no need to specify it explicitly, because nginx will by default use the most efficient method.

Nginx 默认会自动选择当前平台最高效的方法。

  • epoll Linux 平台上的最佳选择
  • kqueue BSD 家族(包括 MacOS)的最佳选择
  • poll 第一备选
  • select 第二备选
  • eventport Solaris 提供的机制,但是 Nginx 文档上建议使用 /dev/poll
  • /dev/poll

我们接触多的是 Linux 服务器,所以知道是 epoll 就行了。
除非你非常知道自己在做什么,不要调整 use 参数。

以下参数可以控制相关模块的引入:

--with-poll_module
--without-poll_module
--with-select_module
--without-select_module

#2 利用 Nginx 做正向代理

2016-06-26

HTTP 代理

server {
    resolver 223.5.5.5;
    listen 80;
    location / {
        proxy_pass http://$host$request_uri;
        proxy_buffers 256 4k;
        proxy_max_temp_file_size 0k;
        proxy_connect_timeout 30;
        proxy_send_timeout 60;
        proxy_read_timeout 60;
        proxy_next_upstream error timeout invalid_header http_502;
    }
}

server {
    resolver 223.5.5.5;
    listen 443;
    location / {
        proxy_pass https://$host$request_uri;
        proxy_buffers 256 4k;
        proxy_max_temp_file_size 0k;
        proxy_connect_timeout 30;
        proxy_send_timeout 60;
        proxy_read_timeout 60;
        proxy_next_upstream error timeout invalid_header http_502;
    }
}

proxy_cache_path /data/Resources/PyPI/ levels=1:1:1 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;

server {
    location /pypi/ {
        proxy_cache pypi_cache;
        proxy_cache_revalidate on;
        proxy_cache_min_uses 3;
        proxy_cache_use_stale error timeout updating
                              http_500 http_502 http_503 http_504;
        proxy_cache_background_update on;
        proxy_cache_lock on;
        proxy_pass http://pypi.douban.com/simple/$request_uri;
    }
}

#1 Nginx sticky 指令

2016-04-10

Nginx 商用版 1.5.7 (2013 年) 开始支持 sticky 指令。

文档: https://nginx.org/en/docs/http/ngx_http_upstream_module.html#sticky

Syntax:  sticky cookie name [expires=time] [domain=domain] [httponly] [samesite=strict|lax|none] [secure] [path=path];
         sticky route $variable ...;
         sticky learn create=$variable lookup=$variable zone=name:size [timeout=time] [header] [sync];
Default: —
Context: upstream

开源版有 nginx-sticky-module。

C 语言写的,BSD 协议。
代码看起来并不复杂,如果需要用到的时候,可以进行一个粗略的 Review。

编译

# 下载 nginx 源码 (2016-04-26)
wget http://nginx.org/download/nginx-1.10.0.tar.gz http://nginx.org/download/nginx-1.10.0.tar.gz.asc
gpg --verify nginx-1.10.0.tar.gz.asc

# 下载 nginx-sticky-module (2015-08-06)
wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/1.2.6.zip

unzip 1.2.6.zip
cd nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d/

unzip nginx-1.10.0.zip
cd nginx-1.10.0/

# 其他参数按需添加
./configure --add-module=../nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d/
make && make install

# Configuration summary
#   + using system PCRE library
#   + OpenSSL library is not used
#   + md5: using system crypto library
#   + sha1: using system crypto library
#   + using system zlib library

#   nginx path prefix: "/usr/local/nginx"
#   nginx binary file: "/usr/local/nginx/sbin/nginx"
#   nginx modules path: "/usr/local/nginx/modules"
#   nginx configuration prefix: "/usr/local/nginx/conf"
#   nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
#   nginx pid file: "/usr/local/nginx/logs/nginx.pid"
#   nginx error log file: "/usr/local/nginx/logs/error.log"
#   nginx http access log file: "/usr/local/nginx/logs/access.log"
#   nginx http client request body temporary files: "client_body_temp"
#   nginx http proxy temporary files: "proxy_temp"
#   nginx http fastcgi temporary files: "fastcgi_temp"
#   nginx http uwsgi temporary files: "uwsgi_temp"
#   nginx http scgi temporary files: "scgi_temp"

用法

upstream {
  sticky;
  server 127.0.0.1:9000;
  server 127.0.0.1:9001;
  server 127.0.0.1:9002;
}

sticky [name=route] [domain=.foo.bar] [path=/] [expires=1h]
       [hash=index|md5|sha1] [no_fallback] [secure] [httponly];