Skip to content

Commit 7fba75b

Browse files
committed
update docs to mention lua-resty-http
1 parent 55faa12 commit 7fba75b

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

docs/utilities.md

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ The `lapis.http` module will attempt to select an HTTP client that works in the
590590
current server/environment. All of these modules should implement LuaSocket's
591591
`request` function interface. See: <https://lunarmodules.github.io/luasocket/http.html#request>
592592

593-
* When using Nginx: `lapis.nginx.http`
593+
* When using Nginx: `lapis.nginx.http` (automatically uses `lapis.nginx.resty_http` in timer phases)
594594
* When using Cqueues/lua-http: `http.compat.socket`
595595
* Default: LuaSocket's `socket.http` (Note: `luasec` is required to perform HTTPS requests)
596596

@@ -747,8 +747,11 @@ parameters of LuaSocket's request function.
747747
The `/proxy` location described above must exist in the same server block that
748748
is handling the original request for this function to work.
749749

750-
> If you are looking for a more flexible HTTP client that is specific to Nginx,
751-
> look at <https://github.com/ledgetech/lua-resty-http>
750+
> Lapis also provides `lapis.nginx.resty_http`, an HTTP client built on
751+
> [lua-resty-http](https://github.com/ledgetech/lua-resty-http). It is
752+
> automatically used in timer phases (where `location.capture` doesn't work),
753+
> but can also be used directly. See [Using
754+
> lua-resty-http](#using-lua-resty-http) below for more details.
752755
753756
**Parameters:**
754757

@@ -773,6 +776,59 @@ Every successful HTTP request increments the following performance metrics in th
773776
- `http_count`: This metric counts the total number of HTTP requests.
774777
- `http_time`: This metric measures the total time taken for HTTP requests. It is calculated as the difference between the current time and the start time of the request.
775778

779+
### Using lua-resty-http
780+
781+
Lapis provides an HTTP client built on
782+
[lua-resty-http](https://github.com/ledgetech/lua-resty-http) in the
783+
`lapis.nginx.resty_http` module. It implements the same LuaSocket `http.request`
784+
interface as the other Lapis HTTP clients.
785+
786+
This client is automatically used when making HTTP requests from timer phase
787+
contexts (`ngx.timer.*` callbacks), since nginx's `location.capture` doesn't
788+
work in those phases. You can also require and use it directly:
789+
790+
```lua
791+
local http = require("lapis.nginx.resty_http")
792+
local body, status, headers = http.request("https://example.com")
793+
```
794+
795+
**Key differences from proxy_pass approach:**
796+
797+
- No `/proxy` location block needed in nginx config
798+
- SSL verification is enabled by default
799+
- Requires lua-resty-http and lua-resty-openssl dependencies
800+
801+
**Dependencies:**
802+
803+
```bash
804+
luarocks install lua-resty-http
805+
luarocks install lua-resty-openssl
806+
```
807+
808+
#### Enabling HTTPS
809+
810+
To make HTTPS requests, you must configure nginx with the path to your system's
811+
CA certificates. Add the following directives to your `http` block in
812+
`nginx.conf`:
813+
814+
```nginx
815+
http {
816+
lua_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt;
817+
lua_ssl_verify_depth 2;
818+
819+
# ... rest of your configuration
820+
}
821+
```
822+
823+
The path to the CA certificates file varies by operating system (see the [SSL
824+
verification section above](#enabling-ssl-verification) for common paths).
825+
826+
When using the `resty` CLI tool, pass these directives via `--http-conf`:
827+
828+
```bash
829+
resty --http-conf "lua_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt; lua_ssl_verify_depth 2;" -e 'print(require("lapis.nginx.resty_http").request("https://example.com"))'
830+
```
831+
776832
### `http.simple(req, body)`
777833

778834
> This function is now deprecated. We recommend using the `http.request`

0 commit comments

Comments
 (0)