在同一域下使用Nginx和Gunicorn服务多个Django应用程序
现在我在一个域中有一个Django项目。我想为一个域下的三个Django项目提供服务器,每个域之间用/。例如:www.domain.com/firstone/、www.domain.com/secondone/等。如何配置nGinx为一个域下的多个Django项目提供服务?在这种情况下,如何配置静态文件服务 我当前的nGinx配置是:在同一域下使用Nginx和Gunicorn服务多个Django应用程序,django,nginx,gunicorn,Django,Nginx,Gunicorn,现在我在一个域中有一个Django项目。我想为一个域下的三个Django项目提供服务器,每个域之间用/。例如:www.domain.com/firstone/、www.domain.com/secondone/等。如何配置nGinx为一个域下的多个Django项目提供服务?在这种情况下,如何配置静态文件服务 我当前的nGinx配置是: server {
server {
listen 80;
listen [::]:80;
server_name domain.com www.domain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name domain.com www.domain.com;
ssl_certificate /etc/nginx/ssl/Certificate.crt;
ssl_certificate_key /etc/nginx/ssl/Certificate.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
root /home/admin/web/project;
location /static {
alias /home/admin/web/project/static;
}
location /media {
alias /home/admin/web/project/media;
}
location /assets {
alias /home/admin/web/project/assets;
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-Proto https;
proxy_connect_timeout 75s;
proxy_read_timeout 300s;
proxy_pass http://127.0.0.1:8000/;
client_max_body_size 100M;
}
# Proxies
# location /first {
# proxy_pass http://127.0.0.1:8001/;
# }
#
# location /second {
# proxy_pass http://127.0.0.1:8002/;
# }
error_page 500 502 503 504 /media/50x.html;
您必须在不同的端口上运行您的项目,如8000上的firsrone和8001上的secondone。 然后在nginx conf中,代替
location/
,您必须写入location/firstone/
并代理将其传递到端口8000,然后为第二个端口写入与location/secondone/
相同的位置对象,并代理将其传递到端口8001
对于静态文件和介质,您必须使它们以/firstone/static的形式可用,而对于secondone则相同。
另一种方法是为这两个项目指定相同的MEDIA\u ROOT和STATIC\u ROOT。phython教授所说的应该是正确的。我不是这方面的专家,但我在我们的服务器上也看到了类似的情况。希望共享的nginx.conf文件有帮助
server {
listen 80;
listen [::]:80;
server_name alicebot.tech;
return 301 https://web.alicebot.tech$request_uri;
}
server {
listen 80;
listen [::]:80;
server_name web.alicebot.tech;
return 301 https://web.alicebot.tech$request_uri;
}
server {
listen 443 ssl;
server_name alicebot.tech;
ssl_certificate /etc/ssl/alicebot_tech_cert_chain.crt;
ssl_certificate_key /etc/ssl/alicebot.key;
location /static/ {
expires 1M;
access_log off;
add_header Cache-Control "public";
proxy_ignore_headers "Set-Cookie";
}
location / {
include proxy_params;
proxy_pass http://unix:/var/www/html/alice/alice.sock;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
server {
listen 443 ssl;
server_name web.alicebot.tech;
ssl_certificate /etc/letsencrypt/live/web.alicebot.tech/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/web.alicebot.tech/privkey.pem; # managed by Certbot
location /static/ {
autoindex on;
alias /var/www/html/static/;
expires 1M;
access_log off;
add_header Cache-Control "public";
proxy_ignore_headers "Set-Cookie";
}
location / {
include proxy_params;
proxy_pass http://unix:/var/www/alice_v2/alice/alice.sock;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
server {
listen 8000 ssl;
listen [::]:8000 ssl;
server_name alicebot.tech;
ssl_certificate /etc/ssl/alicebot_tech_cert_chain.crt;
ssl_certificate_key /etc/ssl/alicebot.key;
location /static/ {
autoindex on;
alias /var/www/alice_v2/static/;
expires 1M;
access_log off;
add_header Cache-Control "public";
proxy_ignore_headers "Set-Cookie";
}
location / {
include proxy_params;
proxy_pass http://unix:/var/www/alice_v2/alice/alice.sock;
}
}
正如你所看到的,我们这里有不同的域名,你不需要。因此,您需要更改服务器{…}中的服务器名称,@prof.phython正确地指出,您需要为每个应用程序运行单独的gunicorn进程。这导致每个应用程序都在一个单独的端口上运行 接下来,在http下为每个应用程序服务器创建一个单独的上游块:
upstream app1 {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response
# for UNIX domain socket setups
#server unix:/tmp/gunicorn.sock fail_timeout=0;
# for a TCP configuration
server 127.0.0.1:9000 fail_timeout=0;
}
显然,要相应地更改每个上游区块的标题和端口号
然后,在您的http->server
块下,为每个模块定义以下内容:
location @app1_proxy {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;
proxy_pass http://app1;
}
确保最后一行指向您所称的上游块(app1),并且@app1_proxy
也应特定于该应用程序
最后,在http->server
块中,使用以下代码将URL映射到应用程序服务器:
location /any/subpath {
# checks for static file, if not found proxy to app
try_files $uri @app1_proxy;
}
谢谢!但这是子域的配置