nginx配置完rewrite浏览器提示将您重定向的次数过多

为什么要给nginx配置rewrite?

因为公司要求访问 shidongyun.com的时候浏览器会自动跳转到www.shidong.com下面,专业术语叫“301跳转”百度了一番,nginx配置规则,用rewrite还有return进行重写301跳转。我这里用的是rewrite。

错误原因

1,在配置网站站点的时候service里面的service_name 规则不正确。错误配置规则如下:

只看service这部分错误的即可。service_name 不能把rewrite即将要重写的域名写进去,这样就造成了死循环了。比如:我要访问"shidongyun.com",利用rewrite在浏览器输入“shidongyun.com”的时候,重写到www.shidongyun.com下面。那么在service_name就不能写www.shidongyun.com这个域名。可以单独写一个service,也可以不用写。直接这样写:rewrite ^/(.*) http://www.shidongyun.com/$1 permanent;。

server {listen       80;server_name www.shidongyun.com shidongyun.com;#charset koi8-r;#access_log  logs/host.access.log  main;root    "/data/wwwroot/shidong";location / {rewrite ^/(.*) http://www.shidongyun.com/$1 permanent;index  index.html index.htm index.php l.php;try_files $uri $uri/ /index.php?$query_string;autoindex  off;}

解决方案

1,把service下面的service_name 做正确的修改,删除www.shidongyun.com这个要重写的域名。

server {listen       80;server_name shidongyun.com;#charset koi8-r;#access_log  logs/host.access.log  main;root    "/data/wwwroot/shidong";location / {rewrite ^/(.*) http://www.shidongyun.com/$1 permanent;index  index.html index.htm index.php l.php;try_files $uri $uri/ /index.php?$query_string;autoindex  off;}

在次在浏览器访问:shidongyun.com,我们看到截图中已经成功的重写过去了。但是访问域名的时候默认找的是网站安装时候的目录。并不是项目目录。解决方案如下:

1,需要配置rewrite重定向到指定的目录或者单独配置一个service虚拟机,然后把需要rewrite重定向的service主机跟域名配置好。配置信息如下:

我们先配置一个service虚拟机,要访问的域名,比如“shidongyun.com”,然后在配置一个service虚拟机,把要rewrite重写的域名放进去,比如:“www.shidong.com”,我们达到的效果就是访问“shidongyun.com”浏览器地址会自动跳转到“www.shidongyun,com”下面。


示例代码如下:

server {listen       80;server_name shidongyun.com;#charset koi8-r;#access_log  logs/host.access.log  main;root    "/data/wwwroot/shidong";location / {index  index.html index.htm index.php l.php;rewrite ^/(.*) http://www.shidongyun.com/$1 permanent;try_files $uri $uri/ /index.php?$query_string;autoindex  off;}省略多余的部分.....只需要看rewrite跟service_name即可
}
server {listen       80;server_name www.shidongyun.com;#charset koi8-r;#access_log  logs/host.access.log  main;root    "/data/wwwroot/shidong";location / {index  index.html index.htm index.php l.php;try_files $uri $uri/ /index.php?$query_string;autoindex  off;}省略多余的部分.....只需要看service_name即可。root设置项目路径。}
重启nginx服务器

/etc/init.d/nginx restart

[root@iZm5e8nyz28v9zr7lhb7moZ ~]# /etc/init.d/nginx restart
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]
[root@iZm5e8nyz28v9zr7lhb7moZ ~]#

2,浏览器输入“shidongyun.com”自动跳转到“www.shidongyun.com”下面

nginx配置完rewrite浏览器提示将您重定向的次数过多相关推荐

  1. nginx配置反向代理浏览器输入地址后点击登陆URL发生变化的处理

    今天项目上需要再用NGINX配置反向代理需求如下: 现在有A服务器只做为nginx服务器,B服务器作为代码服务器,用户访问A要实际上访问的是B的程序. 处理方式就是用nginx的方向代理处理即可: 打 ...

  2. nginx配置转发时,提示no Route matched with those values

    近期,在使用nginx配置转发链接时,出现提示:no Route matched with those values.服务器这边用的时KONG网关. 问题原因: 配置带kong 服务的转发不能带 ho ...

  3. iMindMap思维导图V2020.12提示许可证激活使用的次数过多解决教程

    iMindMap是一款十分受欢迎的思维导图软件,随着12版本的上线,iMindMap新增了很多新用户,最近小编发现有不少新用户在群里反映:"为什么购买iMindMap时说可以支持换机,但是在 ...

  4. nginx配置以及rewrite规则

    1 location正则写法 语法规则: location [=||*|^~] /uri/ { - } = 开头表示精确匹配 ^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可.ng ...

  5. vue.config.js配置完代理还是提示跨域?

    哥,配置了代理之后,请求的url是向代理服务器发请求.请求的url的host和端口改一下,配置的时候如果设置了pathRewrite,也改改url.

  6. 【Nginx】将您重定向的次数过多

    如果项目根路径不为空,需要多做一层映射,防止前后端来回重定向: server{listen 80;server_name pmp.mussessein.cn;location / {root html ...

  7. nginx配置rewrite的用法详解

    文章目录 rewrite在if中的用法 rewrite中break和last的用法 1.break和last在location{}外部时 2.break和last在location{}内部时 3.br ...

  8. 必学必会的nginx配置location匹配顺序总结

    location 匹配规则: location 路径正则匹配: 符号 说明 ~ 正则匹配,区分大小写 ~* 正则匹配,不区分大小写 ^~ 普通字符匹配,如果该选项匹配,则,只匹配该选项,不再向下匹配其 ...

  9. nginx配置location匹配顺序总结

    location匹配顺序 "="前缀指令匹配,如果匹配成功,则停止其他匹配 普通字符串指令匹配,顺序是从长到短,匹配成功的location如果使用^~,则停止其他匹配(正则匹配) ...

最新文章

  1. 论文阅读:FFDNet:Toward a Fast and Flexible Solution for CNN based Image Denoising
  2. 阿里淘系程序员“开源”内部年度技术总结,还把P9大佬喊出来教你“打怪升级”...
  3. python第三十课--异常(raise关键字)
  4. unity-tweak-tool不能打开的解决办法
  5. 区块链BaaS云服务(22)趣链BitXHub跨链平台
  6. [北航软工]第一次团队作业
  7. oracle hr样本模式,Oracle 样本模式 HR
  8. C#中线程间操作无效: 从不是创建控件 txtBOX 的线程访问它
  9. Google 拼音会导致卡 Ctrl 键?
  10. [C++11]initializer_lisr模板类的使用
  11. Linux网站访问的电脑占CPU,详解Linux如何查看当前占用CPU或内存最多的几个进程...
  12. Go Concurrency Patterns: Timing out, moving on
  13. react-router中进行路由控制
  14. SaaSpace:12种最好的免费甘特图软件工具
  15. RHEL常用Linux命令操作 第四章实验报告
  16. 银行业保险业数字化转型指导意见下发,IT 基础架构团队如何应对挑战?
  17. 供应链管理 MOOC学习笔记(全)
  18. The server encountered an unexpected condition that prevented it from fulfilling the request.(解决思路)
  19. 《毒液2》上线,豆瓣评分6.1-附繁体中字预告片
  20. 《C++ Primer 第5版》-11.1使用关联容器-康奈尔笔记

热门文章

  1. 【报告分享】2021年中国汽车行业营销研究报告-艾瑞咨询(附下载)
  2. mysql主从复制1062_主从复制1062错误的解决方法
  3. Mac M1:zsh command not found ll
  4. Node.contains() 报错, vue中监听点击事件,除了点击自己,点击其他地方将自身隐藏
  5. 分享69个ASP.NET企业网站源码,总有一款适合您
  6. 老臣隐退,雷军提拔新人,但尚未有真正脱颖而出的将帅
  7. docker启动container
  8. 电商项目介绍---说的很好
  9. JAVA电影网站计算机毕业设计Mybatis+系统+数据库+调试部署
  10. Sublime+Xelatex+latextools配置参考文献的诸多问题