我想允許我的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_1server {
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;
}
}
我的錯字: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
端口80的domain2配置的另一個問題是'server_name domain2;'不會匹配到'domain2.com'的請求,這些請求將由服務器部分通過'server_name domain1 default_server;'處理因爲它有'default_server'選項。您不需要'proxy_pass',因爲您可以在偵聽端口80的服務器部分中使用'uwsgi_pass',只需使用'uwsgi_cache'而不是'proxy_cache'。 – AlexD
不工作:我從第一個服務器塊的配置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