Nginx 的跳转有三种写法,即 rewrite、return、error_page
server {
listen 80;
server_name domain.com;
rewrite ^(.*) https://$server_name$1 permanent;
}
server {
listen 443 ssl;
server_name domain.com;
ssl on;
# other
}
server {
listen 80;
server_name domain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name domain.com;
ssl on;
# other
}
非常规手段,即利用 497 状态码。
当此虚拟机只允许 HTTPS 来访问时,用 HTTP 访问会让 Nginx 报 497 错误,然后利用 error_page 将链接重定向至 HTTPS 上,即
server {
listen 443 ssl;
listen 80;
server_name domain.com;
ssl on;
# other
error_page 497 https://$server_name$request_uri;
}
当然 497 一般用于非标准端口上,譬如 9443 端口默认使用使用 ssl
server {
listen 9443 ssl;
server_name domain.com;
# other
error_page 497 https://$server_name$server_port$request_uri;
}