Report this

What is the reason for this report?

can you set up cookies in nginx?

Posted on November 1, 2016

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 / { proxy_pass http://127.0.0.1:5000; proxy_http_version 1.1;

proxy_set_header Connection $connection_upgrade; proxy_set_header Upgrade $http_upgrade; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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. :)

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.