【问题标题】:nginx lua processing form datanginx lua处理表单数据
【发布时间】:2015-10-03 14:52:02
【问题描述】:

我有一个 html 表单,我正在获取用户名和密码,我想将其传递给需要基本身份验证的 Web 服务器。这是一个非常简约的 Web 服务器,仅支持 nginx/lua(不支持 php/perl/python)。

index.html:

<form action="/form_validate" method="POST">
<label for="username">Username</label>
<input id="username" name="username" size="16" type="text"/>
<label for="password">Password</label>
<input name="password" size="16" type="password"/>
</form>

nginx.conf 截图:

 upstream web_svr 
  {
    least_conn;
    server 127.0.0.1:8080;
  }


  server
  {
    listen       80;
    server_name  testSvr;

    location / 
    {
      root /var/html;
      index index.html;
    }

    location /form_validate/
    {
      set_form_input $username username;
      set_form_input $password password;

      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Authorization "Basic $digest";
      proxy_redirect off;

      log_by_lua '
        ngx.header.content_type = ("text/plain")
        ngx.log(ngx.ERR,"username: (",ngx.var.username,")")
        ngx.log(ngx.ERR,"password: (",ngx.var.password,")")
      ';
      proxy_pass http://web_svr/;
    }
  }

我的问题是:

在表单中,如果我定义“form action=/form_validate”,则不会处理表单数据。 Nginx 方面将此请求视为“GET”方法而不是“POST”。

但是,如果我将其定义为“form action=/form_validate/”,则表单数据会得到处理,但不幸的是我的 proxy_pass 调用搞砸了。当它访问代理服务器时,它不知道所有 java 脚本、CSS 和其他文件的路径。

我在这里做错了什么?

【问题讨论】:

    标签: forms nginx lua


    【解决方案1】:

    在表单中,如果我定义“form action=/form_validate”,则不会处理表单数据。 Nginx 方面将此请求视为“GET”方法而不是“POST”。

    这是 nginx 的一个特殊功能。如果您使用“location /form_validate/ { ... }”配置 nginx 并使用 uri "/form_validate" 请求它,则 nginx 将响应 301 状态代码和 "Location: .../form_validate/" 标头。

    您可以从location directive document的最后部分获得更多详细信息。

    但是,如果我将其定义为“form action=/form_validate/”,则表单数据会得到处理,但不幸的是我的 proxy_pass 调用搞砸了。当它访问代理服务器时,它不知道所有 java 脚本、CSS 和其他文件的路径。

    如果url以"/form_validate/"开头,比如"/form_validate/test.js",则代理服务器会得到路径"/test.js"。使用不带斜杠的"proxy_pass http://web_svr;" 不会更改原始网址。
    如果 url 不以"/form_validate/" 开头,则代理服务器无法获取此请求。因为这个请求被location / {}捕获了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-13
      • 2020-08-15
      • 1970-01-01
      • 2021-03-21
      • 1970-01-01
      • 2012-10-18
      • 2018-12-07
      相关资源
      最近更新 更多