2016-09-12 1 views
0

nginx에서 실행되는 장고 응용 프로그램이 있습니다. 이 응용 프로그램은 (필자가 아는 한) 프록시되어야하는 소켓을 사용합니다. 그래서 nginx 및 기타 물건을 구성하는 데 문제가 있습니다. 동일한 애플리케이션이 Apache/2.4.7에서 제대로 작동하므로 프로그래밍 실수가 아니라고 가정합니다.gninx에서 Django로 소켓 설정하기

소켓은 장고 - 채널을 기반으로하고 백엔드는 Channels getting started의 코드와 매우 유사합니다.

서버 구성에 대해서는 this 설명서를 사용했습니다.

처음에는 하나의 문제점 만있었습니다. 소켓 생성시 101 대신 200 요청 응답이 있습니다. 나는 현재의 상황에 와서 수집하는 많은 조작 (구성 및 최신 버전 설치) 및 정보 후 :

나는 소켓에 대해 개별적으로 uwsgi를 시작합니다 내가 할 var socket = new WebSocket("ws://appname.ch/ws/64"); 소켓의 작성에이 단계에

uwsgi --virtualenv /home/appname/env/ --http-socket /var/run/redis/redis.sock --http-websock --wsgi-file /home/appname/appname/appname/wsgi.py 

WebSocket connection to 'ws://appname.ch/ws/64' failed: Error during WebSocket handshake: Unexpected response code: 502 

있는지

2016/09/12 12:00:26 [crit] 30070#0: *2141 connect() to unix:/var/run/redis/redis.sock failed (13: Permission denied) while connecting to upstream, client: 140.70.82.220, server: appname.ch,, request: "GET /ws/64 HTTP/1.1", upstream: "http://unix:/var/run/redis/redis.sock:/ws/64", host: "appname.ch" 
nginx 오류 로그에.

chmod 777 /var/run/redis/redis.sock 후 나는 responce

WebSocket connection to 'ws://appname.ch/ws/64' failed: Error during WebSocket handshake: Unexpected response code: 404 

및 uwsgi에서

[pid: 6572|app: 0|req: 1/1] 0.0.0.0() {46 vars in 916 bytes} [Mon Sep 12 12:01:29 2016] GET /ws/64 => generated 3357 bytes in 24 msecs (HTTP/1.1 404) 2 headers in 80 bytes (1 switches on core 0) 

이 파일 nginx.conf 얻을

user www-data; 
worker_processes 4; 
pid /run/nginx.pid; 

events { 
worker_connections 768; 
# multi_accept on; 
} 

