AWS 中使用GA,服务从http自动跳转到https
·
一、自动跳转方法
1.1 使用 rewrite 方法
使用nginx的 rewrite 将请求过来的 http URL直接重写成 https
server {
listen 80;
#填写域名
server_name www.xxx.com;
#将http的URL重写成https
rewrite ^(.*) https://$server_name$1 permanent;
}
1.2 使用 301 永久跳转
server {
listen 80;
#填写域名
server_name www.xxx.com;
#把http的域名请求转成https
return 301 https://$host$request_uri;
}
二、使用GA之后如何自动跳转
2.1 服务访问路径
www.xxx.com --> AWS GA --> AWS ALB --> EC2 --> Nginx
以下是nginx配置:
$ cat /etc/nginx/conf.d/web-h5.conf
server {
server_name _;
listen 9999;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
add_header Access-Control-Allow-Origin '*';
add_header Access-Control-Allow-Methods '*';
add_header Access-Control-Allow-Headers '*';
#禁用druid访问链接
location ~* /druid/login.html {
deny all;
return 403;
}
location /app/ {
proxy_pass http://172.31.7.55:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
}
location / {
root /usr/local/web/h5/dist;
index index.html;
try_files $uri $uri/ @router;
}
location @router {
rewrite ^.*$ /index.html last;
}
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
# 自动跳转是这一部分
server {
if ($host = www.xxx.com) {
return 301 https://$host$request_uri;
}
server_name www.xxx.com;
listen 80;
return 404;
}
在 GA配置跳转的时候要把 443 和 80 都配置上

ALB分别帧听器配置了ssl,

这样访问 www.xxx.com 的时候将会自动跳转到 https://www.xxx.com
总结:1. nginx 配置 自动跳转 2. GA服务加速对应的端口80 和 443。
更多推荐



所有评论(0)