{"id":15343693,"url":"https://github.com/spacewander/ngx_http_example_or_module","last_synced_at":"2025-04-15T03:15:38.004Z","repository":{"id":71091903,"uuid":"100161391","full_name":"spacewander/ngx_http_example_or_module","owner":"spacewander","description":null,"archived":false,"fork":false,"pushed_at":"2017-10-26T15:42:58.000Z","size":8,"stargazers_count":11,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T03:15:30.215Z","etag":null,"topics":["nginx-module","openresty-module"],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spacewander.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-08-13T07:36:43.000Z","updated_at":"2022-12-05T06:59:27.000Z","dependencies_parsed_at":"2023-03-06T01:30:34.407Z","dependency_job_id":null,"html_url":"https://github.com/spacewander/ngx_http_example_or_module","commit_stats":{"total_commits":3,"total_committers":1,"mean_commits":3.0,"dds":0.0,"last_synced_commit":"d51040bcf6cefe0c9d3a472833475c6b078cfeb9"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spacewander%2Fngx_http_example_or_module","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spacewander%2Fngx_http_example_or_module/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spacewander%2Fngx_http_example_or_module/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spacewander%2Fngx_http_example_or_module/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spacewander","download_url":"https://codeload.github.com/spacewander/ngx_http_example_or_module/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248997084,"owners_count":21195799,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nginx-module","openresty-module"],"created_at":"2024-10-01T10:48:48.484Z","updated_at":"2025-04-15T03:15:37.987Z","avatar_url":"https://github.com/spacewander.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"本项目是 [为 OpenResty 项目编写自定义 Nginx C 模块](https://segmentfault.com/a/1190000010637226) 一文中的示例代码。\n\n### 包含内容\n\n```\n├── config # Nginx 模块编译配置\n├── lib\n│   └── example_or.lua # 通过 FFI 调用 Nginx C 函数的 Lua 代码库\n├── ngx_http_example_or_module.c # Nginx C 模块代码文件\n└── t\n    └── example_or.t # 测试代码\n```\n\n### 如何安装一个 Nginx C 模块\n\n##### 静态链接\n\n编译 OpenResty 时，在 `./configure` 最后添加一个小尾巴：`--add-module=/path/to/ngx_http_example_or_module`。\n\n##### 动态加载\n\n切换 OpenResty 源码安装包下的 `bundle/nginx-*` 目录，执行 Nginx 的 `./configure`，带上 `--add-dynamic-module=/path/to/your/module` 参数。注意此处指定的参数需要同实际编译OpenResty 时一致（但移除仅跟 OpenResty 相关的选项）。因为 Nginx 在编译动态链接库时，需要确保 ABI 上的兼容（包括是否开启 ssl，额外引入的模块，等等）。比如：\n\n```\n./configure --with-cc-opt='-I/usr/local/Cellar/pcre/8.39/include/' --with-ld-opt='-L/usr/local/Cellar/pcre/8.39/lib' --with-openssl=/Users/lzx/lib/openssl-1.0.2h --with-http_realip_module --with-http_ssl_module --add-dynamic-module=/path/to/ngx_http_example_or_module\n```\n\n然后运行 `make modules`。\n\n最后在配置文件中加载编译出的动态链接库即可。\n```\nload_module /path/to/objs/ngx_http_example_or_module.so;\n```\n\n### 运行测试\n\n为了保证示例的正确性，我编写了相关的测试代码。\n运行测试的方式：首先确保 Nginx 已经通过静态链接的方式加载了示例模块，然后安装 `test-nginx`，运行 `prove -r t`。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspacewander%2Fngx_http_example_or_module","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspacewander%2Fngx_http_example_or_module","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspacewander%2Fngx_http_example_or_module/lists"}