连接到上游时,Nginx Gunicorn Django1.5 – connect()失败(111:连接被拒绝)

前端之家收集整理的这篇文章主要介绍了连接到上游时,Nginx Gunicorn Django1.5 – connect()失败(111:连接被拒绝)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我在访问我的域时得到这个:我看到我的默认,显示了http500错误django模板.

我有gunicorn设置:

  1. command = '/usr/local/bin/gunicorn'
  2. logfile = "/home/ubuntu/virtualenv/myapp/error/gunicorn.log"
  3. loglevel = "info"
  4. pythonpath = '/home/ubuntu/virtualenv/myapp'
  5. bind = '127.0.0.1:8001'
  6. workers = 3

我有Nginx配置:

  1. server {
  2. listen 80;
  3. server_name *.myapp.com;
  4. access_log /home/ubuntu/virtualenv/myapp/error/access.log;
  5. error_log /home/ubuntu/virtualenv/myapp/error/error.log warn;
  6. connection_pool_size 2048;
  7. fastcgi_buffer_size 4K;
  8. fastcgi_buffers 64 4k;
  9. root /home/ubuntu/virtualenv/myapp/homelaunch/;
  10. location /static/ {
  11. alias /home/ubuntu/virtualenv/myapp/homelaunch/static/;
  12. #alias /static/;
  13. #root /home/ubuntu/virtualenv/myapp/homelaunch/;
  14. }
  15. location / {
  16. proxy_pass http://127.0.0.1:8001;
  17. proxy_set_header Host $host;
  18. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  19. #proxy_set_header X-Forwarded-Host $server_name;
  20. #proxy_set_header X-Real-IP $remote_addr;
  21. add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
  22. }
  23. }

