http默认端口是80,https默认端口是443,server配置listen只能监听一个端口,要么是80,要么是443,此时就需要进行跳转
return跳转
# 此server可以写在nginx.conf文件中,include不用动。
server {
access_log off; # 由于这段配置是来做跳转的,先关闭log记录。
listen 80;
server_name www.wzx.com;
location / {
return 302 https://www.wzx.com$request_uri; #当访问此网址的80端口时,自动跳转443端口。$request_uri 就是www.wzx.com后面不管加什么内容,都直接跳转https,不写的话,如果后面加上uri,那么通过http访问时还会跳到首页。302 是设置重定向状态码,也可不加。
}
}
server {
access_log off;
listen 80;
server_name wzx.com; # 不输入www也跳转https
location / {
return 302 https://www.wzx.com$request_uri;
}
}
server {
access_log off;
listen 443 ssl;
server_name www.wzx.com;
location / {
root /web;
index index.html index.htm;
}
}
将这两个server写进配置文件,那么无论访问的时http还是htttps,都会跳转到https