Nginx lua processing form data
I have a html form. I am receiving a username and password and I wanted to pass it to a web server that requires basic authentication. It is a very minimalistic webserver with nginx / lua support (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 problem:
In the form, if I define "form action = / form_validate", the form data is not processed. The Nginx side sees this request as a "GET" method, not a "POSt" method.
But, if I define it as "form action = / form_validate /", the form data is processed, but unfortunately my proxy_pass call gets messed up. When it hits the proxy, it doesn't know the path to all java script, CSS and other files.
What am I doing wrong here?
source to share
In the form, if I define "form action = / form_validate", the form data is not processed. The Nginx side sees this request as a "GET" method, not a "POSt" method.
This is a feature of nginx. If you configure nginx with "location / form_validate / {...}" and request it with uri "/form_validate"
, nginx will receive a 301 status code response and header "Location: .../form_validate/"
.
You can get more details from the last part of the location directive document .
But, if I define it as "form action = / form_validate /", the form data is processed, but unfortunately my proxy_pass call gets messed up. When it hits the proxy, it doesn't know the path to all java script, CSS and other files.
If the url starts with "/form_validate/"
, for example "/form_validate/test.js"
, the proxy will get the path "/test.js"
. Using "proxy_pass http://web_svr;"
without the trailing slash will not change the original url.
If the URL does not start with "/form_validate/"
, the proxy cannot receive this request. Since this request is being hijacked location / {}
.
source to share