开发者社区> 问答> 正文

Swarm mode 集群中服务发现和负载均衡的简单路由-HTTP 强制跳转到 HTTPS

反向一觉 2017-10-30 16:00:34 1394
您可以根据 将暴露的 HTTP 服务修改为 HTTPS 服务 进行操作实现通过 HTTPS 协议访问您需要的网站。
操作流程
  1. 您可以使用编排模板创建 hello world 应用。
    应用模板示例如下:
    [backcolor=transparent]compose V1/V2 示例[backcolor=transparent] app[backcolor=transparent]:
  2. [backcolor=transparent]   ports[backcolor=transparent]:
  3. [backcolor=transparent]     [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]80[backcolor=transparent]/[backcolor=transparent]tcp
  4. [backcolor=transparent]   image[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]'registry.cn-hangzhou.aliyuncs.com/linhuatest/hello-world:latest'
  5. [backcolor=transparent]   labels[backcolor=transparent]:
  6. [backcolor=transparent]     [backcolor=transparent]# 此处只是 http/https/ws/wss 协议
  7. [backcolor=transparent]     aliyun[backcolor=transparent].[backcolor=transparent]routing[backcolor=transparent].[backcolor=transparent]port_80[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]"http://www.example.com"
  8. [backcolor=transparent]   restart[backcolor=transparent]:[backcolor=transparent] always

[backcolor=transparent]compose V3 示例
  1. [backcolor=transparent] version[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]'3'
  2. [backcolor=transparent] services[backcolor=transparent]:
  3. [backcolor=transparent]   web[backcolor=transparent]:
  4. [backcolor=transparent]     image[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]'registry.cn-hangzhou.aliyuncs.com/linhuatest/hello-world:latest'
  5. [backcolor=transparent]     ports[backcolor=transparent]:
  6. [backcolor=transparent]       [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]80[backcolor=transparent]/[backcolor=transparent]tcp
  7. [backcolor=transparent]     deploy[backcolor=transparent]:
  8. [backcolor=transparent]       restart_policy[backcolor=transparent]:
  9. [backcolor=transparent]         condition[backcolor=transparent]:[backcolor=transparent] on[backcolor=transparent]-[backcolor=transparent]failure
  10. [backcolor=transparent]     labels[backcolor=transparent]:
  11. [backcolor=transparent]       aliyun[backcolor=transparent].[backcolor=transparent]routing[backcolor=transparent].[backcolor=transparent]port_80[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]"http://www.example.com"

配置好负载均衡之后,参见 HTTP 协议变为HTTPS,访问 HTTPS 协议的网站如下所示。

您可以配置使 HTTP 协议请求强制跳转到 HTTPS 协议。
下面的示例配置了一个 nginx 容器并将 rewrite 规则写到配置文件中,即如果收到请求 http://www.example.com,则返回 301 且自动跳转到 https://www.example.com
登录集群中的[backcolor=transparent]每台机器,创建 nginx 配置文件(将会作为 volume 挂载到容器 nginx 中)/ngx/nginx.conf 如下所示。
  1. [backcolor=transparent] user  nginx[backcolor=transparent];
  2. [backcolor=transparent] error_log  [backcolor=transparent]/[backcolor=transparent]var[backcolor=transparent]/[backcolor=transparent]log[backcolor=transparent]/[backcolor=transparent]nginx[backcolor=transparent]/[backcolor=transparent]error[backcolor=transparent].[backcolor=transparent]log warn[backcolor=transparent];
  3. [backcolor=transparent] pid        [backcolor=transparent]/[backcolor=transparent]var[backcolor=transparent]/[backcolor=transparent]run[backcolor=transparent]/[backcolor=transparent]nginx[backcolor=transparent].[backcolor=transparent]pid[backcolor=transparent];
  4. [backcolor=transparent] events [backcolor=transparent]{
  5. [backcolor=transparent]     worker_connections  [backcolor=transparent]65535[backcolor=transparent];
  6. [backcolor=transparent] [backcolor=transparent]}
  7. [backcolor=transparent] http [backcolor=transparent]{
  8. [backcolor=transparent]     include       [backcolor=transparent]/[backcolor=transparent]etc[backcolor=transparent]/[backcolor=transparent]nginx[backcolor=transparent]/[backcolor=transparent]mime[backcolor=transparent].[backcolor=transparent]types[backcolor=transparent];
  9. [backcolor=transparent]     default_type  application[backcolor=transparent]/[backcolor=transparent]octet[backcolor=transparent]-[backcolor=transparent]stream[backcolor=transparent];
  10. [backcolor=transparent]     log_format  main  [backcolor=transparent]'$remote_addr - $remote_user [$time_local] "$request" '
  11. [backcolor=transparent]                       [backcolor=transparent]'$status $body_bytes_sent "$http_referer" '
  12. [backcolor=transparent]                       [backcolor=transparent]'"$http_user_agent" "$http_x_forwarded_for"'[backcolor=transparent];
  13. [backcolor=transparent]     access_log  [backcolor=transparent]/[backcolor=transparent]var[backcolor=transparent]/[backcolor=transparent]log[backcolor=transparent]/[backcolor=transparent]nginx[backcolor=transparent]/[backcolor=transparent]access[backcolor=transparent].[backcolor=transparent]log  main[backcolor=transparent];
  14. [backcolor=transparent]     keepalive_timeout  [backcolor=transparent]65[backcolor=transparent];
  15. [backcolor=transparent]     gzip  on[backcolor=transparent];
  16. [backcolor=transparent]     server [backcolor=transparent]{
  17. [backcolor=transparent]         listen       [backcolor=transparent]80[backcolor=transparent];
  18. [backcolor=transparent]         server_name  localhost[backcolor=transparent];
  19. [backcolor=transparent]         [backcolor=transparent]return[backcolor=transparent] [backcolor=transparent]301[backcolor=transparent] https[backcolor=transparent]:[backcolor=transparent]//$host$request_uri;
  20. [backcolor=transparent]     [backcolor=transparent]}
  21. [backcolor=transparent] [backcolor=transparent]}

使用编排模板创建 nginx 应用。
编排示例如下:
[backcolor=transparent]compose V1/V2 示例
  1. [backcolor=transparent] nginx[backcolor=transparent]:
  2. [backcolor=transparent]   ports[backcolor=transparent]:
  3. [backcolor=transparent]     [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]80[backcolor=transparent]:[backcolor=transparent]80[backcolor=transparent]/[backcolor=transparent]tcp [backcolor=transparent]# 映射到主机的 80 端口
  4. [backcolor=transparent]   image[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]'nginx:latest'
  5. [backcolor=transparent]   labels[backcolor=transparent]:
  6. [backcolor=transparent]     aliyun[backcolor=transparent].[backcolor=transparent]global[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]true[backcolor=transparent] [backcolor=transparent]# 每台机器均部署一个 nginx 容器,达到高可用目的
  7. [backcolor=transparent]   volumes[backcolor=transparent]:
  8. [backcolor=transparent]     [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]/ngx/[backcolor=transparent]nginx[backcolor=transparent].[backcolor=transparent]conf[backcolor=transparent]:[backcolor=transparent]/etc/[backcolor=transparent]nginx[backcolor=transparent]/[backcolor=transparent]nginx[backcolor=transparent].[backcolor=transparent]conf
  9. [backcolor=transparent]   restart[backcolor=transparent]:[backcolor=transparent] always

[backcolor=transparent]compose V3 示例
  1. [backcolor=transparent] version[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]'3'
  2. [backcolor=transparent] services[backcolor=transparent]:
  3. [backcolor=transparent]   nginx[backcolor=transparent]:
  4. [backcolor=transparent]     ports[backcolor=transparent]:
  5. [backcolor=transparent]       [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]80[backcolor=transparent]:[backcolor=transparent]80[backcolor=transparent]/[backcolor=transparent]tcp [backcolor=transparent]# 映射到主机的 80 端口
  6. [backcolor=transparent]     image[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]'nginx:latest'
  7. [backcolor=transparent]     deploy[backcolor=transparent]:
  8. [backcolor=transparent]       mode[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]global[backcolor=transparent]  [backcolor=transparent]#在每个节点下运行一个容器
  9. [backcolor=transparent]       restart_policy[backcolor=transparent]:
  10. [backcolor=transparent]         condition[backcolor=transparent]:[backcolor=transparent] on[backcolor=transparent]-[backcolor=transparent]failure
  11. [backcolor=transparent]     volumes[backcolor=transparent]:
  12. [backcolor=transparent]       [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]/ngx/[backcolor=transparent]nginx[backcolor=transparent].[backcolor=transparent]conf[backcolor=transparent]:[backcolor=transparent]/etc/[backcolor=transparent]nginx[backcolor=transparent]/[backcolor=transparent]nginx[backcolor=transparent].[backcolor=transparent]conf

配置集群负载均衡的监听规则。
如下图所示(其中,前端 80 端口 > 后端 80 端口,即主机端口 > nginx 的容器端口 80)。

验证 HTTP 强制跳转到 HTTPS
当您访问 http://www.example.com 时,返回的 HTTP 协议内容如下图所示,即完成了正确跳转到 https://www.example.com

分享到
取消 提交回答
全部回答(0)
+ 订阅

时时分享云计算技术内容,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

推荐文章