我很新设立的网络服务器,并已按照这些指南:Nginx的,WSGI,Django的 - 建立Web服务器
http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html https://gist.github.com/evildmp/3094281
由于我工作过的Mac ,我通过Homebrew安装了nginx:brew install nginx
。我一直在努力让我的nginx服务器为我的Django静态文件提供服务。
我照着我的项目路径跟着向导,并复制了他们mysite_nginx.conf
,并改变了它:
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name .example.com; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /Users/Simon/Documents/WebServer/uwsgi-tutorial/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /Users/Simon/Documents/WebServer/uwsgi-tutorial/mysite/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location/{
uwsgi_pass django;
include /Users/Simon/Documents/WebServer/uwsgi-tutorial/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
由于我在本地运行的服务器,我没有一个服务器名和已离开它按照上面的文件为.example.com
。我也尝试将其更改为127.0.0.1
(因为我在本地主机上运行它),但这也不起作用。
完成这个文件后,我符号链接nginx的按照指导文件 - 但该文件夹/etc/nginx/sites-enabled/
是不存在的,所以我只好先创建它自己,然后发出以下命令:
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
我其次是在他们的“部署静态文件”部分中的引导和停止,开始了我的nginx服务器在Mac上通过:
sudo nginx -s stop
sudo nginx
现在,他们都问我去这个地址看看我media.png是否送达:
http://example.com:8000/media/media.png
,因为我不认为Chrome浏览器可以example.com
在互联网上和example.com
在我的本地区分这不是服务。我在Chrome上获得This site can’t be reached. example.com took too long to respond error
。
所以我尝试:
http://127.0.0.1:8000/media/media.png
但我仍然得到一个This site can’t be reached 127.0.0.1 refused to connect
错误。
这是我为我的项目文件结构:
这里是包含uwsgi_params内什么:
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
我不知道如果引导是过时的。有人能告诉我我做错了什么吗?
下面是settings.py文件:
Generated by 'django-admin startproject' using Django 1.11.4.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""
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__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'XXX'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
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 = 'mysite.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 = 'mysite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.11/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/1.11/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.11/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
STATIC_URL = '/static/'
您不能使用example.com作为本地域。你仍然可以使用localhost本身,但如果你需要服务2个站点呢?在这种情况下,您应该创建本地第三级域(编辑您的主机文件)。虽然这些都是复杂的,但对我来说并没有什么意义,只需要在开发中使用dev服务器并在生产环境中使用nginx和uwsgi – abidibo
是的 - 我知道我可以使用dev服务器,但我仍然需要为我的网络服务器将投入生产的时间。 – Simon
你真的想在你的Mac上部署Django吗?通常我们只是在Mac上开发。你必须执行'./manage.py runserver',就是这样 - 不需要nginx和uwsgi。为什么这是不够的? –