最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】nginx中如何强制所有的80的http都强制转发到443的https

HTTP crifan 428浏览 0评论
折腾:
【未解决】nginx中关于ssl配置的逻辑和常见参数含义
期间,需要去实现把80都强制转发到443:
希望
都转发到https的:
但是不清楚最佳的写法是啥。
参考了很多:
https://www.cnblogs.com/chjbbs/p/5748369.html
server {  
        listen 80;
        listen [::]:80 ssl ipv6only=on;
        server_name     
example.com;
        return 301 
https://example.com$request_uri;
}
https://segmentfault.com/a/1190000004976222
server {
        listen       80;
        server_name  
www.yourdomain.com;
        rewrite ^ https://$http_host$request_uri? permanent;    # force redirect http to https
    #return 301 https://$http_host$request_uri;
    }
https://juejin.im/post/5b88b58xxx2db3bedf7
server {
    listen       80;
    server_name  
www.baidu.com;
 //域名
    rewrite ^(.*)$ https://${server_name}$1 permanent; 
}
nginx 80 redirect 443
Redirect all HTTP requests to HTTPS with Nginx
Redirect HTTP to HTTPS in Nginx | Servers for Hackers
Best way to configure Nginx SSL + force HTTP to redirect to HTTPS + force www to non-www on Serverpilot free plan (by using Nginx configuration file only) | DigitalOcean
NGINX – Redirect to HTTPS | DigitalOcean
Disable HTTPS redirect in NGINX | DigitalOcean
nginx http redirect https
redirect – In Nginx, how can I rewrite all http requests to https while maintaining sub-domain? – Server Fault
server {
    listen         80;
    return 301 https://$host$request_uri;
}
和:
server {
       listen         80;
       server_name    my.domain.com;
       return         301 https://$server_name$request_uri;
}

server {
       listen         443 ssl;
       server_name    my.domain.com;
       # add Strict-Transport-Security to prevent man in the middle attacks
       add_header Strict-Transport-Security "max-age=31536000" always; 

       [....]
}
Pitfalls and Common Mistakes | NGINX
意思是:
此处用rewrite不好
不好的:
rewrite ^/(.*)$ http://example.com/$1 permanent;
还可以的:
rewrite ^ http://example.com$request_uri? permanent;
更加合适的:
return 301 http://example.com$request_uri;
redirect – In Nginx, how can I rewrite all http requests to https while maintaining sub-domain? – Server Fault
server {
    listen         [::]:80;
    return 301 https://$host$request_uri;
}
https://www.digitalocean.com/community/questions/nginx-redirect-to-https
server {
    listen 80;
    listen [::]:80; #Added IPv6 here too
    server_name mysite.com;
    #We remove any location-blocks from here, since this server-block just redirects everything
    return 301 http://www.$server_name$request_uri; #We use a variable to have less hardcoding
}
暂时不考虑复杂的ipv6了:[::]:80
因为还要去研究:bindv6only是true还是false
Nginx – Redirect HTTP to HTTPS
server {
       listen         80;
       server_name    www.servercertificates.com;
       return         301 https://$server_name$request_uri;
}
【总结】
综合来说,用:
server {
    listen         80;
    return 301 https://$host$request_uri;
}
可以涵盖子域名的301强制跳转。
且:
  • 废弃不好的rewrite的写法:

转载请注明:在路上 » 【已解决】nginx中如何强制所有的80的http都强制转发到443的https

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.177 seconds, using 20.66MB memory