美文网首页
Nginx rewrite 实例

Nginx rewrite 实例

作者: Alexander_Zz | 来源:发表于2019-03-19 17:24 被阅读0次

全站http 自动跳转 https

  • 基于通信安全,全站在不影响用户访问的前提下实现 http 自动跳转 https
~]# vim /apps/nginx/conf/conf.d/rewrite.conf
server {
  listen 443 ssl;
  listen 80;
  ssl_certificate  /apps/nginx/certs/www.rookie.com.crt;
  ssl_sertificate_key  /apps/nginx/certs/www.rookie.com.key;
  ssl_session_cache  shared:sslcache:20m;
  ssl_session_timeout 10m;
  server_name www.rookie.com;
  location  / {
    root  /data/nginx/html/web;
    index index.html;
    if ( $scheme = http ) {   # 不写条件判断,会导致来自 https 的访问进入死循环
    rewrite  / https://www.rookie.com permanent;
    }
  }
}

判断文件是否存在

  • 当用户访问一个错误 URL,将用户重定向至首页
location  / {
  root  /data/nginx/html/home;
  index index.html;
  if (! -f $request_filename) {
    #return 404 "error";
    rewrite (.*)  http://www.rookie.com/index.html;
  }
}  

相关文章

网友评论

      本文标题:Nginx rewrite 实例

      本文链接:https://www.haomeiwen.com/subject/cqtnmqtx.html