Here’s how you fix it:
You must first make sure you have proxy_set_header Host $host;
& the server-IP:8001 in the proxy_pass
instead of 127.0.0.1:8001 under the location directive in your nginx project config file.
Open your Django project settings.py in /opt/projectname/projectname/projectname.setting.py or /opt/projectname/projectname/projectname/projectname/settings.py (notice added project directory) and set the ALLOWED_HOSTS
to [“yourserverip”]. Example: ALLOWED_HOSTS = ["25.22.12.21"]
If you have started Gunicorn, then while in virtualenv, type ps ax|grep gunicorn
, It will give you a list of gunicorn processes, on the left you will see the process id of these processes, you may kill each of them my typing kill xxxx
and then make sure Gunicorn is not running by typing bg
, It will not return the following “Job 1 in already in background”.
Finally, service nginx restart
and it should work.
Here are my files for example:
/etc/nginx/sites-available/projectname (nginx project config file)
server {
listen 80;
server_name 25.22.12.21;
access_log off;
location /static/ {
alias /opt/projectname/static/;
}
location / {
proxy_set_header Host $host;
proxy_pass http://25.22.12.21:8001;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
/opt/projectname/projectname/projectname/settings.py (project settings.py)
"""
Django settings for robust project.
Generated by 'django-admin startproject' using Django 1.8.3.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ["25.22.12.21"]
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'projectname.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'projectname.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'toordb',
'USER': 'root',
'PASSWORD': 'root',
'HOST': 'localhost',
'PORT': '3306',
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_ROOT = '/opt/projectname/static/'
STATIC_URL = '/static/'
Hope this helps,
Basit