首页 文章

如何在端口443和80的docker swarm中创建nginx服务

提问于
浏览
2

我正在尝试在 生产环境 环境中具有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

运行此命令后,服务无法启动,没有任何有用的错误消息 . 但是,只有在worker节点中,docker守护程序才会侦听端口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://;
}

1 回答

  • 0

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

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

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

相关问题