公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

目录
  • 为什么要给nginx配置rewrite?
  • 错误原因
  • 解决方案
  • 重启nginx服务器

为什么要给nginx配置rewrite?

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

nginx配置完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;。

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,我们看到截图中已经成功的重写过去了。但是访问域名的时候默认找的是网站安装时候的目录。并不是项目目录。解决方案如下:

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

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

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

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

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

示例代码如下:

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服务器

1, /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浏览器提示将您重定向的次数过多的解决方法

分享到:
标签:将您 浏览器 解决方法 过多 重定向
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定