How to return a cookie to a client when auth_request is used?

nginxuser100 nginx-forum at nginx.us
Mon Jan 19 01:50:05 UTC 2015


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!

http {
  server {
    listen       80;
    server_name  localhost;

    location / {
        auth_request /auth;
        fastcgi_param  HTTP_COOKIE $http_cookie;
        include fastcgi_params;
        auth_request_set $saved_set_cookie $upstream_http_set_cookie;
        add_header Set-Cookie $saved_set_cookie;
        fastcgi_pass   127.0.0.1:9000;
    }
   
    location = /auth {
        include fastcgi_params;
        fastcgi_param  HTTP_COOKIE $http_cookie;
        fastcgi_pass   127.0.0.1:9010;
    }
  }
}

The FCGI auth server's code sets the cookie as follows:

int main(int argc, char **argv) {
    int count = 0;
    while(FCGI_Accept() >= 0) {
        ...
        printf("Content-type: text/html\n\n"
        "Set-Cookie: name=AuthCookie\r\n"
               "<html><head><title>FastCGI 9010: Hello!</title></head>\n"
               "<body><h1>FastCGI 9010: Hello!</h1>\n"
               "Request number %d running on host <i>%s</i>\n",
               ++count, getenv("SERVER_NAME"));

        /* code to print the env variables */
        ....

        FCGI_Finish();
    }
    return 0;
}

-------------------------------------------------------------------------
The FCGI back-end server's code is as follows:

#include "fcgi_stdio.h"
#include <stdlib.h>

extern char **environ;

int main(int argc, char **argv) {
    int count = 0;

    while(FCGI_Accept() >= 0){
        char *contentLength = getenv("CONTENT_LENGTH");
        int packetRead = 0;
        int done = 0;
        int len;
        int idx;

        if (contentLength != NULL) {
            len = strtol(contentLength, NULL, 10);
        }
        else {
            len = 0;
        }

        /* Create a file to put output */
        FCGI_FILE * fileOut = FCGI_fopen("/tmp/fcgi.out", "w");
        if (fileOut) {
            while(done < len) {
                char buffer[1024];
                int i;

                packetRead = FCGI_fread(buffer, 1, sizeof(buffer), stdin);
                if (packetRead < 0) {
                    break;
                }
                if (packetRead > 0) {
                   FCGI_fwrite(buffer, 1, packetRead, fileOut);
                    done += packetRead;
                }


            }
            FCGI_fclose(fileOut);
        }

        printf("Content-type: text/html\n\n"
               "<html><head><title>FastCGI 9000: Hello!</title></head>\n"
               "<body><h1>FastCGI 9000: Hello!</h1>\n"
               "Request number=%d lenrx=%d pktRead=%d uri=%s reqMethod=%s
cookie=%s host= <i>%s</i></body></html>\n",
               ++count, len, packetRead, getenv("REQUEST_URI"),
getenv("REQUEST_METHOD"), getenv("HTTP_COOKIE"),
                getenv("SERVER_NAME"));

        /* Print the received environment variables */
        if ( !environ || environ[0] == NULL ){
            printf ("Null environment </body></html>\n");
            return 0;
        }
        for (idx = 0; environ[idx] != NULL; idx++)
        {
            printf("<h1>%s</h1>\n", environ[idx]);
        }
        printf ("</body></html>\n");

        FCGI_Finish();
    }
    return 0;
}

---------------------------------------------------------------
GET request/response, but no cookie in the response from the FastCGI
back-end server:

curl -v 'http://localhost:80/'

* About to connect() to localhost port 80 (#0)
*   Trying ::1... Connection refused
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (i386-redhat-linux-gnu) libcurl/7.19.7
NSS/3.16.2.3 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: localhost
> Accept: */*
> 
< HTTP/1.1 200 OK
< Server: nginx/1.7.9
< Date: Sun, 18 Jan 2015 17:20:23 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< 
<html><head><title>FastCGI 9000: Hello!</title></head>
<body><h1>FastCGI 9000: Hello!</h1>
Request number=2 lenrx=0 pktRead=0 uri=/ reqMethod=GET cookie= host=
<i>localhost</i></body></html>
<h1>FCGI_ROLE=RESPONDER</h1>
<h1>HTTP_COOKIE=</h1>
<h1>QUERY_STRING=</h1>
<h1>REQUEST_METHOD=GET</h1>
<h1>CONTENT_TYPE=</h1>
<h1>CONTENT_LENGTH=</h1>
<h1>SCRIPT_NAME=/</h1>
<h1>REQUEST_URI=/</h1>
<h1>DOCUMENT_URI=/</h1>
<h1>DOCUMENT_ROOT=/usr/local/nginx-1.7.9/html</h1>
<h1>SERVER_PROTOCOL=HTTP/1.1</h1>
<h1>GATEWAY_INTERFACE=CGI/1.1</h1>
<h1>SERVER_SOFTWARE=nginx/1.7.9</h1>
<h1>REMOTE_ADDR=127.0.0.1</h1>
<h1>REMOTE_PORT=41122</h1>
<h1>SERVER_ADDR=127.0.0.1</h1>
<h1>SERVER_PORT=80</h1>
<h1>SERVER_NAME=localhost</h1>
<h1>REDIRECT_STATUS=200</h1>
<h1>HTTP_USER_AGENT=curl/7.19.7 (i386-redhat-linux-gnu) libcurl/7.19.7
NSS/3.16.2.3 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2</h1>
<h1>HTTP_HOST=localhost</h1>
<h1>HTTP_ACCEPT=*/*</h1>
</body></html>
* Connection #0 to host localhost left intact
* Closing connection #0

--------------------------------------------
POST request failure:

curl -d "name=Rafael%20Sagula&phone=3320780" -v 'http://localhost:80/'

* About to connect() to localhost port 80 (#0)
*   Trying ::1... Connection refused
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 80 (#0)
> POST / HTTP/1.1
> User-Agent: curl/7.19.7 (i386-redhat-linux-gnu) libcurl/7.19.7
NSS/3.16.2.3 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: localhost
> Accept: */*
> Content-Length: 34
> Content-Type: application/x-www-form-urlencoded
> 
< HTTP/1.1 500 Internal Server Error
< Server: nginx/1.7.9
< Date: Sun, 18 Jan 2015 16:56:35 GMT
< Content-Type: text/html
< Content-Length: 192
< Connection: close
< 
<html>
<head><title>500 Internal Server Error</title></head>
<body bgcolor="white">
<center><h1>500 Internal Server Error</h1></center>
<hr><center>nginx/1.7.9</center>
</body>
</html>
* Closing connection #0

---------------------------------------------
A curl response that seems to be waiting for input:

curl -d "name=Rafael%20Sagula&phone=3320780" -v 'http://localhost:80/'

* About to connect() to localhost port 80 (#0)
*   Trying ::1... Connection refused
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 80 (#0)
> POST / HTTP/1.1
> User-Agent: curl/7.19.7 (i386-redhat-linux-gnu) libcurl/7.19.7
NSS/3.16.2.3 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: localhost
> Accept: */*
> Content-Length: 34
> Content-Type: application/x-www-form-urlencoded
> 
^C

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,256110,256187#msg-256187



More information about the nginx mailing list