2025年3月27日 星期四 甲辰(龙)年 月廿六 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 服务器 > Nginx

nginx 重定向 rewrite 规则

时间:07-03来源:作者:点击数:31

1.rewrite配置

  • server {
  • listen 80 default_server;
  • server_name jeson.t.imooc.io;
  • access_log /var/log/nginx/log/host.access.log main;
  • root /opt/app/code;
  • location ~ ^/break {
  • rewrite ^/break /test/ break; # break 重定向到/test 不会重新发送请求
  • }
  • location ~ ^/last {
  • rewrite ^/last /test/ last; # last 重定向到/test 会重新发送/test请求
  • }
  • location ~ ^/imooc {
  • rewrite ^/imooc http://www.imooc.com/ permanent; # 永久重定向 下次访问不会请求 nginx 直接访问镜像地址
  • #rewrite ^/imooc http://www.imooc.com/ redirect; # 临时重定向 每次访问都会请求一次 nginx
  • }
  • location / {
  • rewrite ^/course-(\d+)-(\d+)-(\d+)\.html$ /course/$1/$2/course_$3.html break;
  • if ($http_user_agent ~* Chrome) { # 判断浏览器是否是chrome 浏览器
  • rewrite ^/nginx http://coding.imooc.com/class/121.html redirect;
  • }
  • if (!-f $request_filename) { # 判断请求的文件的路径是发存在 ! 非 取反的意思 -f 判断路径用的
  • rewrite ^/(.*)$ http://www.baidu.com/$1 redirect;
  • }
  • index index.html index.htm;
  • }
  • location /test/ {
  • default_type application/json;
  • return 200 '{"status":"success"}';
  • }
  • }

2.rewrite优先级

  • server > location
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门