个人技术分享

使用服务器端的 Lua 脚本来动态生成 HTML 页面,并在生成页面时传递重定向参数。这种方法不涉及客户端 JavaScript,而是在服务器端完成重定向参数的设置。

以下是修改后的示例:

# 人机验证页面
location /captcha {
    default_type 'text/html';
    content_by_lua_block {
        local redirect_to = ngx.var.arg_redirect_to or "/"
        ngx.print([[
            <!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
                <title>CAPTCHA Verification</title>
                <script src="https://www.google.com/recaptcha/api.js" async defer></script>
            </head>
            <body>
                <form action="/verify_captcha" method="POST">
                    <div class="g-recaptcha" data-sitekey="your_site_key"></div>
                    <br/>
                    <input type="hidden" name="redirect_to" value="]] .. redirect_to .. [[">
                    <input type="submit" value="Submit">
                </form>
            </body>
            </html>
        ]])
    }
}

这样,无论用户是否启用 JavaScript,页面都能正常工作。同时,服务器端代码也更加简洁明了,不需要在客户端执行 JavaScript。