#9 Nginx proxy-set-header

2022-02-22

线上环境 HTTP 服务有报获取不到 Host 信息。应该是域名,但是拿到的是 backend 这样的名字。

我第一反应肯定是 Nginx 的配置问题,我印象中 Nginx 给 upstream 转发的请求就会设置 Host 为 upstream 名称。

但是我检查了 Nginx 的配置 (nginx.conf), 在 http 这一层的配置中有 proxy_set_header Host $http_host;

不过搜索 proxy_set_header 的时候发现,在 server 这一层设置了 proxy_set_header X-Forwarded-For $remote_addr;, 这个头在 http 这一层也设置了,没必要在设置的。

我就想到,会不会下层设置头会覆盖上一层的所有头设置。

在 Nginx 文档 (proxy_set_header) 中查到:

Allows redefining or appending fields to the request header passed to the proxied server.
The value can contain text, variables, and their combinations.
These directives are inherited from the previous configuration level if and only if there are no proxy_set_header directives defined on the current level.

当本层没有 proxy_set_header 指令的时候,才会继承上一级的 proxy_set_header 指令。

将 server 层重复的这个设置拿掉,果然就好了。

#8 Nginx 缓存导致的问题

2021-12-03

背景:公司有两套环境,其中有一个 Web 服务,包含一个比较耗时的接口。

今天收到反馈说环境 B 中的接口每次都是半天没有反应,等执行完一次性输出,A 环境中则是流式的,一点一点输出。
然后相关同事希望 A 环境体验好一些,希望 B 环境能和 A 环境保持一致。

经过检查,发现其中一遍经过了 Nginx 的代理,马上就想到了代理缓存的问题。

联系运维修改配置,在相关 server 中添加 proxy_buffering off;,问题果然解决。

#7 Nginx 版本

2020-12-26

Nginx Release History

参考: https://nginx.org/en/CHANGES-1.20

