3
votes

I have a html form I am getting the username & password and I wanted to pass it on to a web server which require a basic authentication. This is a very minimalistic web server with nginx/lua support only (no 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 snip:

 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/;
    }
  }

My issue is:

In the form, if I define the "form action=/form_validate" the form data does not processed. Nginx side see this request as a "GET" method rather than a "POSt".

But, if I do define it as "form action=/form_validate/", the form data does get processed, but unfortunately my proxy_pass call get messed up. When it hits the proxied server, it does not know the path to all the java script, CSS & other files.

What am I doing wrong here?

1

1 Answers

0
votes

In the form, if I define the "form action=/form_validate" the form data does not processed. Nginx side see this request as a "GET" method rather than a "POSt".

This is a special feature of nginx. If you configure nginx with "location /form_validate/ { ... }" and request it with uri "/form_validate", nginx will response 301 status code and "Location: .../form_validate/" header.

You can get more details from the last part of location directive document .

But, if I do define it as "form action=/form_validate/", the form data does get processed, but unfortunately my proxy_pass call get messed up. When it hits the proxied server, it does not know the path to all the java script, CSS & other files.

If the url starts with "/form_validate/", such as "/form_validate/test.js", the proxied server will get path "/test.js". Using "proxy_pass http://web_svr;" without the trailing slash will not change original url.
If the url does not starts with "/form_validate/", the proxied server cannot get this request. Because this request is captured by location / {}.