nginx 文件名重复

File name repeating in nginx

我正在尝试设置 django 项目以使用 gunicorn 和 nginx 服务器。与 DEBUG=FALSE。 我在 nginx 日志中看到静态词重复了两次,从而改变了路径。

# settings.py
"""
Django settings for cognizance project.

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

For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
import os
from pathlib import Path
import sys


# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '<SECRET>'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ['127.0.0.1']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'pages'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'cognizance.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 = 'cognizance.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.2/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/3.2/howto/static-files/

STATIC_URL = '/static/'
if sys.argv[1] != 'runserver':
    STATIC_ROOT = os.path.join(BASE_DIR, "static/")

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]


# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


# NGINX CONF - under sites-enabled

server {
    listen 80;
    server_name 127.0.0.1;

location /static/ {
    root /home/tejas/Desktop/cognizance;
    }

location / {
    proxy_pass http://127.0.0.1:8000;
    }
}
# The error when I see the nginx logs

2021/04/18 17:27:05 [error] 7902#7902: *96 open() "/home/tejas/Desktop/cognizance/static/static/js/bs-init.js" failed (2: No such file or directory), client: 127.0.0.1, server: 127.0.0.1, request: "GET /static/js/bs-init.js HTTP/1.1", host: "127.0.0.1", referrer: "http://127.0.0.1/"
2021/04/18 17:27:05 [error] 7902#7902: *95 open() "/home/tejas/Desktop/cognizance/static/static/js/smoothproducts.min.js" failed (2: No such file or directory), client: 127.0.0.1, server: 127.0.0.1, request: "GET /static/js/smoothproducts.min.js HTTP/1.1", host: "127.0.0.1", referrer: "http://127.0.0.1/"
2021/04/18 17:27:05 [error] 7902#7902: *99 open() "/home/tejas/Desktop/cognizance/static/static/js/theme.js" failed (2: No such file or directory), client: 127.0.0.1, server: 127.0.0.1, request: "GET /static/js/theme.js HTTP/1.1", host: "127.0.0.1", referrer: "http://127.0.0.1/"
2021/04/18 17:27:06 [error] 7902#7902: *98 open() "/home/tejas/Desktop/cognizance/static/static/js/Simple-Slider.js" failed (2: No such file or directory), client: 127.0.0.1, server: 127.0.0.1, request: "GET /static/js/Simple-Slider.js HTTP/1.1", host: "127.0.0.1", referrer: "http://127.0.0.1/"


你可以看到它请求 /home/tejas/Desktop/cognizance/static/static/js/Simple-Slider.js,而不是 /home/tejas/Desktop/cognizance/static/js/Simple-Slider.js.

我该如何解决?

项目目录骨架

我按照本教程设置 nginx

请忽略这个,Whosebug需要我再写几行stufffff.....

尝试使用 alias 而不是 root 作为静态文件配置

location /static {
    alias /home/tejas/Desktop/cognizance/static/;
}

无论哪种方式,您都需要将 /static/ 添加到 root/alias