翼度科技»论坛 云主机 服务器技术 查看内容

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

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
为什么要给nginx配置rewrite?

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


错误原因

在配置网站站点的时候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;。
  1. server {
  2.     listen       80;
  3.     server_name www.shidongyun.com shidongyun.com;
  4.     #charset koi8-r;
  5.     #access_log  logs/host.access.log  main;
  6.     root    "/data/wwwroot/shidong";
  7.     location / {
  8.         rewrite ^/(.*) http://www.shidongyun.com/$1 permanent;
  9.         index  index.html index.htm index.php l.php;
  10.         try_files $uri $uri/ /index.php?$query_string;
  11.        autoindex  off;
  12.     }
复制代码
解决方案

1,把service下面的service_name 做正确的修改,删除www.shidongyun.com这个要重写的域名。
  1. server {
  2.     listen       80;
  3.     server_name shidongyun.com;
  4.     #charset koi8-r;
  5.     #access_log  logs/host.access.log  main;
  6.     root    "/data/wwwroot/shidong";
  7.     location / {
  8.         rewrite ^/(.*) http://www.shidongyun.com/$1 permanent;
  9.         index  index.html index.htm index.php l.php;
  10.         try_files $uri $uri/ /index.php?$query_string;
  11.        autoindex  off;
  12.     }
复制代码
在次在浏览器访问:shidongyun.com,我们看到截图中已经成功的重写过去了。但是访问域名的时候默认找的是网站安装时候的目录。并不是项目目录。解决方案如下:

2,需要配置rewrite重定向到指定的目录或者单独配置一个service虚拟机,然后把需要rewrite重定向的service主机跟域名配置好。配置信息如下:
我们先配置一个service虚拟机,要访问的域名,比如“shidongyun.com”,然后在配置一个service虚拟机,把要rewrite重写的域名放进去,比如:“www.shidong.com”,我们达到的效果就是访问“shidongyun.com”浏览器地址会自动跳转到“www.shidongyun,com”下面。


示例代码如下:
  1. server {
  2.     listen       80;
  3.     server_name shidongyun.com;
  4.     #charset koi8-r;
  5.     #access_log  logs/host.access.log  main;
  6.     root    "/data/wwwroot/shidong";
  7.     location / {
  8.         index  index.html index.htm index.php l.php;
  9.         rewrite ^/(.*) http://www.shidongyun.com/$1 permanent;
  10.         try_files $uri $uri/ /index.php?$query_string;
  11.        autoindex  off;
  12.     }
  13.     省略多余的部分.....只需要看rewrite跟service_name即可
  14. }
  15. server {
  16.     listen       80;
  17.     server_name www.shidongyun.com;
  18.     #charset koi8-r;
  19.     #access_log  logs/host.access.log  main;
  20.     root    "/data/wwwroot/shidong";
  21.     location / {
  22.         index  index.html index.htm index.php l.php;
  23.         try_files $uri $uri/ /index.php?$query_string;
  24.        autoindex  off;
  25.     }
  26.         省略多余的部分.....只需要看service_name即可。root设置项目路径。
  27.     }
复制代码
重启nginx服务器

1, /etc/init.d/nginx restart
  1. [root@iZm5e8nyz28v9zr7lhb7moZ ~]# /etc/init.d/nginx restart
  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  4. Stopping nginx:                                            [  OK  ]
  5. Starting nginx:                                            [  OK  ]
  6. [root@iZm5e8nyz28v9zr7lhb7moZ ~]#
复制代码
2,浏览器输入“shidongyun.com”自动跳转到“www.shidongyun.com”下面

到此这篇关于nginx配置完rewrite浏览器提示将您重定向的次数过多的解决方法的文章就介绍到这了,更多相关nginx rewrite重定向内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源:https://www.jb51.net/server/2916814z2.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具