Nginx 将每个 URL 重定向到本地主机

Nginx 将每个 URL 重定向到本地主机

我有一个使用 Nginx 和 Gunicorn 运行的 Django 网站。每次我在服务器上调用 URL(例如 website/url),它都会重定向到 localhost/url。我在 nginx.conf 和 sites-available/site-name 中都提供了 nginx 设置

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/站点名称

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 设置文件

"""
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

答案1

我在使用 Django 2.0 和 Nginx 1.6 时也遇到了同样的问题。如果你按照 Gunicorn 教程操作,这里 你会发现他们建议使用“上游”指令,而不是http://unix:/...

我不太清楚是什么导致了这个重定向,但是远离http://unix对我有用。

还要记住,Firefox 会缓存此行为(我打开了一个新的私人窗口进行测试)。

相关内容