{"id":15175851,"url":"https://github.com/hashicorp/ngx_http_consul_backend_module","last_synced_at":"2025-10-19T10:30:31.320Z","repository":{"id":66024416,"uuid":"109997289","full_name":"hashicorp/ngx_http_consul_backend_module","owner":"hashicorp","description":"An nginx module for setting backends from Consul services.","archived":false,"fork":false,"pushed_at":"2023-03-20T16:12:19.000Z","size":4766,"stargazers_count":154,"open_issues_count":0,"forks_count":21,"subscribers_count":44,"default_branch":"master","last_synced_at":"2025-01-30T06:11:21.291Z","etag":null,"topics":["consul","nginx","nginx-consul-template"],"latest_commit_sha":null,"homepage":"https://www.consul.io/","language":"Go","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hashicorp.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-11-08T15:57:44.000Z","updated_at":"2024-12-13T07:45:49.000Z","dependencies_parsed_at":"2024-06-20T03:20:41.826Z","dependency_job_id":null,"html_url":"https://github.com/hashicorp/ngx_http_consul_backend_module","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp%2Fngx_http_consul_backend_module","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp%2Fngx_http_consul_backend_module/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp%2Fngx_http_consul_backend_module/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp%2Fngx_http_consul_backend_module/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hashicorp","download_url":"https://codeload.github.com/hashicorp/ngx_http_consul_backend_module/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237109378,"owners_count":19257125,"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":["consul","nginx","nginx-consul-template"],"created_at":"2024-09-27T12:43:10.683Z","updated_at":"2025-10-19T10:30:30.218Z","avatar_url":"https://github.com/hashicorp.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nginx + Consul Backend\n\nThis repository contains an [nginx](https://nginx.org) module extension for\ndynamically choosing a healthy backend by communicating directly with HashiCorp\n[Consul](https://www.consul.io/)'s API.\n\n**This code is for example purposes only. It demonstrates both the ability to\ncall Go code from C and the ability to link nginx directly to Consul! It is\nnot production ready and should be considered _inspiration only_.**\n\n\n## Usage\n\nThis module installs a `consul` directive inside the `location` block, and sets\nthe resulting `$backend` variable to one of the healthy IP:PORT by the given\nservice.\n\n```nginx\nhttp {\n  server {\n    listen       80;\n    server_name  example.com;\n\n    location /my-service {\n      consul $backend service-name;\n      proxy_pass http://$backend;\n    }\n  }\n}\n```\n\nIn this example, when a request to \"http://example.com/my-service\" comes to\nnginx, nginx invokes the `consul` directive and looks for a healthy service\nnamed \"service-name\", returning a random entry from the list. Then it utilized\nnginx's built-in `proxy_pass` to send traffic to that IP:PORT.\n\nTo put it another way, requests to \"http://example.com/my-service\" are\nload-balanced among the instances registered in a Consul service, using Consul's \nhealth checks to remove unhealthy backends automatically.\n\n\n## Architecture\n\nInstead of re-inventing the wheel, this module uses Consul's existing [Golang\nAPI client](https://github.com/hashicorp/consul/tree/master/api). The majority\nof the code is written in Go, with small glue pieces to wire it back into the\nrequired C code for nginx. This makes use of [golang shared C\nlibraries](http://blog.ralch.com/tutorial/golang-sharing-libraries/).\n\nPositively, this gains all the benefits of using the official API client\nlibrary, including configuration via familiar environment variables, connection\npooling and multiplexing, and time-tested stability. This saves the need to\nwrite and maintain a new client library written in pure C, and allows us to\nshowcase a really cool feature of Go - shared C libraries. Each request goes\nthrough Consul, meaning the probability of routing traffic to an unhealthy host\nis significantly lower than other solutions.\n\nNegatively, this requires Golang to compile the dynamic library `.so` file. In\ntheory, this could be compiled in advance by a CI/CD system. There is no need\nfor the Golang _runtime_, since the runtime is compiled into the dynamic library.\nAdditionally, each request goes through Consul. Thus using a local agent is\nrequired for performance and latency reasons.\n\nThe general flow is as follows:\n\n1. A request comes into nginx that matches a defined `location` block with a\n`consul` directive.\n\n1. nginx calls the `ngx_http_consul_backend` function with two arguments.\n\n  1. The first argument is the variable in which to store the result\n  (e.g. `$backend`).\n\n  1. The second argument is the name of the Consul service to route to\n  (e.g. `my-service`).\n\n1. The `ngx_http_consul_backend` calls `dlopen` on the shared C library (the\n`.so` file mentioned above), and executes the Go function by calling its symbol.\n\n1. The Go function communicates with Consul using the official API client\nlibrary, compiles a list of IP:PORT results, and then chooses a random result to\nreturn.\n\n1. The IP:PORT is returned to the `ngx_http_consul_backend` function, which then\nsets the result as the defined variable (e.g. `$backend`).\n\n1. Usually the next step is to use the built-in `proxy_pass` directive to send\ntraffic to that host.\n\n## Installation\n\nThis installation guide uses ubuntu/debian. Adapt as-needed for other platforms.\n\n### Prerequisites\n\n- [Golang](https://golang.org) \u003e= 1.9\n- Standard build tools, including GCC\n\n### Steps\n\n1. Install the necessary build tools:\n\n    ```sh\n    $ apt-get -yqq install build-essential curl git libpcre3 libpcre3-dev libssl-dev zlib1g-dev\n    ```\n\n1. Download and extract nginx source:\n\n    ```sh\n    $ cd /tmp\n    $ curl -sLo nginx.tgz https://nginx.org/download/nginx-1.12.2.tar.gz\n    $ tar -xzvf nginx.tgz\n    ```\n\n1. Download and extract the nginx development kit (ndk):\n\n    ```sh\n    $ cd /tmp\n    $ curl -sLo ngx_devel_kit-0.3.0.tgz https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz\n    $ tar -xzvf ngx_devel_kit-0.3.0.tgz\n    ```\n\n1. Download/clone this repository:\n\n    ```sh\n    $ git clone https://github.com/hashicorp/ngx_http_consul_backend_module.git /go/src/github.com/hashicorp/ngx_http_consul_backend_module\n    ```\n\n1. Compile the Go code as a shared C library which nginx will dynamically load.\nThis uses CGO and binds to the nginx development kit:\n\n    ```sh\n    $ cd /tmp/ngx_http_consul_backend_module/src\n    $ mkdir -p /usr/local/nginx/ext\n    $ CGO_CFLAGS=\"-I /tmp/ngx_devel_kit-0.3.0/src\" \\\n        go build \\\n          -buildmode=c-shared \\\n          -o /usr/local/nginx/ext/ngx_http_consul_backend_module.so \\\n          src/ngx_http_consul_backend_module.go\n    ```\n\n    This will compile the object file with symbols to\n    `/usr/local/nginx/ext/nginx_http_consul_backend_module.so`. Note that the\n    name and location of this file is important - it will be `dlopen`ed at\n    runtime by nginx.\n\n1. Compile and install nginx with the modules:\n\n    ```sh\n    $ cd /tmp/nginx-1.12.2\n    $ CFLAGS=\"-g -O0\" \\\n        ./configure \\\n          --with-debug \\\n          --add-module=/tmp/ngx_devel_kit-0.3.0 \\\n          --add-module=/go/src/github.com/hashicorp/ngx_http_consul_backend_module\n    $ make\n    $ make install\n    ```\n\n1. Add the required nginx configuration and restart nginx:\n\n    ```nginx\n    http {\n      server {\n        listen       80;\n        server_name  example.com;\n\n        location /my-service {\n          consul $backend service-name;\n          proxy_pass http://$backend;\n        }\n      }\n    }\n    ```\n\n    Unlike other solutions, you will not have to restart nginx each time a\n    change happens in the Consul services. Instead, because each request\n    delegates to Consul, you will get real-time results, and traffic will never\n    be routed to an unhealthy host!\n\n## Development\n\nThere is a sample Dockerfile and entrypoint which builds and runs this custom\nnginx installation with all required modules.\n\n## Alternatives\n\n- [Consul Template](https://github.com/hashicorp/consul-template)\n- [nginx upstream sync](https://github.com/weibocom/nginx-upsync-module)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhashicorp%2Fngx_http_consul_backend_module","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhashicorp%2Fngx_http_consul_backend_module","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhashicorp%2Fngx_http_consul_backend_module/lists"}