2016-01-16 22 views
1

經過幾天的搜索,我發現nginx load banlancing似乎是解決方案,但我不確定。是否可以在nginx,uwsgi和django的會話後端是redis的多服務器上部署應用程序?

  1. 語境

我有一個Django應用程序mydomain與uwsgi和nginx的上的服務器A(外部IP 120.25.x.200)上運行。其nginx.confuwsgi.inihere。我使用Redis的2.8緩存會話:

# /apps/mydomain/proj/settings.py 
CACHES = { 
    "default": { 
     "BACKEND": "django_redis.cache.RedisCache", 
     "LOCATION": "redis://127.0.0.1:6379/1", 
     "OPTIONS": { 
      "CLIENT_CLASS": "django_redis.client.DefaultClient", 
     } 
    } 
} 
SESSION_ENGINE = "django.contrib.sessions.backends.cache" 
SESSION_CACHE_ALIAS = "default" 

我用Posrgresql數據庫:

# /apps/mydomain/proj/settings.py 
DATABASES = {'default': { 
    'ENGINE' : 'django.db.backends.postgresql_psycopg2', 
    'NAME' : 'muser', 
    'USER' : 'postgres', 
    'PASSWORD': '*****', 
    'HOST' : '127.0.0.1', 
    'PORT' : '5432', 
}} 

隨着uwsgi /apps/mydomain/uwsgi.iniservice nginx start應用效果很好服務器A上

  • 我的猜測
  • 如果我想添加兩個服務器B(120.25.x.201)和C(120.25.x.202)與A一起服務於應用mydomain,我可以遵循的步驟:

    # step 1:change the nginx.conf file on server A --- 
    # change the server block's `location /` in http block, others remain same 
    http { 
        upstream mydomain { 
         server 120.25.x.200:80; 
         server 120.25.x.201:80; 
         server 120.25.x.202:80; 
        } 
    
        server { 
         # ... 
         location/{ 
          uwsgi_pass http://mydomain; 
          include  /apps/mydomain/uwsgi_params; 
         } 
         # ... 
        } 
    } 
    
    # step 2:on server B and C, I make a copy of the `mydomain` app environment 
    # in the same path. 
    
    # step 3:on server B and C, I change the cache and database settings 
    # in /apps/mydomain/proj/settings.py like this(just replace 
    # the local ip 127.0.0.1 with A's ip): 
    
    CACHES = { 
        "default": { 
         # ... 
         "LOCATION": "redis://120.25.x.200:6379/1", 
         # ... 
        } 
    } 
    DATABASES = {'default': { 
        # ... 
        'HOST' : '120.25.x.200', 
        # ... 
    }} 
    
    # step 4:on server B and C,use `uwsgi /apps/mydomain/uwsgi.ini` to start the app. 
    # step 5:on server A, start the app with nginx and uwsgi as usual. 
    

    3.問題

    是我的猜想對嗎?服務A,B和C可以共享redis緩存的會話數據和Postgresql數據庫嗎?

    或者,我的目標是否有更好的解決方案?

    回答

    1

    是的,它是正確的解決方案,但是如果有必要的話。也許你的瓶頸不是Django的應用程序,但數據庫。可能更好的方法是將數據庫移動到另一臺服務器,因此它將擁有所有可用資源。

    +0

    是不是指A B C服務器共享一個數據庫服務器?另外,在哪裏放置redis服務器? – xiang

    +0

    是的,你必須有一個數據庫服務器或某種複製(這很難實現)。 Redis可以在任何地方。很難說如果不知道它的工作原理以及哪裏存在某些瓶頸,您的應用中哪些尺寸最大。 – GwynBleidD

    相關問題