Emiller’s Advanced Topics In Nginx Module Development

原文链接 https://www.evanmiller.org/nginx-modules-guide-advanced.html 相较于_Emiller’s Guide To Nginx Module Development_ 描述了编写 Nginx 简单处理程序、过滤器或负载均衡器的基础问题,这篇文档涵盖了三个高级主题:共享内存、子请求和解析,适合雄心勃勃的 Nginx 开发者。因为这些主题处于 Nginx 宇宙的边界上,所以这里的代码可能很少。示例可能已经过时。但是希望你不仅能够顺利完成,而且能够掌握一些额外的工具。 共享内存Shared Memory Nginx 在非线程化的情况下允许工作进程在它们之间共享内存。然而,这与标准池分配器有很大不同,因为共享段具有固定大小,并且在不重新启动 nginx 或以其他方式释放其内容的情况下无法调整大小。 提前声明 首先,警告黑客。本指南是在亲身体验 nginx 中的共享内存几个月后编写的,虽然我尽力做到准确(并花了一些时间刷新我的记忆),但不能保证它。你已被警告。 此外,这些知识 100% 来自阅读源代码和对核心概念进行逆向工程,因此可能有更好的方法来完成所描述的大部分内容。 哦,本指南基于 0.6.31,尽管据我所知 0.5.x 是 100% 兼容 ,而 0.7.x 也没有带来我所知道的破坏兼容性的变化。 有关 nginx 中共享内存的实际使用情况,请参阅我的 upstream_fair module。 这可能根本不适用于 Windows。过去他的出现更容易导致的coredump. 生成使用共享内存 要在 nginx 中创建共享内存段,您需要: 提供构造函数来初始化 调用 ngx_shared_memory_add 这两点包含了主要陷阱(我遇到过), 1 您的构造函数将被多次调用,您可以自行判断是否是第一次调用(并且应该设置一些东西),或者不是(并且可能应该不理会所有内容)。共享内存构造函数的原型如下所示: static ngx_int_t init(ngx_shm_zone_t *shm_zone, void *data); 数据变量将包含 oshm_zone->data 的内容,其中 oshm_zone 是“旧的”shm 区域描述符(稍后会详细介绍)。这个变量是唯一可以在重新加载后仍然存在的值,所以如果你不想丢失共享内存的内容,就必须使用它。 您的构造函数可能看起来与 upstream_fair 中的构造函数大致相似,即: static ngx_int_t init(ngx_shm_zone_t *shm_zone, void *data) { if (data) { /* we're being reloaded, propagate the data "cookie" */ shm_zone->data = data; return NGX_OK; } /* set up whatever structures you wish to keep in the shm */ /* initialise shm_zone->data so that we know we have been called; if nothing interesting comes to your mind, try shm_zone->shm.addr or, if you're desperate, (void*) 1, just set the value to something non-NULL for future invocations */ shm_zone->data = something_interesting; return NGX_OK; } 2 访问 shm 段时必须小心。 添加共享内存段的界面如下所示: ...

2023-03-30 · 9 min · 1748 words

X.509 certificates

 X.509是定义的一个公钥证书格式标准。 RFC 5280 详细描述公钥证书,包括它们的字段和扩展名。 ...

2023-03-09 · 1 min · 191 words

Emiller’s Guide To Nginx Module Development

原文链接:https://www.evanmiller.org/nginx-modules-guide.html 要充分理解网络服务器 Nginx,有助于理解漫画人物蝙蝠侠。 蝙蝠侠很快。 Nginx 很快。蝙蝠侠打击犯罪。 Nginx 与浪费的 CPU 周期和内存泄漏作斗争。蝙蝠侠在压力下表现出色。就 Nginx 而言,它在服务器负载很重的情况下表现出色。 但如果没有蝙蝠侠实用腰带,蝙蝠侠几乎什么都不是。 ...

2023-03-05 · 14 min · 2877 words

Why does calloc exist?

 原文链接:https://vorpus.org/blog/why-does-calloc-exist/ ...

2023-01-29 · 3 min · 598 words

leetcode - Monotonic Array

