Question

can you set up cookies in 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 / { 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; }


Submit an answer

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!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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