Docker+django+gunicorn连接到主机系统的Nginx



在ubuntu服务器18.04上
我用Django 3.1和Gunicorn运行了一个docker容器它连接到本地安装的Postgres。

我已经有另一个Django项目,在同一台服务器上有Nginx和Gunicorn,没有docker。

问题是:如何在容器中为Gunicorn设置套接字文件,并将该容器连接到现有的外部Nginx,以便我可以从外部访问容器化的django应用程序

docker compose.prod.yml

version: '3'
services:
app:
build:
context: .
ports:
- "8001:8001"
volumes:
- ./app:/app
env_file:
- .env.prod
command: >
sh -c "gunicorn app.wsgi:application --bind 0.0.0.0:8001"

我这样运行容器:

docker-compose -f docker-compose.prod.yml up

.env.prod

...
DJANGO_ALLOWED_HOSTS='111.111.111.111 .mysitename.com localhost 172.18.0.1 127.0.0.1 [::1]'
...

当运行容器执行此命令时:

curl 'http://127.0.0.1:8001/admin/login/?next=/admin/'

给了我管理页面的html代码,它是可以的。

现有的Nginx是在这些数字海洋教程中设置的:

https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04

我还通过让我们使用本教程加密来保护它https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-18-04

一切都比我想象的简单。

在Nginx conf文件中

/etc/nginx/站点可用/像那样编辑

server {
server_name 111.111.111.111 domainname.com www.domainname.com *.domainname.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/user/<my-previous-project-installed-locally>/project-app;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
# Added for the NEW application
###############################################
location  /MY-NEW-PATH/ {
rewrite ^/MY-NEW-PATH(.*) $1 break;
proxy_pass http://127.0.0.1:8001;
}
###############################################
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/domainname.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/domainname.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.domainname.com) {
return 301 https://$host$request_uri;
} # managed by Certbot

相关内容

  • 没有找到相关文章

最新更新