settings.py

  1. #### LOCAL ENV
  2. #DEBUG = True
  3. #TEMPLATE_DEBUG = True
  4. #### PRODUCTION ENV
  5. DEBUG = False
  6. TEMPLATE_DEBUG = False
  7. ADMINS = (
  8. ('xxx','xxx@gmail.com'),#('xxx','joe@myapp.com'),)
  9. MANAGERS = ADMINS
  10. DATABASES = {
  11. 'default': {
  12. 'ENGINE': 'django.db.backends.MysqL',# Add 'postgresql_psycopg2','MysqL','sqlite3' or 'oracle'.
  13. 'NAME': 'launch',# Or path to database file if using sqlite3.
  14. # The following settings are not used with sqlite3:
  15. 'USER': 'root','PASSWORD': 'xxx','HOST': '',# Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
  16. 'PORT': '',# Set to empty string for default.
  17. }
  18. }
  19. # Hosts/domain names that are valid for this site; required if DEBUG is False
  20. # See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
  21. ALLOWED_HOSTS = ['myapp.com','zzz.zz.zzz.zzz','ec2-zz-zz-zz-zz.compute-1.amazonaws.com']
  22. #### MODIFY THIS ^^^^^^^^^^^^^^ WHEN USING IN PRODUCTION - DJANGO 1.5.
  23. ######## if this is left blank in production,it will not load.
  24. # Local time zone for this installation. Choices can be found here:
  25. # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
  26. # although not all choices may be available on all operating systems.
  27. # In a Windows environment this must be set to your system time zone.
  28. TIME_ZONE = 'America/Chicago'
  29. # Language code for this installation. All choices can be found here:
  30. # http://www.i18nguy.com/unicode/language-identifiers.html
  31. LANGUAGE_CODE = 'en-us'
  32. SITE_ID = 1
  33. # If you set this to False,Django will make some optimizations so as not
  34. # to load the internationalization machinery.
  35. USE_I18N = True
  36. # If you set this to False,Django will not format dates,numbers and
  37. # calendars according to the current locale.
  38. USE_L10N = True
  39. # If you set this to False,Django will not use timezone-aware datetimes.
  40. USE_TZ = True
  41. # Absolute filesystem path to the directory that will hold user-uploaded files.
  42. # Example: "/var/www/example.com/media/"
  43. MEDIA_ROOT = ''
  44. # URL that handles the media served from MEDIA_ROOT. Make sure to use a
  45. # trailing slash.
  46. # Examples: "http://example.com/media/","http://media.example.com/"
  47. MEDIA_URL = ''
  48. # Absolute path to the directory static files should be collected to.
  49. # Don't put anything in this directory yourself; store your static files
  50. # in apps' "static/" subdirectories and in STATICFILES_DIRS.
  51. # Example: "/var/www/example.com/static/"
  52. STATIC_ROOT = '/home/ubuntu/virtualenv/myapp/homelaunch/static/'
  53. #STATIC_ROOT = '/static/'
  54. # URL prefix for static files.
  55. # Example: "http://example.com/static/","http://static.example.com/"
  56. #STATIC_URL = '/home/ubuntu/virtualenv/myapp/homelaunch/static/'
  57. STATIC_URL = '/static/'
  58. # Additional locations of static files
  59. STATICFILES_DIRS = (
  60. # Put strings here,like "/home/html/static" or "C:/www/django/static".
  61. # Always use forward slashes,even on Windows.
  62. # Don't forget to use absolute paths,not relative paths.
  63. #'/home/ubuntu/virtualenv/myapp/homelaunch/static/',)
  64. # List of finder classes that know how to find static files in
  65. # varIoUs locations.
  66. STATICFILES_FINDERS = (
  67. 'django.contrib.staticfiles.finders.FileSystemFinder','django.contrib.staticfiles.finders.AppDirectoriesFinder',# 'django.contrib.staticfiles.finders.DefaultStorageFinder',)
  68. # Make this unique,and don't share it with anybody.
  69. SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
  70. # List of callables that know how to import templates from varIoUs sources.
  71. TEMPLATE_LOADERS = (
  72. 'django.template.loaders.filesystem.Loader','django.template.loaders.app_directories.Loader','django.template.loaders.eggs.Loader',)
  73. MIDDLEWARE_CLASSES = (
  74. 'django.middleware.common.CommonMiddleware','django.contrib.sessions.middleware.SessionMiddleware','django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.middleware.clickjacking.XFrameOptionsMiddleware',)
  75. ROOT_URLconf = 'myapp.urls'
  76. # Python dotted path to the WSGI application used by Django's runserver.
  77. WSGI_APPLICATION = 'myapp.wsgi.application'
  78. TEMPLATE_DIRS = (
  79. # Put strings here,like "/home/html/django_templates" or "C:/www/django/templates".
  80. # Always use forward slashes,not relative paths.
  81. '/home/ubuntu/virtualenv/myapp/homelaunch/templates',)
  82. INSTALLED_APPS = (
  83. 'django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.sites','django.contrib.messages','django.contrib.staticfiles','south','homelaunch','gunicorn',# Uncomment the next line to enable the admin:
  84. # 'django.contrib.admin',# Uncomment the next line to enable admin documentation:
  85. # 'django.contrib.admindocs',)
  86. # A sample logging configuration. The only tangible logging
  87. # performed by this configuration is to send an email to
  88. # the site admins on every HTTP 500 error when DEBUG=False.
  89. # See http://docs.djangoproject.com/en/dev/topics/logging for
  90. # more details on how to customize your logging configuration.
  91. LOGGING = {
  92. 'version': 1,'disable_existing_loggers': False,'filters': {
  93. 'require_debug_false': {
  94. '()': 'django.utils.log.requiredebugFalse'
  95. }
  96. },'handlers': {
  97. 'mail_admins': {
  98. 'level': 'ERROR','filters': ['require_debug_false'],'class': 'django.utils.log.AdminEmailHandler'
  99. }
  100. },'loggers': {
  101. 'django.request': {
  102. 'handlers': ['mail_admins'],'level': 'ERROR','propagate': True,},}
  103. }

在这个Nginx的error_log中我看到:

  1. 2013/10/25 00:31:49 [error] 1840#0: *1 connect() Failed (111: Connection refused) while connecting to upstream,client: xx.xxx.xx.xxx,server: myapp.com,request: "GET / HTTP/1.1",upstream: "http://127.0.0.1:8001/",host: "myapp.com"

在这个Nginx配置的access_log中我看到:

  1. xx.xxx.xx.xxx - - [25/Oct/2013:00:31:49 +0000] "GET / HTTP/1.1" 502 173 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0"

我已经尝试阅读问题并看到添加

  1. fastcgi_buffer_size 4K;
  2. fastcgi_buffers 64 4k;

Nginx配置可能会有所帮助,但事实并非如此.

我对如何从这里调试这个问题感到困惑?有人可以帮忙吗?

谢谢.

最佳答案
更改您的settings.py文件,以便ALLOWED_HOSTS = [‘domain’](或[‘*’]使其工作)

猜你在找的Nginx相关文章