Nginx 将 http 子域重定向到 https
Nginx redirect http subdomains to https
我有一个包含 3 个子域的域:
- example.com (main domain)
- api.example.com
- blog.example.com
- support.example.com (just a cname to point to zendesk)
我的 Nginx 上有这 3 个配置:
api
# HTTP server
server {
listen 80;
server_name api.example.com;
return 301 https://api.example.com$request_uri;
}
# HTTPS server
server {
ssl on;
listen 443;
server_name api.example.com;
ssl_certificate APIcert.crt;
ssl_certificate_key APIcert.key;
#root configuration.....
}
博客
server {
listen 80;
server_name blog.example.com;
root /var/www/blog;
index index.php index.html index.htm;
site/main 域
server {
listen 80;
listen 443 ssl;
server_name www.example.com;
return 301 https://example.com$request_uri;
location ~ \.(php|html)$ {
deny all;
}
}
server {
listen 80;
server_name example.com;
return 301 https://example.com$request_uri;
location ~ \.(php|html)$ {
deny all;
}
}
server {
ssl on;
listen 443 ssl;
ssl_certificate mycert.crt;
ssl_certificate_key mycert.key;
server_name example.com;
root /var/www/frontend;
.....
}
我的问题:
- 子域名api.example没问题!
- 主域http://example.com and https://example.com可以!
- 如果我尝试使用 HTTP 上的 www 访问主域,浏览器重定向正确到 https://example.com. But when I try to access the main domain with www and https, https://www.example.com,浏览器尝试从 api.
访问 SSL CERT
- 在我尝试访问主域并重定向到 HTTPS 后,我的其他没有 HTTPS 的子域被重定向到 https 并显示错误,因为他们试图使用来自 api 的 SSL CERT。
- 示例:如果我尝试访问 http://blog.exemple.com, firefox redirect to https://blog.example.com 并显示 SSL 错误。
- 这是一个显示this problem
的视频
- 域名已上线,您可以在http://blog.alooga.com.br, http://alooga.com.br上测试
您的 Web 服务器设置为 Strict-Transport-Security max-age=16070400; includeSubdomains
。
这将告诉网络浏览器仅使用 https 请求您的域。如果您希望通过不安全的 http 访问子域 blog
,您需要从 HTTP 严格传输安全 (HSTS) 中删除 includeSubdomains
并使用不同的浏览器(或清除您的 Firefox)。
https://www.ssllabs.com/ssltest/analyze.html?d=alooga.com.br
您收到 SSL 错误消息是因为您没有 blog.alooga.com.br
域的 SSL 证书。
供您参考的 SSL 错误信息:
我有一个包含 3 个子域的域:
- example.com (main domain) - api.example.com - blog.example.com - support.example.com (just a cname to point to zendesk)
我的 Nginx 上有这 3 个配置:
api
# HTTP server
server {
listen 80;
server_name api.example.com;
return 301 https://api.example.com$request_uri;
}
# HTTPS server
server {
ssl on;
listen 443;
server_name api.example.com;
ssl_certificate APIcert.crt;
ssl_certificate_key APIcert.key;
#root configuration.....
}
博客
server {
listen 80;
server_name blog.example.com;
root /var/www/blog;
index index.php index.html index.htm;
site/main 域
server {
listen 80;
listen 443 ssl;
server_name www.example.com;
return 301 https://example.com$request_uri;
location ~ \.(php|html)$ {
deny all;
}
}
server {
listen 80;
server_name example.com;
return 301 https://example.com$request_uri;
location ~ \.(php|html)$ {
deny all;
}
}
server {
ssl on;
listen 443 ssl;
ssl_certificate mycert.crt;
ssl_certificate_key mycert.key;
server_name example.com;
root /var/www/frontend;
.....
}
我的问题:
- 子域名api.example没问题!
- 主域http://example.com and https://example.com可以!
- 如果我尝试使用 HTTP 上的 www 访问主域,浏览器重定向正确到 https://example.com. But when I try to access the main domain with www and https, https://www.example.com,浏览器尝试从 api. 访问 SSL CERT
- 在我尝试访问主域并重定向到 HTTPS 后,我的其他没有 HTTPS 的子域被重定向到 https 并显示错误,因为他们试图使用来自 api 的 SSL CERT。
- 示例:如果我尝试访问 http://blog.exemple.com, firefox redirect to https://blog.example.com 并显示 SSL 错误。
- 这是一个显示this problem 的视频
- 域名已上线,您可以在http://blog.alooga.com.br, http://alooga.com.br上测试
您的 Web 服务器设置为 Strict-Transport-Security max-age=16070400; includeSubdomains
。
这将告诉网络浏览器仅使用 https 请求您的域。如果您希望通过不安全的 http 访问子域 blog
,您需要从 HTTP 严格传输安全 (HSTS) 中删除 includeSubdomains
并使用不同的浏览器(或清除您的 Firefox)。
https://www.ssllabs.com/ssltest/analyze.html?d=alooga.com.br
您收到 SSL 错误消息是因为您没有 blog.alooga.com.br
域的 SSL 证书。
供您参考的 SSL 错误信息: