2

I have a Django website running with Nginx and Gunicorn. Everytime I call a url on the server, example website/url, it redirects to localhost/url. I have given the nginx settings in both nginx.conf and sites-available/site-name

nginx.conf:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

##
# Basic Settings
##
    client_max_body_size 5M;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;

# server_names_hash_bucket_size 64;
server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# SSL Settings
##

ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;

##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Gzip Settings
##

gzip on;
gzip_disable "msie6";

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

}

nginx/sites-available/site name

server {
    listen 80;
    server_name url;
    server_name_in_redirect off;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location /static/ {
        alias /opt/itc/iitb-tech/static/;
    }

    location / {
            proxy_pass http://unix:/opt/itc/iitb-tech/itc_iitb/itc_iitb.sock;
            proxy_set_header X-Forwarded-Host url.org;
            proxy_set_header X-Real-IP $remote_addr;
            add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
    }
}

Django settings file

"""
Django settings for itc_iitb project.

Generated by 'django-admin startproject' using Django 1.8.7.

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 = ['server ip','127.0.0.1','localhost']

ALLOWED_PORTS = ['*']
# Application definition

INSTALLED_APPS = (
'aero',
'erc',
'biotech',
'mnp',
'krittika',
'main',
'srg',
'ITSP2017',
'ec',
'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 = 'itc_iitb.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 = 'itc_iitb.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 
'postgres$
        'NAME': 'dbname',                      # Or path to database 
 file$
        # The following settings are not used with sqlite3:
        'USER': 'user',
        'PASSWORD': 'pwd',
        'HOST': 'localhost',                      # Empty for 
localhost thr$
        'PORT': '',                      # Set to empty string for 
default.
    }
}

# 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_URL = '/static/'
STATIC_ROOT = "/opt/itc/iitb-tech/static/"
MEDIA_ROOT = BASE_DIR + '/ITSP2017/static/media/'
MEDIA_URL = '/static/media/'

APPEND_SLASH = True
Shounak
  • 21
  • 3
  • Can you clarify the question? Write a bit more detail, shorten the header, give us something organized to work with. – Hassan Baig Oct 07 '17 at 23:35
  • Is this enough? Because I have given all the details I think are relevant – Shounak Oct 08 '17 at 14:56
  • The first part of your proxy_pass with both http and unix looks fishy to me. Are you trying to proxy to http or a unix socket? Are your requests making it to the destination service / server? Once Nginx is working I would check the Django server to see if it's sending the localhost redirect. – Tim Oct 08 '17 at 18:36
  • Yes I believe the requests are getting to the server as I can see them in the Nginx access log. I have uploaded the Django settings file too so just let me know if you find any problem in that – Shounak Oct 09 '17 at 11:18

1 Answers1

0

I had the same problem with Django 2.0 and Nginx 1.6. If you follow the tutorial for Gunicorn from here you'll see that they recommend using the 'upstream' directive instead of http://unix:/...

I'm not quite sure what was causing this redirect, but moving away from http://unix did the trick for me.

Also keep in mind that Firefox caches this behavior (I opened a new private window for testing).