【问题标题】:Django - get 404 on live, works on local enviromentDjango - 在现场获得 404,在本地环境中工作
【发布时间】:2018-03-02 15:04:45
【问题描述】:

我在 django 1.6 中有一个应用程序,并且 /admin/ 实时地址存在问题。

当我在直播中转到此地址时,收到 404 错误

Nginx 日志说我没有文件或目录:

/path_to_project/live/admin/index.html "未找到(2:没有这样的文件或目录)

在本地,一切正常,项目在 admin 目录中没有 index.html 文件。

请帮忙。

我的网址:


from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.conf import settings

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('presentationtool.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

我的设置:


from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

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

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = ['*']


TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)

# Application definition
DEFAULT_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

)

THIRD_PARTY_APPS = (
    'south',
    'easy_thumbnails',
)

ADMIN_APPS = (
    'suit',
    'adminsortable',
)

LOCAL_APPS = (
    'myapp',
)

INSTALLED_APPS = ADMIN_APPS + DEFAULT_APPS + THIRD_PARTY_APPS + LOCAL_APPS

MIDDLEWARE_CLASSES = (
    '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 = 'www.urls'
WSGI_APPLICATION = 'www.wsgi.application'

TEMPLATE_CONTEXT_PROCESSORS = TCP + (
    'django.core.context_processors.request',
    'django.core.context_processors.static',
)

# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.6/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.6/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '..', 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, '..', 'media')


SOUTH_MIGRATION_MODULES = {
    'easy_thumbnails': 'easy_thumbnails.south_migrations',
}

编辑

我的 nginx 设置:

upstream myapp_upstream {
server my_url:port;
}

server {
        include     inc/80.inc;

        server_name

        my_url_here.com;

        root        /path_to_project_on_server/live/;


access_log      /var/log/nginx/app/myapp.live.access.log;
error_log       /var/log/nginx/app/myapp.live.error.log;
access_log      /var/log/nginx/app/myapp.live.upstream.log upstream;


        location / {
                include uwsgi_params;

uwsgi_pass myapp_upstream ;

        }

        location /admin {
                client_max_body_size 400m;
        }

        location /static/ {
                alias /path_to_project_on_server/live/static/;
        }

        location /media {
                alias /path_to_project_on_server/live/media;
        }

#        return      302 https://my_url_here.com$request_uri;

}

【问题讨论】:

  • 如果你只加载yourLiveURL/admin会发生什么?
  • @N.Ivanov 我收到了2017/09/21 11:59:39 [error] 1909#0: *25969334 open() "/path_to_project/live/admin" failed (2: No such file or directory), 和 404 错误。
  • 其他页面加载了吗?非管理员?
  • @N.Ivanov 主页看起来不错,但我对这个 /admin/ url 有疑问
  • 试试This,可能会有用处

标签: python django nginx django-admin http-status-code-404


【解决方案1】:

您需要设置 nginx 以使该路径上的任何请求都由您的 django 设置使用 uWSGI 处理。丢失的 index.html 消息似乎表明 nginx 本身(而不是 Django)正在尝试查找和提供一个不存在的索引文件。

似乎问题与 nginx 而不是您的 django 设置有关。你是如何配置 nginx 的?

http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

【讨论】:

    【解决方案2】:

    确保您的响应返回正确的 http 状态。 完整回复请见this issue

    【讨论】:

      猜你喜欢
      • 2013-06-27
      • 1970-01-01
      • 2016-03-22
      • 2021-12-08
      • 2021-10-21
      • 2016-07-16
      • 2018-03-23
      • 1970-01-01
      • 2014-03-31
      相关资源
      最近更新 更多