I would like to have an FastCGI authentication app assign a cookie to a
client, and the Fast Auth app is called using auth_request. The steps
are as
follows:
Client sends a request
NGINX auth_request forwards the request to a FastCGI app to
authenticate.
The authentication FastCGI app creates a cookie, using “Set-Cookie:
name=value”. I would like this value to be returned to the client.
Assuming the authentication was successful, NGINX then forwards the
request to an upstream FastCGI app which sends a response to the client.
The
HTTP header should contain Set-Cookie: name=value
How do I get NGINX to include the cookie in the header that gets
forwarded
to the upstream module so the final response to the client contains the
cookie? I tried using auth_request_set but got
location / {
auth_request /auth;
include fastcgi_params;
fastcgi_param HTTP_COOKIE $http_cookie;
#auth_request_set $http_cookie "test"; <======= I tried this
just
to see how auth_request_set works. NGINX j
fastcgi_pass 127.0.0.1:9000;
}
# new fastcgi to set the cookie
location /auth {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9010;
}
Question 2. I also tried
auth_request_set $http_cookie “test”;
to see how auth_request_set works. NGINX gave me this error at
start
time
nginx: [emerg] the duplicate “http_cookie” variable in
/usr/local/nginx-1.7.9/conf/nginxWat.conf:25
Why did get such error?
Question 3. Can someone give me a pointer to a list of NGINX FastCGI
supported env variables such as $http_cookie / HTTP_COOKIE?
On Thu, Jan 15, 2015 at 03:11:23AM -0500, nginxuser100 wrote:
authenticate.
3. The authentication FastCGI app creates a cookie, using “Set-Cookie:
name=value”. I would like this value to be returned to the client.
4. Assuming the authentication was successful, NGINX then forwards the
request to an upstream FastCGI app which sends a response to the client. The
HTTP header should contain Set-Cookie: name=value
How do I get NGINX to include the cookie in the header that gets forwarded
to the upstream module so the final response to the client contains the
cookie? I tried using auth_request_set but got
You have to save the header value returned by the subrequest to a
variable with auth_request_set, and then add the header to a
response generated using the “add_header” directive. Something
like this should work:
Question 2. I also tried
auth_request_set $http_cookie “test”;
to see how auth_request_set works. NGINX gave me this error at start
time
nginx: [emerg] the duplicate “http_cookie” variable in
/usr/local/nginx-1.7.9/conf/nginxWat.conf:25
Why did get such error?
The $http_* variables are headers of a request, and you can’t
redefine them. Hence the error.
Question 3. Can someone give me a pointer to a list of NGINX FastCGI
supported env variables such as $http_cookie / HTTP_COOKIE?
All HTTP request headers are passed to FastCGI application as
HTTP_* params, and will be available to an application as
coresponding environment variables. Additional params are passed
as configured in your fastcgi_params file.
In case it will help someone else, the problem turned out to be in the
FastCGI auth server’s printf, the last “statement” of the HTTP header
should
end with \n\n instead of \r\n.
The following was wrong:
printf(“Content-type: text/html\n\n”
“Set-Cookie: name=AuthCookie\r\n”
“FastCGI 9010: Hello!\n”
…);
This did the trick:
printf(“Content-type: text/html\r\n”
“Set-Cookie: name=AuthCookie\n\n”
“FastCGI 9010: Hello!\n”
…);
Thank you Maxim, it is much better in the sense that I am not getting an
error at NGINX start time, but the FastCGI back-end server listening at
port
9000 does not seem to get the cookie set by the FastCGI auth server, nor
any
data from a POST request body or data generated by FastCGI auth app.
On a separate note, GET request would get a response, but a POST request
would get an Internal error. Also, after a few successful GET requests,
I
sometimes would get an incomplete response, as if it was waiting for
some
input.
Any idea what I might be missing?
Note that I verified the auth fastcgi app on its own, and it printed the
cookie. I verified the fastcgi back-end server on its own, and it
returns a
complete POST response.
Below is the code and curl requests/responses. Thanks much!