首页 > 解决方案 > 我在使用 docker nginx 和 gunicorn 部署我的 django 项目时遇到问题

问题描述

如果我在服务器端运行了我的容器并且一切正常,为了在浏览器上访问路由,我应该将带有域的 nginx 容器的 ip 放在 etc/host 中还是必须在没有它的情况下工作?我的 nginx.config

server {

    listen 80;

    server_name my_domain;

    root /code/;
    error_log /var/log/nginx/new_error.log debug;


    location / {
        proxy_pass http://web:8000/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /staticfiles/ {
        alias /code/staticfiles/;
    }

    location /mediafiles/ {
        alias /code/mediafiles/;
    }

}

web 是我运行 gunicorn 的 docker 容器

标签: djangodockernginxgunicorn

解决方案


如果您的 nginx 容器具有使用该标志公开的端口,-p <host_port>:<container_port>您可以访问 nginx 服务,在您的 /etc/hosts 文件中添加指向本地主机 ip 的域,但是如果您没有使用此标志,则需要指向该 ip你的 nginx 容器。有什么区别?...当您公开端口时,您甚至可以在部署容器的主机之外使用该服务。

我希望能帮助你。


推荐阅读