Nginx + mason_fcgi.pl + Set-Cookie problem

15 views Asked by At

I'm beginning to write in HTML::Mason, and trying to generate some page with cookies. The problem is, that all the documentation I can find uses only Apache+mod_perl, and this is not my case. I'm using CGI::Cookie + Nginx. The problem is, that when I call $cookie->bake function }even as the first text in my cgi), it's already in the body of the html part of http reply. So the cookie is visible at the returned page as a first text. I need to push this cookie into the header section, but don't know how. nginx configuration:

    server {
        listen 127.0.0.1;
        server_name test.local;

        access_log /var/log/nginx/test.access_log main;
        error_log /var/log/nginx/test.error_log info;
        location ~ \.png$ {
        root /var/www/mason/test;
        }
          location / {
            gzip off;
        # perl_set allow_globals;
            root /var/www/mason/;
            rewrite (.*/[^./]+$)    $1/index.html permanent;
            rewrite (.*)/$          $1/index.html permanent;

            index  index.html index.htm;

            fastcgi_pass    unix:/var/run/fcgi/mason_fcgi.sock;
            fastcgi_read_timeout    5m;
            # include /etc/nginx/nginx-fcgi.conf;
                include fastcgi.conf;
        }

the file fastcgi.conf:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

# httpoxy mitigation (https://httpoxy.org/ https://www.nginx.com/blog/?p=41962)
fastcgi_param  HTTP_PROXY         "";

everything else is working well, Mason is super. Just this... Thannks for hints.

1

There are 1 answers

0
tloudev On

So... the solution is quite easy. After all I found the

$cookie = CGI::Cookie->new( -name =>$session->name, value => $session->id );
$r->header_out('Set-Cookie'=>$cookie);

Wrong thing is, that the method $r->header_out sends just one cookie (if I call it twice for two different cookies, anly the last one is sent). But I can live with this. Thanks, folks