ngineo
By:
ngineo

can you set up cookies in nginx?

November 1, 2016 117 views
Nginx

i am working on AWS Elastic Beanstalk Instance, which runs Java application served through Nginx ( no load balancer in front, just a standalone instance )
I need to set cookie to catch client ip and client hostname. Is this possible to do it in nginx or should this be done in my Java code?

Below if my nginx configuration file:

location / {
proxypass http://127.0.0.1:5000;
proxy
http_version 1.1;

proxysetheader Connection $connectionupgrade;
proxy
setheader Upgrade $httpupgrade;
proxysetheader Host $host;
proxysetheader X-Real-IP $remoteaddr;
proxy
setheader X-Forwarded-For $proxyaddxforwarded_for;
}

1 Answer

You can try playing around with nginxs add_header Set-Cookie.

IP address of client should be in $remote_addr and host in $Host.

You can try adding following in location block:

add_header Set-Cookie cip=$remote_addr;
add_header Set-Cookie chost=$Host;

For more control you can try

add_header Set-Cookie "cip=$remote_addr;Domain=example.com;Path=/;Max-Age=100000";

I didn't tried it myself, I'm not sure is it correct, but test. I will try it myself but I'm out of my test server and can't verify now. :)

Have another answer? Share your knowledge.