【问题标题】:docker with nginx and gunicorn not opening in browser带有 nginx 和 gunicorn 的 docker 未在浏览器中打开
【发布时间】:2018-11-13 21:54:56
【问题描述】:

我是Docker 的新手,并设置docker 以使用gunicornnginx 运行Django 应用程序

我的配置文件是

docker-compose.yml

version: '3'

services:
  nginx:
    image: nginx:latest
    container_name: "myapp-nginx"
    ports:
      - "10080:80"
      - "10443:43"
    volumes:
      - .:/app
      - ./config/nginx:/etc/nginx/conf.d
      - ./static_cdn:/static
    depends_on:
      - web
  web:
    build: .
    container_name: "myapp-dev"
    command: ./start.sh
    volumes:
      - .:/app
      - ./static_cdn:/static
    ports:
      - "9010"
    depends_on:
      - db
    expose:
      - "9010"
  db:
    image: postgres
    container_name: "myapp-db"

config/nginx/nginx.conf

upstream web {
    ip_hash;
    server web:9010;
}

server {
    location /static {
        autoindex on;
        alias /static/;
    }

    location / {
        proxy_pass http://web;
    }
    listen 9010;
    server_name localhost;
}

start.sh

#!/usr/bin/env bash

# Start Gunicorn processes
echo --: Starting application build
echo --: Creating migration
python3 manage.py makemigrations
echo ------: makemigrations complete
echo --: Running migration
python3 manage.py migrate
echo ------: migrate complete
echo --: Running collectstatic
python3 manage.py collectstatic <<<yes
echo ------: collectstatic complete
echo --: Starting Gunicorn.
gunicorn koober.wsgi:application \
    --bind 0.0.0.0:9010 \
    --workers 3

使用 docker 运行

docker-compose up --build

运行成功,输出为

这里 gunicorn 在0.0.0.0:9010 成功启动。但无法使用浏览器访问应用程序。

我在浏览器中尝试了以下地址

  1. 127.0.0.1:9010
  2. 127.0.0.1:10080
  3. 127.0.0.1
  4. 本地主机:9010
  5. 本地主机:10080
  6. 本地主机
  7. 0.0.0.0:9010
  8. 0.0.0.0:10080
  9. 0.0.0.0

但它们都不起作用。

编辑 2:docker ps -a 的输出

【问题讨论】:

    标签: docker nginx


    【解决方案1】:

    试试这个

    upstream web {
      ip_hash;
      server web:9010;
    }
    server {
    
     listen       10080;
    
     location / {
        proxy_pass http://web;
     }     
    }
    

    Nginx 应该侦听 10080 端口,因为在您的撰写文件中,您已将端口 80 公开到 10080 端口。

    然后尝试 http://localhost:10080http://machine-ip-address:10080

    这是我写的博客,用来解释 Docker + Nginx + Web 应用程序如何协同工作。

    https://rohanjmohite.wordpress.com/2017/08/02/how-to-configure-docker-with-nginx-and-php-application/

    源代码 https://github.com/RohanMohite/Docker-Nginx-PHP/blob/master/server_nginx/conf/server.conf

    【讨论】:

    • 我试过了,但没有成功。我使用ipconfig getifaddr en0 找到了返回192.168.31.13 的IP 地址,我也尝试使用此IP 地址但没有成功。
    • 我在使用 nginx 并作为核心服务运行的 mac 机器中设置了 valet。有影响吗?
    • 还有一件事 - 在你的 docker-compose 文件中,你可以尝试端口: - “9010:9010” 并删除暴露标签。
    • 是的,我将 9010:9010 添加到 web 并在 127.0.0.1:9010 工作。但无法理解端口的流向。我们已将listen 10080; 添加到 nginx conf 中,它正在处理9010。你能解释一下它的工作原理吗?
    • 它应该在 10080 端口上工作,请你交叉检查你的 Nginx 配置文件。最后,您应该能够点击 127.0.0.1:10080 url,它应该会自动将您重定向到 Web 服务器。
    猜你喜欢
    • 2021-09-23
    • 2019-03-22
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 2014-09-28
    • 2011-10-22
    相关资源
    最近更新 更多