Version Date
1.20.2 16 Nov 2021
1.20.1 25 May 2021
1.20.0 20 Apr 2021
1.19.10 13 Apr 2021
1.19.9 30 Mar 2021
1.19.8 09 Mar 2021
1.19.7 16 Feb 2021
1.19.6 15 Dec 2020
1.19.5 24 Nov 2020
1.19.4 27 Oct 2020
1.19.3 29 Sep 2020
1.19.2 11 Aug 2020
1.19.1 07 Jul 2020
1.19.0 26 May 2020
1.17.10 14 Apr 2020
1.17.9 03 Mar 2020
1.17.8 21 Jan 2020
1.17.7 24 Dec 2019
1.17.6 19 Nov 2019
1.17.5 22 Oct 2019
1.17.4 24 Sep 2019
1.17.3 13 Aug 2019
1.17.2 23 Jul 2019
1.17.1 25 Jun 2019
1.17.0 21 May 2019
1.15.12 16 Apr 2019
1.15.11 09 Apr 2019
1.15.10 26 Mar 2019
1.15.9 26 Feb 2019
1.15.8 25 Dec 2018
1.15.7 27 Nov 2018
1.15.6 06 Nov 2018
1.15.5 02 Oct 2018
1.15.4 25 Sep 2018
1.15.3 28 Aug 2018
1.15.2 24 Jul 2018
1.15.1 03 Jul 2018
1.15.0 05 Jun 2018
1.13.12 10 Apr 2018
1.13.11 03 Apr 2018
1.13.10 20 Mar 2018
1.13.9 20 Feb 2018
1.13.8 26 Dec 2017
1.13.7 21 Nov 2017
1.13.6 10 Oct 2017
1.13.5 05 Sep 2017
1.13.4 08 Aug 2017
1.13.3 11 Jul 2017
1.13.2 27 Jun 2017
1.13.1 30 May 2017
1.13.0 25 Apr 2017
1.11.13 04 Apr 2017
1.11.12 24 Mar 2017
1.11.11 21 Mar 2017
1.11.10 14 Feb 2017
1.11.9 24 Jan 2017
1.11.8 27 Dec 2016
1.11.7 13 Dec 2016
1.11.6 15 Nov 2016
1.11.5 11 Oct 2016
1.11.4 13 Sep 2016
1.11.3 26 Jul 2016
1.11.2 05 Jul 2016
1.11.1 31 May 2016
1.11.0 24 May 2016
1.9.15 19 Apr 2016
1.9.14 05 Apr 2016
1.9.13 29 Mar 2016
1.9.12 24 Feb 2016
1.9.11 09 Feb 2016
1.9.10 26 Jan 2016
1.9.9 09 Dec 2015
1.9.8 08 Dec 2015
1.9.7 17 Nov 2015
1.9.6 27 Oct 2015
1.9.5 22 Sep 2015
1.9.4 18 Aug 2015
1.9.3 14 Jul 2015
1.9.2 16 Jun 2015
1.9.1 26 May 2015
1.9.0 28 Apr 2015
1.7.12 07 Apr 2015
1.7.11 24 Mar 2015
1.7.10 10 Feb 2015
1.7.9 23 Dec 2014
1.7.8 02 Dec 2014
1.7.7 28 Oct 2014
1.7.6 30 Sep 2014
1.7.5 16 Sep 2014
1.7.4 05 Aug 2014
1.7.3 08 Jul 2014
1.7.2 17 Jun 2014
1.7.1 27 May 2014
1.7.0 24 Apr 2014
1.5.13 08 Apr 2014
1.5.12 18 Mar 2014
1.5.11 04 Mar 2014
1.5.10 04 Feb 2014
1.5.9 22 Jan 2014
1.5.8 17 Dec 2013
1.5.7 19 Nov 2013
1.5.6 01 Oct 2013
1.5.5 17 Sep 2013
1.5.4 27 Aug 2013
1.5.3 30 Jul 2013
1.5.2 02 Jul 2013
1.5.1 04 Jun 2013
1.5.0 07 May 2013
1.4.0 24 Apr 2013
1.3.16 16 Apr 2013
1.3.15 26 Mar 2013
1.3.14 05 Mar 2013
1.3.13 19 Feb 2013
1.3.12 05 Feb 2013
1.3.11 10 Jan 2013
1.3.10 25 Dec 2012
1.3.9 27 Nov 2012
1.3.8 30 Oct 2012
1.3.7 02 Oct 2012
1.3.6 12 Sep 2012
1.3.5 21 Aug 2012
1.3.4 31 Jul 2012
1.3.3 10 Jul 2012
1.3.2 26 Jun 2012
1.3.1 05 Jun 2012
1.3.0 15 May 2012
1.2.0 23 Apr 2012
1.1.19 12 Apr 2012
1.1.18 28 Mar 2012
1.1.17 15 Mar 2012
1.1.16 29 Feb 2012
1.1.15 15 Feb 2012
1.1.14 30 Jan 2012
1.1.13 16 Jan 2012
1.1.12 26 Dec 2011
1.1.11 12 Dec 2011
1.1.10 30 Nov 2011
1.1.9 28 Nov 2011
1.1.8 14 Nov 2011
1.1.7 31 Oct 2011
1.1.6 17 Oct 2011
1.1.5 05 Oct 2011
1.1.4 20 Sep 2011
1.1.3 14 Sep 2011
1.1.2 05 Sep 2011
1.1.1 22 Aug 2011
1.1.0 01 Aug 2011
1.0.5 19 Jul 2011
1.0.4 01 Jun 2011
1.0.3 25 May 2011
1.0.2 10 May 2011
1.0.1 03 May 2011
1.0.0 12 Apr 2011
0.9.7 04 Apr 2011
0.9.6 21 Mar 2011
0.9.5 21 Feb 2011
0.9.4 21 Jan 2011
0.9.3 13 Dec 2010
0.9.2 06 Dec 2010
0.9.1 30 Nov 2010
0.9.0 29 Nov 2010
0.8.53 18 Oct 2010
0.8.52 28 Sep 2010
0.8.51 27 Sep 2010
0.8.50 02 Sep 2010
0.8.49 09 Aug 2010
0.8.48 03 Aug 2010
0.8.47 28 Jul 2010
0.8.46 19 Jul 2010
0.8.45 13 Jul 2010
0.8.44 05 Jul 2010
0.8.43 30 Jun 2010
0.8.42 21 Jun 2010
0.8.41 15 Jun 2010
0.8.40 07 Jun 2010
0.8.39 31 May 2010
0.8.38 24 May 2010
0.8.37 17 May 2010
0.8.36 22 Apr 2010
0.8.35 01 Apr 2010
0.8.34 03 Mar 2010
0.8.33 01 Feb 2010
0.8.32 11 Jan 2010
0.8.31 23 Dec 2009
0.8.30 15 Dec 2009
0.8.29 30 Nov 2009
0.8.28 23 Nov 2009
0.8.27 17 Nov 2009
0.8.26 16 Nov 2009
0.8.25 16 Nov 2009
0.8.24 11 Nov 2009
0.8.23 11 Nov 2009
0.8.22 03 Nov 2009
0.8.21 26 Oct 2009
0.8.20 14 Oct 2009
0.8.19 06 Oct 2009
0.8.18 06 Oct 2009
0.8.17 28 Sep 2009
0.8.16 22 Sep 2009
0.8.15 14 Sep 2009
0.8.14 07 Sep 2009
0.8.13 31 Aug 2009
0.8.12 31 Aug 2009
0.8.11 28 Aug 2009
0.8.10 24 Aug 2009
0.8.9 17 Aug 2009
0.8.8 10 Aug 2009
0.8.7 27 Jul 2009
0.8.6 20 Jul 2009
0.8.5 13 Jul 2009
0.8.4 22 Jun 2009
0.8.3 19 Jun 2009
0.8.2 15 Jun 2009
0.8.1 08 Jun 2009
0.8.0 02 Jun 2009
0.7.59 25 May 2009
0.7.58 18 May 2009
0.7.57 12 May 2009
0.7.56 11 May 2009
0.7.55 06 May 2009
0.7.54 01 May 2009
0.7.53 27 Apr 2009
0.7.52 20 Apr 2009
0.7.51 12 Apr 2009
0.7.50 06 Apr 2009
0.7.49 06 Apr 2009
0.7.48 06 Apr 2009
0.7.47 01 Apr 2009
0.7.46 30 Mar 2009
0.7.45 30 Mar 2009
0.7.44 23 Mar 2009
0.7.43 18 Mar 2009
0.7.42 16 Mar 2009
0.7.41 11 Mar 2009
0.7.40 09 Mar 2009
0.7.39 02 Mar 2009
0.7.38 23 Feb 2009
0.7.37 21 Feb 2009
0.7.36 21 Feb 2009
0.7.35 16 Feb 2009
0.7.34 10 Feb 2009
0.7.33 02 Feb 2009
0.7.32 26 Jan 2009
0.7.31 19 Jan 2009
0.7.30 24 Dec 2008
0.7.29 24 Dec 2008
0.7.28 22 Dec 2008
0.7.27 15 Dec 2008
0.7.26 08 Dec 2008
0.7.25 08 Dec 2008
0.7.24 01 Dec 2008
0.7.23 27 Nov 2008
0.7.22 20 Nov 2008
0.7.21 11 Nov 2008
0.7.20 10 Nov 2008
0.7.19 13 Oct 2008
0.7.18 13 Oct 2008
0.7.17 15 Sep 2008
0.7.16 08 Sep 2008
0.7.15 08 Sep 2008
0.7.14 01 Sep 2008
0.7.13 26 Aug 2008
0.7.12 26 Aug 2008
0.7.11 18 Aug 2008
0.7.10 13 Aug 2008
0.7.9 12 Aug 2008
0.7.8 04 Aug 2008
0.7.7 30 Jul 2008
0.7.6 07 Jul 2008
0.7.5 01 Jul 2008
0.7.4 30 Jun 2008
0.7.3 23 Jun 2008
0.7.2 16 Jun 2008
0.7.1 26 May 2008
0.7.0 19 May 2008
0.6.31 12 May 2008
0.6.30 29 Apr 2008
0.6.29 18 Mar 2008
0.6.28 13 Mar 2008
0.6.27 12 Mar 2008
0.6.26 11 Feb 2008
0.6.25 08 Jan 2008
0.6.24 27 Dec 2007
0.6.23 27 Dec 2007
0.6.22 19 Dec 2007
0.6.21 03 Dec 2007
0.6.20 28 Nov 2007
0.6.19 27 Nov 2007
0.6.18 27 Nov 2007
0.6.17 15 Nov 2007
0.6.16 29 Oct 2007
0.6.15 22 Oct 2007
0.6.14 15 Oct 2007
0.6.13 24 Sep 2007
0.6.12 21 Sep 2007
0.6.11 11 Sep 2007
0.6.10 03 Sep 2007
0.6.9 28 Aug 2007
0.6.8 20 Aug 2007
0.6.7 15 Aug 2007
0.6.6 30 Jul 2007
0.6.5 23 Jul 2007
0.6.4 17 Jul 2007
0.6.3 12 Jul 2007
0.6.2 09 Jul 2007
0.6.1 17 Jun 2007
0.6.0 14 Jun 2007
0.5.25 11 Jun 2007
0.5.24 06 Jun 2007
0.5.23 04 Jun 2007
0.5.22 29 May 2007
0.5.21 28 May 2007
0.5.20 07 May 2007
0.5.19 24 Apr 2007
0.5.18 19 Apr 2007
0.5.17 02 Apr 2007
0.5.16 26 Mar 2007
0.5.15 19 Mar 2007
0.5.14 23 Feb 2007
0.5.13 19 Feb 2007
0.5.12 12 Feb 2007
0.5.11 05 Feb 2007
0.5.10 26 Jan 2007
0.5.9 25 Jan 2007
0.5.8 19 Jan 2007
0.5.7 15 Jan 2007
0.5.6 09 Jan 2007
0.5.5 24 Dec 2006
0.5.4 15 Dec 2006
0.5.3 13 Dec 2006
0.5.2 11 Dec 2006
0.5.1 11 Dec 2006
0.5.0 04 Dec 2006
0.4.14 27 Nov 2006
0.4.13 15 Nov 2006
0.4.12 31 Oct 2006
0.4.11 25 Oct 2006
0.4.10 23 Oct 2006
0.4.9 13 Oct 2006
0.4.8 11 Oct 2006
0.4.7 10 Oct 2006
0.4.6 06 Oct 2006
0.4.5 02 Oct 2006
0.4.4 02 Oct 2006
0.4.3 26 Sep 2006
0.4.2 14 Sep 2006
0.4.1 14 Sep 2006
0.4.0 30 Aug 2006
0.3.61 28 Aug 2006
0.3.60 18 Aug 2006
0.3.59 16 Aug 2006
0.3.58 14 Aug 2006
0.3.57 09 Aug 2006
0.3.56 04 Aug 2006
0.3.55 28 Jul 2006
0.3.54 11 Jul 2006
0.3.53 07 Jul 2006
0.3.52 03 Jul 2006
0.3.51 30 Jun 2006
0.3.50 28 Jun 2006
0.3.49 31 May 2006
0.3.48 29 May 2006
0.3.47 23 May 2006
0.3.46 11 May 2006
0.3.45 06 May 2006
0.3.44 04 May 2006
0.3.43 26 Apr 2006
0.3.42 26 Apr 2006
0.3.41 21 Apr 2006
0.3.40 19 Apr 2006
0.3.39 17 Apr 2006
0.3.38 14 Apr 2006
0.3.37 07 Apr 2006
0.3.36 05 Apr 2006
0.3.35 22 Mar 2006
0.3.34 21 Mar 2006
0.3.33 15 Mar 2006
0.3.32 11 Mar 2006
0.3.31 10 Mar 2006
0.3.30 22 Feb 2006
0.3.29 20 Feb 2006
0.3.28 16 Feb 2006
0.3.27 08 Feb 2006
0.3.26 03 Feb 2006
0.3.25 01 Feb 2006
0.3.24 01 Feb 2006
0.3.23 24 Jan 2006
0.3.22 17 Jan 2006
0.3.21 16 Jan 2006
0.3.20 11 Jan 2006
0.3.19 28 Dec 2005
0.3.18 26 Dec 2005
0.3.17 18 Dec 2005
0.3.16 16 Dec 2005
0.3.15 07 Dec 2005
0.3.14 05 Dec 2005
0.3.13 05 Dec 2005
0.3.12 26 Nov 2005
0.3.11 15 Nov 2005
0.3.10 15 Nov 2005
0.3.9 10 Nov 2005
0.3.8 09 Nov 2005
0.3.7 27 Oct 2005
0.3.6 24 Oct 2005
0.3.5 21 Oct 2005
0.3.4 19 Oct 2005
0.3.3 19 Oct 2005
0.3.2 12 Oct 2005
0.3.1 10 Oct 2005
0.3.0 07 Oct 2005
0.2.6 05 Oct 2005
0.2.5 04 Oct 2005
0.2.4 03 Oct 2005
0.2.3 30 Sep 2005
0.2.2 30 Sep 2005
0.2.1 23 Sep 2005
0.2.0 23 Sep 2005
0.1.45 08 Sep 2005
0.1.44 06 Sep 2005
0.1.43 30 Aug 2005
0.1.42 23 Aug 2005
0.1.41 25 Jul 2005
0.1.40 22 Jul 2005
0.1.39 14 Jul 2005
0.1.38 08 Jul 2005
0.1.37 23 Jun 2005
0.1.36 15 Jun 2005
0.1.35 07 Jun 2005
0.1.34 26 May 2005
0.1.33 23 May 2005
0.1.32 19 May 2005
0.1.31 16 May 2005
0.1.30 14 May 2005
0.1.29 12 May 2005
0.1.28 08 Apr 2005
0.1.27 28 Mar 2005
0.1.26 22 Mar 2005
0.1.25 19 Mar 2005
0.1.24 04 Mar 2005
0.1.23 01 Mar 2005
0.1.22 22 Feb 2005
0.1.21 22 Feb 2005
0.1.20 17 Feb 2005
0.1.19 16 Feb 2005
0.1.18 09 Feb 2005
0.1.17 03 Feb 2005
0.1.16 25 Jan 2005
0.1.15 19 Jan 2005
0.1.14 18 Jan 2005
0.1.13 21 Dec 2004
0.1.12 06 Dec 2004
0.1.11 02 Dec 2004
0.1.10 26 Nov 2004
0.1.9 25 Nov 2004
0.1.8 20 Nov 2004
0.1.7 12 Nov 2004
0.1.6 11 Nov 2004
0.1.5 11 Nov 2004
0.1.4 26 Oct 2004
0.1.3 25 Oct 2004
0.1.2 21 Oct 2004
0.1.1 11 Oct 2004
0.1.0 04 Oct 2004

