wasm-nginx-module

The wasm-nginx-module is an Nginx module built upon OpenResty. By implementing the Proxy Wasm ABI, any Wasm program written with Proxy Wasm SDK can be run inside it. Hence, you can write Go or Rust code, compile them into Wasm, then load & execute it in Nginx.

The wasm-nginx-module is already used in APISIX and allows it to run Wasm plugin like Lua plugin.

In order to follow along the tutorials in this chapter, you will need to first build your Nginx with wasm-nginx-module included and WasmEdge shared library installed in the right path.

Once you have Nginx installed, let me show you a real world example - using Wasm to inject custom responses in Nginx.

Inject Custom Response via Go in Nginx, Step by Step

Step 1: Write code based on proxy-wasm-go-sdk

The implementation code (including go.mod and others) can be found at here.

It should be explained that although the proxy-wasm-go-sdk project carries the Go name, it actually uses tinygo instead of native Go, which has some problems supporting WASI (which you can think of as a non-browser WASM runtime interface), see here for more details.

We also provide a Rust version (including Cargo.toml and others) there.

Step 2: Build the corresponding Wasm file

  1. tinygo build -o ./fault-injection/main.go.wasm -scheduler=none -target=wasi ./fault-injection/main.go

Step 3: Load and execute the Wasm file

Then, start Nginx with the configuration below:

  1. worker_processes 1;
  2. error_log /tmp/error.log warn;
  3. events {
  4. worker_connections 10240;
  5. }
  6. http {
  7. wasm_vm wasmedge;
  8. init_by_lua_block {
  9. local wasm = require("resty.proxy-wasm")
  10. package.loaded.plugin = assert(wasm.load("fault_injection",
  11. "/path/to/fault-injection/main.go.wasm"))
  12. }
  13. server {
  14. listen 1980;
  15. location / {
  16. content_by_lua_block {
  17. local wasm = require("resty.proxy-wasm")
  18. local ctx = assert(wasm.on_configure(package.loaded.plugin,
  19. '{"http_status": 403, "body": "powered by wasm-nginx-module"}'))
  20. assert(wasm.on_http_request_headers(ctx))
  21. }
  22. }
  23. }
  24. }

This configuration loads the Wasm file we just built, executes it with the configuration {"http_status": 403, "body": "powered by wasm-nginx-module"}.

Step 4: verify the result

After Nginx starts, we can use curl http://127.0.0.1:1980/ -i to verify the execution result of the Wasm.

It is expected to see the output:

  1. HTTP/1.1 403 Forbidden
  2. ...
  3. powered by wasm-nginx-module

Inject Custom Response via Rust in Nginx, Step by Step

Step 1: Write code based on proxy-wasm-rust-sdk

We also provide a Rust version (including Cargo.toml and others) there.

Step 2: Build the corresponding Wasm file

  1. cargo build --target=wasm32-wasi

Step 3: Load and execute the Wasm file

Then, start Nginx with the configuration below:

  1. worker_processes 1;
  2. error_log /tmp/error.log warn;
  3. events {
  4. worker_connections 10240;
  5. }
  6. http {
  7. wasm_vm wasmedge;
  8. init_by_lua_block {
  9. local wasm = require("resty.proxy-wasm")
  10. package.loaded.plugin = assert(wasm.load("fault_injection",
  11. "/path/to/fault-injection/target/wasm32-wasi/debug/fault_injection.wasm"))
  12. }
  13. server {
  14. listen 1980;
  15. location / {
  16. content_by_lua_block {
  17. local wasm = require("resty.proxy-wasm")
  18. local ctx = assert(wasm.on_configure(package.loaded.plugin,
  19. '{"http_status": 403, "body": "powered by wasm-nginx-module"}'))
  20. assert(wasm.on_http_request_headers(ctx))
  21. }
  22. }
  23. }
  24. }

This configuration loads the Wasm file we just built, executes it with the configuration {"http_status": 403, "body": "powered by wasm-nginx-module"}.

Step 4: verify the result

After Nginx starts, we can use curl http://127.0.0.1:1980/ -i to verify the execution result of the Wasm.

It is expected to see the output:

  1. HTTP/1.1 403 Forbidden
  2. ...
  3. powered by wasm-nginx-module