http { 

## 
# Basic Settings 
## 

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

## 
# Virtual Host Configs 
## 

include /etc/nginx/conf.d/*.conf; 
include /etc/nginx/sites-enabled/*; 
} 

redis.conf

daemonize yes 

pidfile /var/run/redis/redis-server.pid 

port 6379 

unixsocket /var/run/redis/redis.sock 
unixsocketperm 777 

timeout 0 

loglevel notice 

logfile /var/log/redis/redis-server.log 

databases 16 

save 900 1 
save 300 10 
save 60 10000 

rdbcompression yes 

dbfilename dump.rdb 

dir /var/lib/redis 

auto-aof-rewrite-percentage 100 
auto-aof-rewrite-min-size 64mb 
,691 363,210

을/etc/nginx를/사이트 사용/APPNAME

server { 
     listen  80; 
     server_name appname.ch, 177.62.206.170; 

     #charset koi8-r; 
    client_max_body_size 8M; 

     access_log /var/log/nginx/access.log; 
     error_log /var/log/nginx/error.log; 

     location/{ 
      include  uwsgi_params; 
      uwsgi_pass  unix:///home/appname/appname/app.sock; 
      #add_header  Access-Control-Allow-Origin *; 
    } 
    location /ws/ { 
     #proxy_redirect off; 
     proxy_pass http://unix:/var/run/redis/redis.sock; 
     #proxy_http_version 1.1; 
     #proxy_set_header Upgrade $http_upgrade; 
      #proxy_set_header Connection "upgrade"; 

     proxy_http_version 1.1; 
     proxy_set_header Upgrade $http_upgrade; 
     proxy_set_header Connection "upgrade"; 
     proxy_set_header Host $host; 

    } 

     location /static { 
      alias /home/appname/appname/static_files; 
     } 

     location /media { 
      alias /home/appname/appname/media; 
     } 
} 

uwsgi.ini

[uwsgi] 

chdir=/home/appname/appname 
env=DJANGO_SETTINGS_MODULE=appname.settings 
wsgi-file=appname/wsgi.py 
master=True 
pidfile=/home/appname/appname/appname-master.pid 
vacuum=True 
max-requests=5000 
daemonize=/home/appname/appname/uwsgi.log 
socket=/home/appname/appname/app.sock 
virtualenv=/home/appname/env 
uid=appname 
gid=appname 

장고 응용 프로그램 settings.py

""" 
Django settings for appname project. 

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

For the full list of settings and their values, see 
https://docs.djangoproject.com/en/1.7/ref/settings/ 
""" 

# 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: keep the secret key used in production secret! 
SECRET_KEY = '' 

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

TEMPLATE_DEBUG = DEBUG 

ALLOWED_HOSTS = ['.appname.ch', '177.62.206.170', '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', 
    'customers', 
    'projects', 
    'moodboard', 
    'channels', 
    'debug_toolbar', 
    'rest_framework', 
    'appname', 
) 

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', 
) 

ROOT_URLCONF = 'appname.urls' 

WSGI_APPLICATION = 'appname.wsgi.application' 


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

STATIC_URL = '/static/' 

MEDIA_URL = '/media/' 

STATIC_ROOT = os.path.join(BASE_DIR, 'static_root') 

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

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static_files'), 
) 

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

AUTH_PROFILE_MODULE = 'customers.Customer' 

REST_FRAMEWORK = { 
    # Use Django's standard `django.contrib.auth` permissions, 
    # or allow read-only access for unauthenticated users. 
    'DEFAULT_PERMISSION_CLASSES': [ 
     'rest_framework.permissions.IsAuthenticated', 
    ] 
} 

LOGIN_REDIRECT_URL = '/accounts/home' 

CHANNEL_LAYERS = { 
    "default": { 
     "BACKEND": "asgi_redis.RedisChannelLayer", 
     "CONFIG": { 
      "hosts": [("localhost", 6379)], 
     }, 
     "ROUTING": "appname.routing.channel_routing", 
    }, 
} 

앱이

from django.conf.urls import patterns, include, url 
from django.contrib import admin 
from django.contrib.auth import views as auth_views 
from projects.views import ProjectViewSet 
from customers.views import UserHomeView, RegistrationView, CustomerViewSet, UserViewSet 
from moodboard.views import MoodBoardViewSet, BoardItemViewSet, BoardTextViewSet, ShareMoodBoardItem, LiveViewSet 
from rest_framework import routers 
from django.conf import settings 
from django.conf.urls.static import static 


router = routers.DefaultRouter() 
router.register(r'projects', ProjectViewSet) 
router.register(r'moodboards', MoodBoardViewSet) 
router.register(r'items', BoardItemViewSet) 
router.register(r'texts', BoardTextViewSet) 
router.register(r'share', ShareMoodBoardItem) 
router.register(r'customers', CustomerViewSet) 
router.register(r'users', UserViewSet) 
router.register(r'live', LiveViewSet) 


urlpatterns = patterns('', 
         url(r'^$', 'appname.views.home', name='landing_page'), 
         url(r'^api/', include(router.urls)), 
         url(r'^accounts/login/$', auth_views.login, name='login'), 
         url(r'^accounts/logout/$', auth_views.logout, name='logout'), 
         url(r'^accounts/home/$', UserHomeView.as_view(), name='home'), 
         url(r'^accounts/register/$', RegistrationView.as_view(), name='registration'), 
         url(r'^admin/', include(admin.site.urls)), 
         url(r'^customers/', include('customers.urls')), 
         url(r'^projects/', include('projects.urls')), 
         url(r'^moodboard/', include('moodboard.urls')), 
         url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')) 
         ) 

if settings.DEBUG: 
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)root 
를하는 URL

nginx version: 1.6.2 Redis server version: 2.4.14 uwsgi version: 2.1 Django version: 1.8.0 'final' Python version: 2.7.3

404 오류가 나타나서는 안되지만 문제가 무엇인지, 그리고 일반적으로 제대로 작동하는지는 모르겠다.

답변

0

우선, 수동으로 양말을 만들려고 시도하지 마십시오. 경로를 설정하기 만하면 자동으로 생성됩니다.

이 예제의 nginx과 uwsgi은 conf의 :

server { 
root /your/djang/app/main/folder/; 

# the port your site will be served on 
listen  80; 
server_name your-domain.com *.your-domain.com # if you have subdomains 
charset  utf-8; 

access_log /path/to/logs/if/you/have/access_log.log 
error_log /path/to/logs/if/you/have/error_log.log 

# max upload size 
client_max_body_size 1G; 

location /media/ { 
    alias /path/to/django/media/if/exist/; 
} 

location /static/ { 
    alias /path/to/django/static/if/exist/; 
} 
# Note three slash 
location/{ 
    uwsgi_pass unix:///home/path/to/sock/file/your-sock.sock 
} 

}

이 CNA는 uwsgi 설정 파일 수

# suprasl_uwsgi.ini file 
[uwsgi] 

uid = www-data 
gid = www-data 
chmod-socket = 755 
chown-socket = www-data 
# Django-related settings 
# the base directory (full path) 
chdir   = /your/djang/app/main/folder/ 
# Django's wsgi file 
wsgi-file  = /your/djang/app/main/folder/main-folder/wsgi.py; 
# the virtualenv (full path) 
home   = /your/env/folder/; 
# process-related settings 
# master 
master   = true 
# maximum number of worker processes 
processes  = 2 
# the socket (use the full path to be safe 
socket   = /home/path/to/sock/file/your-sock.soc 

logto   = /path/to/logs/if/you/have/uwsgi_logs.log 

그냥이 명령을 실행해야 할 모든 :

uwsgi --ini your_uwsgi_file.ini # --ini 옵션을 사용하여 파일을 지정합니다.