Nginx rewrite规则实现http跳转到https及301永久重定向

环境准备:

[root@ubuntu1804 ~]#curl https://linux2022.com -Ik
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Sat, 20 Aug 2022 07:26:10 GMT
Content-Type: text/html
Content-Length: 15
Last-Modified: Sat, 20 Aug 2022 04:15:33 GMT
Connection: keep-alive
ETag: "63005fe5-f"
Accept-Ranges: bytes[root@ubuntu1804 ~]#curl https://www.linux2022.com -Ik
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Sat, 20 Aug 2022 07:26:19 GMT
Content-Type: text/html
Content-Length: 11
Last-Modified: Sat, 20 Aug 2022 03:36:17 GMT
Connection: keep-alive
ETag: "630056b1-b"
Accept-Ranges: bytes

1. 实现http跳转到https及301永久重定向

#注意:配置rewrite才能实现http跳转到https
#参数说明:
#一年内实现浏览器自动跳转
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;[root@centos7 ~]#cd /apps/nginx/conf/conf.d/
[root@centos7 conf.d]#ls
mobile.conf  pc.conf  ssl
[root@centos7 conf.d]#vim pc.conf
server{listen 80;listen 443 ssl;ssl_certificate /apps/nginx/conf/conf.d/ssl/www.linux2022.com.crt;ssl_certificate_key /apps/nginx/conf/conf.d/ssl/www.linux2022.com.key;ssl_session_cache shared:sslcache:20m;ssl_session_timeout 10m;add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;server_name www.linux2022.com;location / {root /data/nginx/html/pc;if ( $scheme = http ) {rewrite ^/(.*)$ https://www.linux2022.com/$1 permanent;}}
}[root@centos7 conf.d]#vim mobile.conf
server{listen 80;listen 443 ssl;ssl_certificate /apps/nginx/conf/conf.d/ssl/linux2022.com.pem;ssl_certificate_key /apps/nginx/conf/conf.d/ssl/linux2022.com.key;ssl_session_cache shared:sslcache:20m;ssl_session_timeout 10m;add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;server_name linux2022.com;location / {root /data/nginx/html/mobile;if ( $scheme = http ) {rewrite ^/(.*)$ https://linux2022.com/$1 permanent;}}
}[root@centos7 conf.d]#nginx -t
[root@centos7 conf.d]#nginx -s reload#测试验证
[root@ubuntu1804 ~]#vim /etc/hosts
10.0.0.27 www.linux2022.com linux2022.com
[root@ubuntu1804 ~]#curl -I http://www.linux2022.com
HTTP/1.1 301 Moved Permanently
Server: nginx/1.20.1
Date: Sat, 20 Aug 2022 09:09:39 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: https://www.linux2022.com/
Strict-Transport-Security: max-age=31536000; includeSubDomains[root@ubuntu1804 ~]#curl -I http://linux2022.com
HTTP/1.1 301 Moved Permanently
Server: nginx/1.20.1
Date: Sat, 20 Aug 2022 09:22:13 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: https://linux2022.com/
Strict-Transport-Security: max-age=31536000; includeSubDomains

Nginx rewrite规则实现http跳转到https及301永久重定向相关推荐

  1. nginx rewrite规则和参考

    推荐参考地址: Mailing list ARChives 官方讨论区 http://marc.info/?l=nginx http://edu.codepub.com/2010/0829/25477 ...

  2. Nginx Rewrite规则初探

    Nginx  rewrite(nginx url地址重写) Rewrite 主要的功能就是实现URL的重写,Nginx的Rewrite规则采用Pcre,perl兼容正则表达式的语法规则匹配,如果需要N ...

  3. Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)...

    一.Nginx Rewrite 规则 1. Nginx rewrite规则 Rewrite规则含义就是某个URL重写成特定的URL(类似于Redirect),从某种意义上说为了美观或者对搜索引擎友好, ...

  4. Nginx—— Rewrite规则的使用

    一.使用场景 1.URL访问跳转 (1)页面跳转 (2)兼容性支持(比如新老版本交替时,给老版本一条访问道路) (3)展示效果(比如缩短前台界面的地址栏的url,增强用户体验) 2.SEO优化 谷歌和 ...

  5. Nginx rewrite规则整理

    本日志内容来自互联网和平日使用经验,整理一下方便日后参考. 正则表达式匹配,其中: * ~ 为区分大小写匹配 * ~* 为不区分大小写匹配 * !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 ...

  6. nginx rewrite规则语法

    在新主机的迁移过程中,最大的困难就是WP permalink rewrite的设置. 因为旧主机是用的Apache, 使用的是WP本身就可以更改的.htaccess,没有太大的难度.而这次在VPS上跑 ...

  7. nginx rewrite规则语法(关于break和last分析得很好)

    在新主机的迁移过程中,最大的困难就是WP permalink rewrite的设置. 因为旧主机是用的Apache, 使用的是WP本身就可以更改的.htaccess,没有太大的难度.而这次在VPS上跑 ...

  8. 运维之道 | Nginx rewrite 规则详解

    Nginx rewrite 规则详解 一.rewrite规则概念 rewirte 规则也称为规则重写,主要功能是实现浏览器访问 HTTP URL 的跳转,其正则表达式是基于 Perl 语言.通常而言, ...

  9. nginx Rewrite 规则

    一:nginx  Rewrite 规则 1:rewrite的概念: Nginx Rewrite功能是使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现URL重写以及重定向功能.R ...

最新文章

  1. Android Handler详细使用方法实例
  2. RabbitMq 本地连接报错 org.springframework.amqp.AmqpIOException: java.io.IOException
  3. struts2中拦截器和过滤器的比较
  4. One order event display tool
  5. 公司来了个傻员工,改变了所有聪明的员工
  6. call_user_func_array
  7. 非标准语法;请使用 _使用无线AP如何供电?
  8. vue项目中报常见错误
  9. 互动快报:从读报刊看性格
  10. SUBMAIL 短网址 API 授权与验证机制
  11. 简单的Charles抓包ios微信网页
  12. Windows系统(tips)
  13. 怎么设置计算机语言中文,Windows10 IE11浏览器怎么把默认语言设置为中文
  14. 英语发音规则---L字母
  15. WinRar DOS命令大全带详细参数rar/zip/7z压缩文件解密
  16. 51采集温度电压电流+LCD1602显示
  17. 类的继承关系,多态的体现,我的觉得题目还是有点欠缺
  18. 用Python写中文数字对照表
  19. uni微信小程序引入商圈快速积分插件
  20. Transformer论文学习笔记

热门文章

  1. 电脑匹配,汽车节气门清洗后都要做电脑匹配吗?
  2. CSS 手写加号和减号
  3. ppt2010不支持html,ppt2010演示文稿不能使用怎么解决
  4. SPGridView 研究笔记 Part 1 - 基础和项菜单
  5. ThinkPHP6.0伪静态使用
  6. 【OpenCV】双目摄像头输入及左右图像分割
  7. 佛教观点谈儿童教育问题
  8. Spring与XFire
  9. 2011级同学加分通知(只针对河软高校俱乐部2011级同学)
  10. mysql-proxy做mysql代理连接阿里云服务器