#6 Nginx location 指令

2019-05-22
Syntax : location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, location
  1. 执行顺序是:完整匹配(= uri) > 前缀匹配(^~ uri/uri) > 正则匹配(~/~*)。
  2. 优先级是:完整匹配 > 正则匹配 > 前缀匹配(越长优先级越高)。
  3. 完整匹配、正则匹配匹配上之后直接返回,前缀匹配则不是。

#5 Nginx: multi_accept

2019-02-05

http://nginx.org/en/docs/ngx_core_module.html#multi_accept

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

If multi_accept is disabled, a worker process will accept one new connection at a time. Otherwise, a worker process will accept all new connections at a time.

The directive is ignored if kqueue connection processing method is used, because it reports the number of new connections waiting to be accepted.

翻译

如果 multi_accept 禁用, worker 进程一次只接受一个连接
如果启用, 则会一次性接受所有连接。
如果连接处理方法为 kqueue, 则该指令被忽略, 因为 kqueue 会报告新连接的数量(然后好分配)。

PS: 关于连接处理方法, 参考 2017/02/07, Nginx 连接处理方法

理解

感觉有点迷糊, 那么究竟 multi_accept 的作用是什么呢?

Nginx 采用一个 master 进程和多个 worker 进程的模式工作。多个 worker 共享一个 socket (端口绑定), 当请求进来的时候, 被调度到的进程就会去 accept 连接。
multi_accept 的作用就是控制他一次拿走一个连接, 还是拿走所有等待中的连接。

这个参数可以让调度更加高效。如果请求数一直维持在一个很高的水平, 可以设置为 on。
但是在请求数不大的场景下, 那可能会导致同一时刻, 多个进程之间的负载就会不那么均衡, 总是 1C 干活, 7C 围观。

#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];