Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django- session cookies and sites on multiple ports

Tags:

cookies

django

I have multiple Django projects running on one server using gunicorn and nginx. Currently they are each configured to run on a unique port of the same IP address using the server directive in nginx. All this works fine.

...
server {
    listen 81;
    server_name my.ip.x.x;
    ... #static hosting and reverse proxy to site1
}
server {
    listen 84;
    server_name my.ip.x.x;
    ... #static hosting and reverse proxy to site2
}
...

I came across a problem when I had 2 different projects open in 2 tabs and I realized that I could not be logged into both sites at once (both use the built-in Django User model and auth). Upon inspecting the cookies saved in my browser, I realized that the cookie is bound to just the domain name (in my case just an ip address) and it does not include the port.

On the second site, I tried changing SESSION_COOKIE_NAME annd SESSION_COOKIE_DOMAIN, but it doesn't seem to be working and with these current settings I can't even log in.

SESSION_COOKIE_DOMAIN = 'my.ip.x.x:84'  #solution is to leave this as default
SESSION_COOKIE_NAME = 'site2'           #just using this works 
SESSION_COOKIE_PATH = '/'               #solution is to leave this as default

#site1 is using all default values for these

What do I need to do to get cookies for both sites working independently?

like image 259
j_syk Avatar asked Sep 15 '11 15:09

j_syk


1 Answers

Just change the SESSION_COOKIE_NAME. The SESSION_COOKIE_DOMAIN doesn't support port numbers afaik. So they are all the same for your apps.

like image 125
Reto Aebersold Avatar answered Nov 06 '22 05:11

Reto Aebersold