saber酱的抱枕

Fly me to the moon

09/17
14:18
软件

Nginx 配置 http 跳转到 https

当用户用 http 网址访问网站时,我希望可以自动跳转到 https。在 Nginx 里,可以在该网站的配置文件里用 301 跳转到 https。目前测试了两种方法都有效。

第一种方法,把 80 和 443 端口的监听分开,然后在 80 里直接 301 跳转:

server {
	listen  80;
	server_name pixiv.download;
	return 301 https://$server_name$request_uri;
}

server {
	listen 443 ssl;
	server_name pixiv.download;
	#其余内容省略
}


第二种方法用起来应该更舒服一些,不用把 80 和 443 分来,直接做一个判断来跳转:

server {
	listen  80;
	listen 443 ssl;
	server_name pixiv.download;
	if ($scheme = http ) {
	    return 301 https://$host$request_uri;
	}
	#其余内容省略
}

Nginx 配置 http 跳转到 https

评论 locationiskey 撤销评论