问题: An array is monotonic if it is either monotone increasing or monotone decreasing. An array nums is monotone increasing if for all i <= j, nums[i] <= nums[j]. An array nums is monotone decreasing if for all i <= j, nums[i] >= nums[j]. Given an integer array nums, return true if the given array is monotonic, or false otherwise. 解答: 单调数组,当数组个数大于2时, 遍历数组, 通过比较当当前值与前一个值的,确定其是递增还是递减, 遍历过程中,如果有变化那么则不满足单调性。 def isMonotonic(array): if len(array) <= 2: return True direction = array[1] - array[0] for i in range(2, len(array)): if direction == 0: direction = array[i] - array[i-1] continue if breakDirection(direction , array[i], array[i-1]): return False; return True; def breakDirection (direction, val, preval): diff = val - preval if direction > 0: return diff < 0 else: return diff > 0 ...

2023-01-23 · 1 min · 161 words

Linux sed 中的&

 一次使用sed 替换操作,替换后的字符串包含 **&,**替换文本如下 $ cat test.txt key=hello & 替换命令 $ sed 's/^key=.*$/key=world & /' test.txt 结果 key=world key=hello & sed 中 s 指令语法: ‘s/regexp/replacement/flags & 符号是代表 regexp 匹配的全部。所以又将原有匹配内容打印了一遍, 可以通过增加 \,将&按照普通字符处理 $ sed 's/^key=.*$/key=world \& /' test.txt key=world & 也可以使用 c 命令, 他之后的所有字符都被认为文本处理 $ sed '/^key=.*$/ckey=world & ' test.txt key=world & 参考及引用 更加详细使用 https://www.gnu.org/software/sed/manual/sed.html#Programming-Commands https://www.grymoire.com/Unix/Sed.html https://likegeeks.com/sed-linux/ 图片from 机滄泳

2022-12-03 · 1 min · 57 words

ubuntu 忘记密码

...

2022-12-03 · 1 min · 37 words

Nginx Development guide

这篇文章来自nginx document /http://nginx.org/en/docs/dev/development_guide.html nginx的开发指南 ...

2022-11-15 · 60 min · 12572 words

python tuple 一个元素时需要增加逗号

项目中使用python tuple时如果是一个元素一定要增加一个逗号,使用python3看下输出。 >>> t(0) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 't' is not defined >>> t=(0) >>> print(type(t)) <class 'int'> >>> print(t) 0 >>> t=(0,) >>> print(type(t)) <class 'tuple'> >>> print(t) (0,) 官方文档中描述: Note that it is actually the comma which makes a tuple, not the parentheses. The parentheses are optional, except in the empty tuple case, or when they are needed to avoid syntactic ambiguity. For example, f(a, b, c) is a function call with three arguments, while f((a, b, c)) is a function call with a 3-tuple as the sole argument. ...

2022-05-08 · 1 min · 139 words

单元测试

单元测试 单元测试的目的并不是查找bug,而是帮助我们更好的设计我们的代码,如何合理的来拆分我们的代码。 单元测试vs集成测试 集成测试检查各个组件间协作运行是否正常,单元测试检查应用程序中的一个某一个小的功能模块。 相关工具 以python为例 unittest nose or nose2 pytest 其中,nose和nose2基于unittest, 如果使用python2可以使用前两中,pytest要求python3.7+。pytest有较多插件, 显示内容更为丰富一些。 用官方例子比较一下: # content of test_sample.py def inc(x): return x + 1 def test_answer(): assert inc(3) == 5 直接运行pytest [garlic@centos8 pytest]$ pytest ========================================= test session starts ========================================= platform linux -- Python 3.6.8, pytest-7.0.1, pluggy-1.0.0 rootdir: /home/garlic/pytest/pytest collected 1 item test_sample.py F [100%] ============================================== FAILURES =============================================== _____________________________________________ test_answer _____________________________________________ def test_answer(): > assert inc(3) == 5 E assert 4 == 5 E + where 4 = inc(3) test_sample.py:7: AssertionError ======================================= short test summary info ======================================= FAILED test_sample.py::test_answer - assert 4 == 5 ========================================== 1 failed in 0.03s ========================================== [garlic@centos8 pytest]$ cat test_sample.py # content of test_sample.py def inc(x): return x + 1 def test_answer(): assert inc(3) == 5 如果用unittest要写成下面的样子: ...

2022-05-08 · 2 min · 375 words