Reputation: 1709
I created a simple django app for user authentication, then I want to deploy on nginx with gunicorn. I create my custom user model with 2 fields: username, password.I have tested using:
python manange.py runserver 0.0.0.0:8000
It runs fine! But when I try to deploy on nginx with gunicorn, I got 500 internal server error. The command I used to test django with gunicorn is:
gunicorn --bind 127.0.0.1:8080 backend.wsgi:application
backend is my startapp. Here is my nginx config:
server {
listen 8000;
server_name localhost;
location / {
include proxy_params;
proxy_pass http://127.0.0.1:8080;
}
}
When I login from client site, it will send a post request with user's credentials to the server. At the server side, it successfully receive the request with correct credentials, but when it tries to authenticate the user by querying the User table:
User.objects.get(username=username, password=password)
It gets exception:
(1054, "Unknown column 'auth_user.last_login' in 'field list'")
Uhm strange!!!! Where does the last_login column come from? Then, I checked my log and found something weird when I run django app with gunicorn and without gunicorn.
When running with without nginx and gunicorn, here is the log when it queries User table:
2016-06-25 10:38:11,815|DEBUG|(0.001) SELECT `auth_user`.`id`, `auth_user`.`username`, `auth_user`.`password`, `auth_user`.`role` FROM `auth_user` LIMIT 21; args=()
When running with nginx and gunicorn:
2016-06-25 10:33:57,983|DEBUG|(0.000) SELECT `auth_user`.`id`, `auth_user`.`password`, `auth_user`.`last_login`, `auth_user`.`is_superuser`, `auth_user`.`username`, `auth_user`.`first_name`, `auth_user`.`last_name`, `auth_user`.`email`, `auth_user`.`is_staff`, `auth_user`.`is_active`, `auth_user`.`date_joined` FROM `auth_user` LIMIT 21; args=()
It seems that when running with nginx and gunicorn, it's not using my custom models. Anyone has any idea about this issue? Did I make any mistake when config nginx and gunicorn?
Upvotes: 3
Views: 5269
Reputation: 1709
After spending whole day of debugging, it turns out that my authentication app's name (which is auth) is conflict with django default auth. Somehow, when I serve my app through gunicorn, it used django default auth app instead of mine. If you look at the log carefully, it's querying the default User model of django.
To fix this issue, I go to my app settings and remove django.contrib.auth.middleware! :)
Upvotes: 2