文章目录
- 原因 说明 安全性 防止中间人攻击 SEO Google优先收录HTTPS 信任度 浏览器显示安全标识 合规 PCI DSS等合规要求
-
- 在 server 配置中添加: server { listen 80; server_name example.com www.example.com; return 301 https://$server_name$request_uri; } server { listen 443 ssl http2; server_name example.com www.example.com; ssl_certificate /etc/ssl/certs/example.crt; ssl_certificate_key /etc/ssl/private/example.key; # 强制HTTPS add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; root /var/www/html; index index.php index.html; }
- server { listen 80; server_name example.com; location = / { return 301 https://$server_name$request_uri; } location / { proxy_pass http://127.0.0.1:8080; } }
-
- sudo a2enmod rewrite sudo systemctl restart apache2
- RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
- <VirtualHost *:80> ServerName example.com DocumentRoot /var/www/html RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^/?(.*)$ https://%{SERVER_NAME}/$1 [R=301,L] </VirtualHost>
- HTTP Strict Transport Security强制浏览器使用HTTPS: add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always; 参数 说明 max-age HSTS有效期(秒) includeSubDomains 包含子域名 preload 申请预加载列表
- 在Cloudflare控制台设置: SSL/TLS → 加密模式:完全灵活或完全严格 页面规则: URL:http://*example.com/* 设置:始终使用HTTPS 转发规则:301永久重定向
- 问题 解决方案 跳转循环 检查HTTPS是否也监听80端口 证书错误 更新SSL证书 资源加载失败 内部链接使用协议相对路径 移动端问题 检查重定向状态码是否为301
- server { listen 80; server_name example.com www.example.com; return 301 https://$server_name$request_uri; } server { listen 443 ssl http2; server_name example.com www.example.com; ssl_certificate /etc/ssl/certs/example.crt; ssl_certificate_key /etc/ssl/private/example.key; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256; root /var/www/html; index index.php index.html; add_header Strict-Transport-Security "max-age=63072000" always; add_header X-Content-Type-Options "nosniff" always; add_header X-Frame-Options "SAMEORIGIN" always; } 配置完成后,网站所有HTTP请求都将自动跳转到HTTPS安全连接。
本文介绍如何在Nginx和Apache环境下配置HTTPS全站强制跳转,确保网站所有流量都通过加密通道访问。
| 原因 | 说明 |
|---|---|
| 安全性 | 防止中间人攻击 |
| SEO | Google优先收录HTTPS |
| 信任度 | 浏览器显示安全标识 |
| 合规 | PCI DSS等合规要求 |
在 server 配置中添加:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name example.com www.example.com;
ssl_certificate /etc/ssl/certs/example.crt;
ssl_certificate_key /etc/ssl/private/example.key;
# 强制HTTPS
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
root /var/www/html;
index index.php index.html;
}
server {
listen 80;
server_name example.com;
location = / {
return 301 https://$server_name$request_uri;
}
location / {
proxy_pass http://127.0.0.1:8080;
}
}
server {
listen 80;
server_name example.com;
location = / {
return 301 https://$server_name$request_uri;
}
location / {
proxy_pass http://127.0.0.1:8080;
}
}
sudo a2enmod rewrite
sudo systemctl restart apache2
sudo a2enmod rewrite
sudo systemctl restart apache2
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*)$ https://%{SERVER_NAME}/$1 [R=301,L]
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*)$ https://%{SERVER_NAME}/$1 [R=301,L]
</VirtualHost>
HTTP Strict Transport Security强制浏览器使用HTTPS:
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
| 参数 | 说明 |
|---|---|
| max-age | HSTS有效期(秒) |
| includeSubDomains | 包含子域名 |
| preload | 申请预加载列表 |
在Cloudflare控制台设置:
- SSL/TLS → 加密模式:完全灵活或完全严格
- 页面规则:
- URL:
http://*example.com/* - 设置:始终使用HTTPS
- 转发规则:301永久重定向
curl -I http://example.com
curl -I https://example.com
curl -I https://example.com | grep Strict-Transport
curl -I http://example.com
curl -I https://example.com
curl -I https://example.com | grep Strict-Transport
- 打开开发者工具(F12)
- Network标签
- 访问 http://example.com
- 查看Status是否为301,Location是否为https://
| 问题 | 解决方案 |
|---|---|
| 跳转循环 | 检查HTTPS是否也监听80端口 |
| 证书错误 | 更新SSL证书 |
| 资源加载失败 | 内部链接使用协议相对路径 |
| 移动端问题 | 检查重定向状态码是否为301 |
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name example.com www.example.com;
ssl_certificate /etc/ssl/certs/example.crt;
ssl_certificate_key /etc/ssl/private/example.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
root /var/www/html;
index index.php index.html;
add_header Strict-Transport-Security "max-age=63072000" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
}
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name example.com www.example.com;
ssl_certificate /etc/ssl/certs/example.crt;
ssl_certificate_key /etc/ssl/private/example.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
root /var/www/html;
index index.php index.html;
add_header Strict-Transport-Security "max-age=63072000" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
}
配置完成后,网站所有HTTP请求都将自动跳转到HTTPS安全连接。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

评论(0)