我们根据将暴露的http服务修改为https服务后,通过https协议即能访问我们需要的网站,例如下面这样的应用模板
app: ports: - 80/tcp image: 'registry.cn-hangzhou.aliyuncs.com/linhuatest/hello-world:latest' labels: # 此处只是http/https/ws/wss 协议 aliyun.routing.port_80: "http://www.example.com" restart: always配置好slb之后,访问https协议的网站如下所示
下面我们配置一个nginx容器将rewrite规则写到配置文件中,即如果收到请求http://www.example.com ,则返回301且自动跳转到https://www.example.com 。登陆集群所在的每台机器,创建nginx配置文件(将会作为volume挂载到容器nginx中)/ngx/nginx.conf如下所示:
user nginx; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 65535; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; keepalive_timeout 65; gzip on; server { listen 80; server_name localhost; return 301 https://$host$request_uri; } }nginx容器模板如下所示:
nginx: ports: - 80:80/tcp # 映射到主机的80端口 image: 'nginx:latest' labels: aliyun.global: true # 每台机器均部署一个nginx容器,达到高可用目的 volumes: - /ngx/nginx.conf:/etc/nginx/nginx.conf restart: always集群slb的配置如下图所示(其中前端80端口 -> 后端80端口,即主机端口 ->nginx的容器端口80)当我们访问http://www.example.com 时,返回的http协议内容如下图所示,即完成了正确的跳转到https://www.example.com
