【问题标题】:How to create an nginx service in a docker swarm at port 443 and 80如何在端口 443 和 80 的 docker swarm 中创建 nginx 服务
【发布时间】:2018-02-06 22:12:49
【问题描述】:

我正在尝试在生产环境中使用 2 个节点的 docker swarm 中创建一个具有 2 个副本的 nginx 服务。节点是在数字海洋中创建的。 此 nginx 服务将充当 apache 虚拟主机的反向代理 (https http)。 要创建我使用的 nginx 服务:

docker service create --replicas 2 -p 80:80 --p 443:443 --name webserver --mount type=bind,source=/environments/ssl-env,destination=/etc/nginx/ssl - -mount type=bind,source=/conf/nginx.conf,destination=/etc/nginx/nginx.conf --mount type=bind,source=/middleware,destination=/etc/nginx/conf.d nginx

运行此命令后,服务无法启动,没有任何有用的错误消息。但是,只有在工作节点中,docker daemon 才会监听 443 端口:

netstat -tulpn | grep :443
tcp6 0 0 :::443 :::* LISTEN 5797/dockerd

另外,当我评论 nginx.conf 中监听 443 的 https 部分时,我的 nginx 服务已创建并成功运行,但我当然想使用 https 部分。你有什么主意吗? Docker 版本 17.05.0-ce,构建 89658be。 这是 nginx.conf 的一部分:

#http
server {
    listen 80 ;
    server_name api.hotelgenius.net;
    # redirect http to https ##
    rewrite ^ https://$server_name$request_uri permanent;
}

#https
#server {
   listen 443 ;
   server_name api.hotelgenius.net;
   error_log /var/log/nginx/api_error.log;
   access_log /var/log/nginx/api_access.log;
   ssl on;
   ssl_certificate /etc/nginx/ssl/api.hotelgenius.crt;
   ssl_certificate_key /etc/nginx/ssl/api.hotelgenius.key;
   ssl_client_certificate /etc/nginx/ssl/api.hotelgenius.cer;

   location / {
       proxy_pass http://hotelgenius/;
       proxy_set_header Host $host;
       proxy_redirect http:// https://;
}

【问题讨论】:

  • 检查端口80是否已经在运行,你的命令中的一个标志是-p,一个是--p,希望这是一个错字
  • 没有任何东西在 80 中运行。是的 --p 是一个错字
  • 你有没有试过docker service logs 也试过journalctl -n 10 -f 然后在另一个终端创建服务,看看你是否能找到任何问题
  • 我有许多可以部署在 docker swarm 中的 docker 容器,例如 apache、mysql、mongo、prerender。当我尝试部署 nginx 容器时出现问题。我使用 docker-compose 部署前 4 个容器,然后尝试将 nginx 服务附加到由前 4 个容器创建的 docker swarm 网络中。我不确定这是否完全正确。目前,我从 docker 服务日志中看到 nginx 找不到 prerender 服务
  • 服务发现仅适用于 docker 服务,不适用于非 swarm 模式下运行的容器。所以你不应该混合两者。为什么不使用stackservices 来部署nginx

标签: docker nginx swarm


【解决方案1】:

将 nginx.conf 中的容器名称替换为 docker 堆栈中相应的服务名称后,Nginx 服务部署成功。例如,在修复之前我在 nginx.conf 中

location / {
       proxy_pass http://hotelgenius/;
       proxy_set_header Host $host;
       proxy_redirect http:// https://;
}

其中 'hotelgenius' 是容器的名称。我不得不将 'hotelgenius' 替换为服务名称,因为在 docker-compose.yml 版本 3 中不再支持容器名称。

【讨论】:

    猜你喜欢
    • 2018-01-25
    • 2014-10-13
    • 1970-01-01
    • 2019-06-29
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    相关资源
    最近更新 更多