{"id":13580076,"url":"https://github.com/openresty/lua-upstream-nginx-module","last_synced_at":"2025-04-06T00:30:42.124Z","repository":{"id":13170787,"uuid":"15853845","full_name":"openresty/lua-upstream-nginx-module","owner":"openresty","description":"Nginx C module to expose Lua API to ngx_lua for Nginx upstreams","archived":false,"fork":false,"pushed_at":"2023-11-23T11:41:42.000Z","size":90,"stargazers_count":497,"open_issues_count":29,"forks_count":151,"subscribers_count":53,"default_branch":"master","last_synced_at":"2024-02-13T09:12:09.167Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/openresty.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2014-01-13T00:14:19.000Z","updated_at":"2024-06-18T20:51:24.013Z","dependencies_parsed_at":"2023-11-23T12:44:59.117Z","dependency_job_id":null,"html_url":"https://github.com/openresty/lua-upstream-nginx-module","commit_stats":{"total_commits":70,"total_committers":11,"mean_commits":6.363636363636363,"dds":0.3571428571428571,"last_synced_commit":"542be0893543a4e42d89f6dd85372972f5ff2a36"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-upstream-nginx-module","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-upstream-nginx-module/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-upstream-nginx-module/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-upstream-nginx-module/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/openresty","download_url":"https://codeload.github.com/openresty/lua-upstream-nginx-module/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247419597,"owners_count":20936009,"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":[],"created_at":"2024-08-01T15:01:46.872Z","updated_at":"2025-04-06T00:30:41.817Z","avatar_url":"https://github.com/openresty.png","language":"C","funding_links":[],"categories":["C","Modules","Third Modules","Rust Modules","Lua and OpenResty ecosystem"],"sub_categories":["C Modules","Lua Modules"],"readme":"Name\n====\n\nngx_http_lua_upstream - Nginx C module to expose Lua API to ngx_lua for Nginx upstreams\n\nTable of Contents\n=================\n\n* [Name](#name)\n* [Status](#status)\n* [Synopsis](#synopsis)\n* [Functions](#functions)\n    * [get_upstreams](#get_upstreams)\n    * [get_servers](#get_servers)\n    * [get_primary_peers](#get_primary_peers)\n    * [get_backup_peers](#get_backup_peers)\n    * [set_peer_down](#set_peer_down)\n    * [current_upstream_name](#current_upstream_name)\n* [TODO](#todo)\n* [Compatibility](#compatibility)\n* [Installation](#installation)\n* [Author](#author)\n* [Copyright and License](#copyright-and-license)\n* [See Also](#see-also)\n\nStatus\n======\n\nThis module is production ready.\n\nSynopsis\n========\n\n```nginx\nhttp {\n    upstream foo.com {\n        server 127.0.0.1 fail_timeout=53 weight=4 max_fails=100;\n        server agentzh.org:81;\n    }\n\n    upstream bar {\n        server 127.0.0.2;\n    }\n\n    server {\n        listen 8080;\n\n        # sample output for the following /upstream interface:\n        # upstream foo.com:\n        #     addr = 127.0.0.1:80, weight = 4, fail_timeout = 53, max_fails = 100\n        #     addr = 106.184.1.99:81, weight = 1, fail_timeout = 10, max_fails = 1\n        # upstream bar:\n        #     addr = 127.0.0.2:80, weight = 1, fail_timeout = 10, max_fails = 1\n\n        location = /upstreams {\n            default_type text/plain;\n            content_by_lua_block {\n                local concat = table.concat\n                local upstream = require \"ngx.upstream\"\n                local get_servers = upstream.get_servers\n                local get_upstreams = upstream.get_upstreams\n\n                local us = get_upstreams()\n                for _, u in ipairs(us) do\n                    ngx.say(\"upstream \", u, \":\")\n                    local srvs, err = get_servers(u)\n                    if not srvs then\n                        ngx.say(\"failed to get servers in upstream \", u)\n                    else\n                        for _, srv in ipairs(srvs) do\n                            local first = true\n                            for k, v in pairs(srv) do\n                                if first then\n                                    first = false\n                                    ngx.print(\"    \")\n                                else\n                                    ngx.print(\", \")\n                                end\n                                if type(v) == \"table\" then\n                                    ngx.print(k, \" = {\", concat(v, \", \"), \"}\")\n                                else\n                                    ngx.print(k, \" = \", v)\n                                end\n                            end\n                            ngx.print(\"\\n\")\n                        end\n                    end\n                end\n            }\n        }\n    }\n}\n```\n\nFunctions\n=========\n\n[Back to TOC](#table-of-contents)\n\nget_upstreams\n-------------\n`syntax: names = upstream.get_upstreams()`\n\nGet a list of the names for all the named upstream groups (i.e., explicit `upstream {}` blocks).\n\nNote that implicit upstream groups created by `proxy_pass` and etc are excluded.\n\n[Back to TOC](#table-of-contents)\n\nget_servers\n-----------\n`syntax: servers = upstream.get_servers(upstream_name)`\n\nGet configurations for all the servers in the specified upstream group. Please note that one server may take multiple addresses when its server name can be resolved to multiple addresses.\n\nThe return value is an array-like Lua table. Each table entry is a hash-like Lua table that takes the following keys:\n\n* addr\n\n    socket address(es). can be either a Lua string or an array-like Lua table of Lua strings.\n* backup\n* fail_timeout\n* max_fails\n* name\n* weight\n\n[Back to TOC](#table-of-contents)\n\nget_primary_peers\n-----------------\n`syntax: peers = upstream.get_primary_peers(upstream_name)`\n\nGet configurations for all the primary (non-backup) peers in the specified upstream group.\n\nThe return value is an array-like Lua table for all the primary peers. Each table entry is a (nested) hash-like Lua table that takes the following keys:\n\n* current_weight\n* effective_weight\n* fail_timeout\n* fails\n* id\n\n    Identifier (ID) for the peer. This ID can be used to reference a peer in a group in the peer modifying API.\n* max_fails\n* name\n\n    Socket address for the current peer\n* weight\n* accessed\n\n    Timestamp for the last access (in seconds since the Epoch)\n* checked\n\n    Timestamp for the last check (in seconds since the Epoch)\n* down\n\n    Holds true if the peer has been marked as \"down\", otherwise this key is not present\n* conns\n\n    Number of active connections to the peer (this requires NGINX 1.9.0 or above).\n\n[Back to TOC](#table-of-contents)\n\nget_backup_peers\n----------------\n`syntax: peers = upstream.get_backup_peers(upstream_name)`\n\nGet configurations for all the backup peers in the specified upstream group.\n\nThe return value has the same structure as [get_primary_peers](#get_primary_peers) function.\n\n[Back to TOC](#table-of-contents)\n\nset_peer_down\n-------------\n`syntax: ok, err = upstream.set_peer_down(upstream_name, is_backup, peer_id, down_value)`\n\nSet the \"down\" (boolean) attribute of the specified peer.\n\nTo uniquely specify a peer, you need to specify the upstream name, whether or not it is a backup peer, and the peer id (starting from 0).\n\nNote that this method only changes the peer settings in the current Nginx worker\nprocess. You need to synchronize the changes across all the Nginx workers yourself if you\nwant a server-wide change (for example, by means of [ngx_lua](https://github.com/openresty/lua-nginx-module#ngxshareddict)'s [ngx.shared.DICT](https://github.com/openresty/lua-nginx-module#ngxshareddict)).\n\nBelow is an example. Consider we have a \"bar\" upstream block in `nginx.conf`:\n\n```nginx\nupstream bar {\n    server 127.0.0.2;\n    server 127.0.0.3 backup;\n    server 127.0.0.4 fail_timeout=23 weight=7 max_fails=200 backup;\n}\n```\n\nthen\n\n```lua\nupstream.set_peer_down(\"bar\", false, 0, true)\n```\n\nwill turn down the primary peer corresponding to `server 127.0.0.2`.\n\nSimilarly,\n\n```lua\nupstream.set_peer_down(\"bar\", true, 1, true)\n```\n\nwill turn down the backup peer corresponding to `server 127.0.0.4 ...`.\n\nYou can turn on a peer again by providing a `false` value as the 4th argument.\n\n[Back to TOC](#table-of-contents)\n\ncurrent_upstream_name\n---------------------\n`syntax: name = upstream.current_upstream_name()`\n\nReturns the name of the proxied upstream for the current request.\nIf there is no upstream for this request (no `proxy_pass` call), or this\nfunction is called in a phase prior to the content phase, then the return value\nwill be `nil`. If a port is explicitly included in the upstream definition or\n`proxy_pass` directive, it will be included in the return value of this function.\n\nExample:\n\n```lua\n-- upstream my_upstream { ... }\n-- proxy_pass http://my_upstream;\nupstream.current_upstream_name() --\u003e my_upstream\n\n-- proxy_pass http://example.com:1234;\nupstream.current_upstream_name() --\u003e example.com:1234\n```\n\nNote that implicit upstreams created by `proxy_pass` are included, contrary to\nthe output of `upstream.get_upstreams()`.\n\n[Back to TOC](#table-of-contents)\n\nTODO\n====\n\n* Add API to add or remove servers to existing upstream groups.\n\n[Back to TOC](#table-of-contents)\n\nCompatibility\n=============\n\nThe following versions of Nginx should work with this module:\n\n* **1.11.x** (last tested: 1.11.2)\n* **1.10.x**\n* **1.9.x**  (last tested: 1.9.15)\n* **1.8.x**\n* **1.7.x**  (last tested: 1.7.10)\n* **1.6.x**\n* **1.5.x**  (last tested: 1.5.12)\n\n[Back to TOC](#table-of-contents)\n\nInstallation\n============\n\nThis module is bundled and enabled by default in the [OpenResty](http://openresty.org) bundle. And you are recommended to use OpenResty.\n\n1. Grab the nginx source code from [nginx.org](http://nginx.org/), for example,\nthe version 1.11.2 (see [nginx compatibility](#compatibility)),\n2. then grab the source code of the [ngx_lua](https://github.com/openresty/lua-nginx-module#installation) as well as its dependencies like [LuaJIT](http://luajit.org/download.html).\n3. and finally build the source with this module:\n\n```bash\nwget 'http://nginx.org/download/nginx-1.11.2.tar.gz'\ntar -xzvf nginx-1.11.2.tar.gz\ncd nginx-1.11.2/\n\n# assuming your luajit is installed to /opt/luajit:\nexport LUAJIT_LIB=/opt/luajit/lib\n\n# assuming you are using LuaJIT v2.1:\nexport LUAJIT_INC=/opt/luajit/include/luajit-2.1\n\n# Here we assume you would install you nginx under /opt/nginx/.\n./configure --prefix=/opt/nginx \\\n    --with-ld-opt=\"-Wl,-rpath,$LUAJIT_LIB\" \\\n    --add-module=/path/to/lua-nginx-module \\\n    --add-module=/path/to/lua-upstream-nginx-module\n\nmake -j2\nmake install\n```\n\nStarting from NGINX 1.9.11, you can also compile this module as a dynamic module, by using the `--add-dynamic-module=PATH` option instead of `--add-module=PATH` on the\n`./configure` command line above. And then you can explicitly load the module in your `nginx.conf` via the [load_module](http://nginx.org/en/docs/ngx_core_module.html#load_module)\ndirective, for example,\n\n```nginx\nload_module /path/to/modules/ngx_http_lua_upstream_module.so;\n```\n\n[Back to TOC](#table-of-contents)\n\nAuthor\n======\n\nYichun \"agentzh\" Zhang (章亦春) \u003cagentzh@gmail.com\u003e, OpenResty Inc.\n\n[Back to TOC](#table-of-contents)\n\nCopyright and License\n=====================\n\nThis module is licensed under the BSD license.\n\nCopyright (C) 2014-2017, by Yichun \"agentzh\" Zhang, OpenResty Inc.\n\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:\n\n* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.\n\n* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n[Back to TOC](#table-of-contents)\n\nSee Also\n========\n* the ngx_lua module: http://github.com/openresty/lua-nginx-module#readme\n* the [lua-resty-upstream-healthcheck](https://github.com/openresty/lua-resty-upstream-healthcheck) library which makes use of the Lua API provided by this module.\n\n[Back to TOC](#table-of-contents)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenresty%2Flua-upstream-nginx-module","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopenresty%2Flua-upstream-nginx-module","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenresty%2Flua-upstream-nginx-module/lists"}