nginx未找到Rest-framework返回404

时间:2018-10-03 04:58:54

标签: python django nginx django-rest-framework uwsgi

我一直在尝试解决此问题,但作为初学者,我似乎无法解决。 我已经按照Django-Rest-Framework上的教程创建了一个项目,当我运行Djnago服务器时,它可以完美地工作。但是,当我想使用uwsgi和nginx部署它时,遵循许多教程,nginx会显示“管理页面”,但是当我尝试访问Rest-Framework时,它返回404 Not Found。

我的项目的结构

Project--------|
  Project------|
  appInfo------|
   model-----|
   views-----|
   serializer|
   urls------|
  _uwsg,ini----|
  nginx.conf---|
  static-------|
   admin----|
rest_framework|
  media--------|
  manage.py----|
  uwsgi_params-|

这是我的uwsgi.ini

# _uwsgi.ini file
[uwsgi]
locates
chdir           = /Users/userName/Desktop/Project/API/Project/

module          = project.wsgi
# the virtualenv (full path)
home            = /Users/userName/Desktop/Project/venv

master          = true
env DJANGO_SETTINGS_MODULE=Project.settings
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe)
socket          = /tmp/project.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 664
# clear environment on exit
vacuum          = true
# create a pidfile
pidfile = /tmp/project.pid
# background the process & log
daemonize = uwsgi.log

这是我的nginx。

# the upstream component nginx needs to connect to
upstream mysite {
    # server 127.0.0.1:8001;
    server unix:///tmp/project.sock; # for a file socket, more effective
}
# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    server_name localhost;
    charset     utf-8;
    # max upload size
    client_max_body_size 75M;   # adjust to taste
    # Django media
    location /media  {
        alias /Users/userName/Desktop/Project/API/Project/media;  
    }
    location ^~/static {
        alias /Users/userName/Desktop/Project/API/Project/static; 
    }
    location /static/rest_framework/{
      alias Project/lib/python3.6/site-packages/rest_framework/static/rest_framework;
    }


    location / {
        uwsgi_pass  mysite;
        include /Users/userName/Desktop/Project/API/Project/uwsgi_params; 
    }
}

这是设置。py

import os

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




# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'mv-+i%z@5wn1xj3v!)w*ha7wu%$%bej&0wou@#o@3i&g$a@wk)'

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

ALLOWED_HOSTS = ['127.0.0.1', 'localhost']


# Application definition

INSTALLED_APPS = [
    'appInfo.apps.AppinfoConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.postgres',
    'rest_framework',
]

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



REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS':     'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 10,
    'DEFAULT_AUTHENTICATION_CLASSES': [],
    'DEFAULT_PERMISSION_CLASSES': [],
    'UNAUTHENTICATED_USER': None,
}
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'projectdb',
        'USER': 'userName',
     }
 }


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

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

wsgi.py

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Project.settings')

application = get_wsgi_application()

urls.py

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls import url
from django.conf.urls.static import static

urlpatterns = [
    path('appInfo/', include('appInfo.urls')),
    path('admin/', admin.site.urls),

]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Project.settings')

    application = get_wsgi_application()

请帮助。

0 个答案:

没有答案