JWT#

jwt 模块用于验证 JSON Web Token (JWT),通过指定的密钥完成。 与 auth_jwt 模块不兼容。

加载模块#

main{} 上下文中启用模块:

load_module modules/ngx_http_auth_jwt_module.so;

配置示例#

http {
    server {
        auth_jwt_key "0123456789abcdef" hex;
        auth_jwt     off;

        # 默认使用 Authentication 头部认证
        location /secured-by-auth-header/ {
            auth_jwt on;
        }

        # 使用 cookie 进行认证
        location /secured-by-cookie/ {
            auth_jwt $cookie_MyCookieName;
        }

        # 可以继承也可重写 key
        location /secured-by-auth-header-too/ {
            auth_jwt_key "another-secret";
            auth_jwt on;
        }

        # 使用 RSA 公钥验证
        location /secured-by-rsa-key/ {
            auth_jwt_key /etc/keys/rsa-public.pem file;
            auth_jwt on;
        }

        location /not-secure/ {}
    }
}

更多信息#

详细文档和源代码地址: max-lt/nginx-jwt-module