2015-07-20 55 views
1

我正在使用nginx在Google計算引擎上執行我的django項目。Django空白頁面需要2秒才能加載

我的看法文件,

from django.shortcuts import render_to_response 

def home(request): 
    return render_to_response('html/index.html',RequestContext(request))  

和我的模板index.html文件

<html> 
<head> 
<title> Home </title> 
</head> 
<body> 
Wel come to my new project 
</body> 
</html> 

在呼籲獨此頁面需要2.75秒到我的服務器上加載完全。

FFTB(等待時間)需要1.8秒 此延遲是否存在任何配置問題?

我的設置文件

""" 
Django settings for audiotube project. 

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

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

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


#Base root is for outside of the project structure 
BASE_DIR = os.path.dirname(os.path.dirname(__file__)) 

rsa_private_key = BASE_DIR + "/lib/rsa/id_rsa" 

#Package root is for inside the project 
PACKAGE_ROOT = os.path.abspath(os.path.dirname(__file__)) 

# Quick-start development settings - unsuitable for production 
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/ 

# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = '46=+9k)k25rb(b9&&wy9y_xtn3rx2stl#%+0-5o-$-un9o&4hn' 

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


TEMPLATE_DEBUG = True 
THUMBNAIL_DEBUG = True 


ALLOWED_HOSTS = ['localhost','example.com', '*'] 


# Application definition 

INSTALLED_APPS = (
    #This is for the site name and inbuild framework 
    'django.contrib.sites', 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'app0', 
    'app1', 
    'ckeditor', 
    'social.apps.django_app.default', 
    'sorl.thumbnail', 
    'compressor', 
    'template_timings_panel', 

) 
COMPRESS_ENABLED = True 

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder', 
    'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
    # other finders.. 
    'compressor.finders.CompressorFinder', 
) 

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 = 'mysite.urls' 

WSGI_APPLICATION = 'mysite.wsgi.application' 


# Database 
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases 

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.mysql', 
     'NAME': 'mydb', 
     'USER': 'root', 
     'PASSWORD': 'password', 
     'HOST': 'hostingaddre', 
     'PORT': '', 
    } 
} 

# Internationalization 
# https://docs.djangoproject.com/en/1.6/topics/i18n/ 

LANGUAGE_CODE = 'en-us' 

TIME_ZONE = None 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 

SITE_ID = 1 

# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/1.6/howto/static-files/ 

STATIC_ROOT = '/home/mysite/staticfiles/' 


STATIC_URL = '/static/' 

# Additional locations of static files 
STATICFILES_DIRS = [ 
    os.path.join(PACKAGE_ROOT, "static"), 
] 

MEDIA_ROOT = os.path.join(PACKAGE_ROOT, "site_media", "media") 

MEDIA_URL = '/media/' 

# List of callables that know how to import templates from various sources. 



TEMPLATE_DIRS = [ 
    os.path.join("templates"), 
] 


#Template context processors 



TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.request", 
    "django.contrib.auth.context_processors.auth", 
    "django.core.context_processors.debug", 
    "django.core.context_processors.i18n", 
    "django.core.context_processors.media", 
    "django.core.context_processors.static", 
    "django.core.context_processors.tz", 
    "django.contrib.messages.context_processors.messages", 
    'social.apps.django_app.context_processors.backends', 
    'social.apps.django_app.context_processors.login_redirect', 
) 


# This two setting needed for downloading CKEDITOR 

CKEDITOR_JQUERY_URL = '//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js' 

CKEDITOR_UPLOAD_PATH = "uploads/" 

CKEDITOR_IMAGE_BACKEND = "pillow" 

CKEDITOR_CONFIGS = { 
    'default': { 
     'toolbar': 'UltraFull', 
     'height': 300, 
     'toolbar_UltraFull': [ 
      ['Font', 'FontSize', 'Format'], 
      ['Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat'], 
      [ 
       'NumberedList', 'BulletedList', '-', 
       'Outdent', 'Indent', '-', 
       'Blockquote', '-', 
       'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock' 
      ], 
      ['Link', 'Unlink', 'Anchor'], 
      ['Image', 'Flash', 'Table', 'HorizontalRule', 'PageBreak', 'Smiley', 'SpecialChar'], 
      ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo'], 
      ['TextColor', 'BGColor'], 
      ['Maximize', 'Source'], 
     ],   
     'toolbarCanCollapse': False, 
    }, 
    'awesome_ckeditor': { 
     'toolbar': 'UltraFull', 
     'height': 300, 
     'toolbar_UltraFull': [ 
      ['Font', 'FontSize', 'Format'], 
      ['Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat'], 
      [ 
       'NumberedList', 'BulletedList', '-', 
       'Outdent', 'Indent', '-', 
       'Blockquote', '-', 
       'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock' 
      ], 
      ['Link', 'Unlink', 'Anchor'], 
      ['Image', 'Flash', 'Table', 'HorizontalRule', 'PageBreak', 'Smiley', 'SpecialChar'], 
      ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo'], 
      ['TextColor', 'BGColor'], 
      ['Maximize', 'Source'], 
     ],   
     'toolbarCanCollapse': False, 
    }, 
} 



AUTHENTICATION_BACKENDS = (
    'users.backends.EmailAuthBackend', 
    'social.backends.facebook.FacebookOAuth2', 
    'social.backends.google.GoogleOAuth2', 
    'social.backends.twitter.TwitterOAuth', 
    'django.contrib.auth.backends.ModelBackend', 
    ) 
SOCIAL_AUTH_FACEBOOK_KEY ="XXXXXXXXXXXXX" 
SOCIAL_AUTH_FACEBOOK_SECRET ="XXXXXXXXXXXXX" 
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email'] 
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY ="XXXXXXXXXXXXX" 
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = "XXXXXXXXXXXXX" 
SOCIAL_AUTH_FORCE_EMAIL_VALIDATION = True 



EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' 

SITE_URL="http://example.com:8000/" 


''' 
#Use this for exception handling 
SOCIAL_AUTH_PIPELINE = (
    'social.pipeline.social_auth.associate_by_email', 

) 
''' 

SOCIAL_AUTH_PIPELINE = (
    'social.pipeline.social_auth.social_details', 
    'social.pipeline.social_auth.social_uid', 
    'social.pipeline.social_auth.auth_allowed', 
    'social.pipeline.social_auth.social_user', 
    'social.pipeline.user.get_username', 
    'users.pipeline.require_email', 
    #'social.pipeline.mail.mail_validation', 
    'social.pipeline.user.create_user', 
    'users.pipeline.save_profile', 
    'social.pipeline.social_auth.associate_user', 
    'social.pipeline.social_auth.load_extra_data', 
    'social.pipeline.user.user_details', 
    #'social.pipeline.debug.debug' 
) 

# login redirect urls 
LOGIN_URL = "/signin" 
LOGIN_REDIRECT_URL = "/dashboard" 
DOMAIN = "http://example.com:8000" 



if DEBUG: 
    INTERNAL_IPS = ('127.0.0.1',) 
    MIDDLEWARE_CLASSES += (
     'debug_toolbar.middleware.DebugToolbarMiddleware', 
    ) 

    INSTALLED_APPS += (
     'debug_toolbar', 
    ) 

    DEBUG_TOOLBAR_PANELS = (
    'template_timings_panel.panels.TemplateTimings.TemplateTimings', 
     'debug_toolbar.panels.version.VersionDebugPanel', 
     'debug_toolbar.panels.timer.TimerDebugPanel', 
    'debug_toolbar.panels.sql.SQLPanel', 
     #'debug_toolbar.panels.settings_vars.SettingsVarsDebugPanel', 
     'debug_toolbar.panels.headers.HeaderDebugPanel', 
     #'debug_toolbar.panels.profiling.ProfilingDebugPanel', 
     'debug_toolbar.panels.request_vars.RequestVarsDebugPanel', 
     'debug_toolbar.panels.sql.SQLDebugPanel', 
     'debug_toolbar.panels.template.TemplateDebugPanel', 
     'debug_toolbar.panels.cache.CacheDebugPanel', 
     'debug_toolbar.panels.signals.SignalDebugPanel', 
     #'debug_toolbar.panels.logger.LoggingPanel', 
    ) 

    DEBUG_TOOLBAR_CONFIG = { 
     'INTERCEPT_REDIRECTS': False, 
    } 


PREPEND_WWW = True 

CACHES = { 
    'default': { 
     'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache', 
     'LOCATION': '/var/tmp/django_cache', 
    } 
} 


TEMPLATE_LOADERS = (
    ('django.template.loaders.cached.Loader', (
     'django.template.loaders.filesystem.Loader', 
     'django.template.loaders.app_directories.Loader', 
    )), 
) 

在example.com這裏面的/ etc/nginx的/啓用站點和站點提供

server { 

    listen 80; 
    server_name myservername.com; 
    client_max_body_size 4G; 


    error_page 502 =200 @maintenance; 

    location @maintenance { 
     root /path/to/static/offline/files; 
     try_files $uri /index.html =503; 
    } 
    location /static/ { 

     alias /home/sim/5vs/staticfiles/; 

    } 

    location /media/ { 
     alias /home/sim/5vs/myproject/myproject/site_media/media/; 
     expires 30d; 
    } 
    location/{ 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header Host $http_host; 
     proxy_redirect off; 


    } 
}         

在nginx.conf文件中有

user root; 
worker_processes 4; 
pid /run/nginx.pid; 

events { 
    worker_connections 768; 
    # multi_accept on; 
} 

http { 
    sendfile on; 
    tcp_nopush on; 
    tcp_nodelay on; 
    keepalive_timeout 65; 
    types_hash_max_size 2048; 

    include /etc/nginx/mime.types; 
    default_type application/octet-stream; 

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



    gzip on; 
    gzip_disable "msie6"; 


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

請問您爲什麼需要很長時間,我的配置有什麼問題。

使用探查

總時間:1.89419小號 文件:/home/staging/live/users/views.py 功能:comingsoon_view在第93行

線#點擊時間每命中%時間行內容

93           def comingsoon_view(request): 
94   1   2  2.0  0.0  temp = {} 
95   1  1894189 1894189.0 100.0 return render(request, 'pages/comingsoon.html',temp) 
+0

你用什麼從上游端口8000服務? –

+0

@DanielRoseman我沒有關於服務器端集成的想法。我已經使用了一些手冊來配置它。即使這個項目正在運行,檢查更新的文件。請告訴你建議我會檢查並通知你。 –

+0

您已將nginx設置爲指向端口8000上的上游服務器。在那裏運行的是什麼?你必須有東西,否則你的應用程序根本就沒有響應。你在使用gunicorn?或者你(使用Django runserver)(如Yevgeniy提示)?如果是後者,你**絕對不能這樣做**。 –

回答

0

用戶使用調試服務器與DEBUG = True設置運行django項目。它啓用了DEBUG工具欄 - 這非常慢。這是該設置的正常時間。如果你會禁用這個 - 我認爲一切都會好的。

如果你仍然有麻煩 - 我建議你檢查django調試服務器的響應時間(python manage.py runserver)。如果它更快,那麼問題是nginx配置,或者如果相同 - 你應該檢查與lineprofiler時間。

如果是生產配置 - 請勿在生產中使用調試服務器。您應該使用gunicorn或類似的東西來運行django項目。

+0

即使調試是禁用它也需要相同的時間才能執行,而django調試服務器和nginx也差不多,差別不大。我會檢查線輪廓儀和gunicorn,讓你知道。 –

+0

我已經使用了line prifiler,我在我的問題中附加了結果。請看看,讓我知道我還有什麼要做。 –

+0

下一步應該是: 1.禁用社交認證中間件 2.切換到memcached或其他可用的內存緩存後端 3.檢查確實禁用了DEBUG設置(./manage.py shell;來自django.conf導入設置;打印settings.DEBUG) 4.在本地運行項目 - 可能是谷歌計算引擎的原因是什麼? ) 通常,django可以很好地處理這樣簡單的項目。 –

相關問題