2017-05-31 79 views
0

我想允許我的nginx服務器爲多個子域服務器。 每個站點都有一個燒瓶+ uwsgi,監聽自己的端口。具有一個IP的多個網站:使用代理服務器和代理緩存(flask,uwsgi,nginx)的設置

所有站點都有許多具有相同名稱的端點,並將響應緩存在不同的區域中:我想從代理服務器提供正確的緩存(或正確的站點)。

我讀https://askubuntu.com/questions/766352/multiple-websites-on-nginx-one-ip:在我的配置中,我一直讓domain2重定向到domain1。 我無法找到正確的配置來偵聽uwsgi並讓代理服務器爲正確的站點提供服務。

如何正確設置proxy_server上的端口和proxy_cache以允許nginx從單個服務器提供兩個燒瓶站點?

下面是我設定的當前設置:

配置DOMAIN_1
server {   
    server_name www.domain1.com; 
    return 301 $scheme://domain1.com$request_uri; 
} 

server { 
    listen 8000 default_server; 
    server_name domain1.com; 

    root /var/www/example_site_1; 

    # common locations for all sites 
    location/{ 
     include uwsgi_params; 
     uwsgi_pass unix:/var/www/example_site_1/domain1.sock; 
    } 

    # API 
    location /api {      
     include uwsgi_params; 
     uwsgi_param UWSGI_SCRIPT wsgi; 
     uwsgi_pass unix:/var/www/example_site_1/domain1.sock; 
    } 
} 



# Set cache directory for site 

proxy_cache_path /tmp/nginx/domain1 levels=1:2 keys_zone=my_zone_domain_1:10m max_size=50m inactive=60m; 
proxy_cache_key "$scheme$request_method$host$request_uri"; 


# Virtualhost/server configuration 
server { 
    listen 80 default_server; 
    server_name domain1; 

    root /var/www/domain1; 

    ## how to serve proxy_cache if locations of domain_1 and domain_2 are the same ? 

    location/{   
     proxy_cache my_zone_domain_1;  
     add_header X-Proxy-Cache $upstream_cache_status; 
     include proxy_params; 
     proxy_pass http://domain1.com:8000; 
    } 

    location /api { 
     add_header X-Proxy-Cache $upstream_cache_status; 
     proxy_cache my_zone_domain_1; 
     proxy_pass http://domain1.com:8000/api; 
    } 

} 
端口8000上domain_2
server {   
    server_name www.domain2.com; 
    return 301 $scheme://domain2.com$request_uri; 
} 

server { 
    listen 3000; 
    server_name domain2.com; 

    root /var/www/example_site_2; 

    # common locations for all sites 
    location/{ 
     include uwsgi_params; 
     uwsgi_pass unix:/var/www/example_site_2/domain2.sock; 
    } 

    # API 
    location /api {      
     include uwsgi_params; 
     uwsgi_param UWSGI_SCRIPT wsgi; 
     uwsgi_pass unix:/var/www/example_site_2/domain2.sock; 
    } 
} 



# Set cache directory for site 

proxy_cache_path /tmp/nginx/domain2 levels=1:2 keys_zone=my_zone_domain_2:10m max_size=50m inactive=60m; 
proxy_cache_key "$scheme$request_method$host$request_uri"; 


# Virtualhost/server configuration 
server { 

    # I tried listening on other ports than 80, but kept having redirects on domain_1  

    listen 80; 
    server_name domain2; 

    root /var/www/domain2; 

    ## how to serve proxy_cache if locations of domain_1 and domain_2 are the same ? 

    location/{   
     proxy_cache my_zone_domain_2;  
     add_header X-Proxy-Cache $upstream_cache_status; 
     include proxy_params; 
     proxy_pass http://domain2.com:3000; 
    } 

    location /api { 
     add_header X-Proxy-Cache $upstream_cache_status; 
     proxy_cache my_zone_domain_2; 
     proxy_pass http://domain2.com:3000/api; 
    } 

} 

回答

0

你domain_2配置使用proxy_pass http://domain2.com:8000但只有DOMAIN_1服務器配置監聽所以它可以提供指向domain_2的請求。

我也建議您在配置中重新考慮proxy_pass的使用情況,但這不是真的有必要。

+0

我的錯字:proxy_pass位置/ api在兩個不同的門(3000和8000)上偵聽,但它仍然將domain_2重定向到domain_1。 您是否可以詳細說明重新思考proxy_pass的用法? 我的用例是緩存GET APIs,其中包含domain.com/entity/one(忽略domain.com/entity?q=one)等端點的響應200 - 並避免domain_1和domain_2之間發生衝突,因爲apis帶有相同的名稱。 你想發佈一個例子嗎? – user305883

+0

端口80的domain2配置的另一個問題是'server_name domain2;'不會匹配到'domain2.com'的請求,這些請求將由服務器部分通過'server_name domain1 default_server;'處理因爲它有'default_server'選項。您不需要'proxy_pass',因爲您可以在偵聽端口80的服務器部分中使用'uwsgi_pass',只需使用'uwsgi_cache'而不是'proxy_cache'。 – AlexD

+0

不工作:我從第一個服務器塊的配置domain_1中刪除了default_server。現在'server {listen 8000; server_name domain1; ...},'server {listen 3000; server_name domain2; ...}和'服務器{聽80; DOMAIN1; }'和'server {listen 80;域2; }'我也嘗試了後者聽8080端口,但沒有成功:'curl localhost:3000/api'一直在端口8000上重定向。 – user305883

0

我發現罪魁禍首是Uwsgi:它從usr/bin文件夾加載,而不是從我的應用程序的虛擬環境文件夾中加載。

也許virtualenv文件夾被破壞:當我試圖重新安裝uwsgi(pip install uwsgi)時,它一直說要求滿足,直到我注意到沒有從virtualenv加載which uwsgi

我不得不使用uwsgi和python模塊刪除並重新安裝virtualenv文件夾。

現在應用程序正在運行(對問題的回答),但在我的情況下proxy_server配置必須進一步調整。

以下可能對在燒瓶中使用url_for()指令的人有用:url_for()指令路由到絕對路徑,這可能與nginx代理髮生衝突。

示例:nginx代理服務器在端口80上偵聽domains2.com,proxy_pass /path位置爲http://domain2.com:3000;如果燒瓶正在將路由重定向到/path(使用url_for()),則生成的url將爲http://domain2.com:3000/path(因爲它遵循絕對路徑中指定的端口)而不是http://domain2.com/path(代理的URL)。

proxy_set_header Host $http_host;添加到/path位置以允許nginx代理遵循燒瓶應用程序的正確重定向。

相關問題