【问题标题】:Getting 500 internal error on redirect rule of alias in nginxnginx 中别名的重定向规则出现 500 内部错误
【发布时间】:2019-10-04 18:01:01
【问题描述】:

我是 nginx 服务器的新手。我陷入了 URL 重定向。我在默认文件中有以下几行。

server {
    listen 80;
    root /home/ubuntu/web/server/current/public;
    index index.php index.html index.htm index.nginx-debian.html;
    server_name _;

    error_log /home/ubuntu/web/error.log info;


   location / {
    # try_files $uri $uri/ /index.php?$query_string;
    # try_files $uri $uri/ =404;
   }

   rewrite ^/web/(.*) /web/;
     location /web/ {
     alias /home/ubuntu/web/client/web/;
     # try_files $uri $uri/ index.html;
   }

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }

    location ~ /\.ht {
            deny all;
    }
}

我对上述重写规则的期望是 - 所有像 - http://example.com/web/loginhttp://exmpale.com/web/dashboard 这样的 URL 都将被重定向到 /home/ubuntu/web/client/web/ 并且要访问的默认页面是 index.html 文件。

当我打开错误日志文件时,我发现了类似的错误 -

重写或内部重定向循环,同时内部重定向到“/web/index.php”,客户端:ip_address,服务器:_,请求:“GET /web/ HTTP/1.1”,主机:“ipaddress”

我在这里做错了什么。

【问题讨论】:

  • 显然默认页面是index.php 而不是index.html,因为这是您的index 语句的顺序。但是您的 rewrite 语句导致了循环。您可以尝试重写为/web/index.html 以绕过index 顺序和循环。
  • @RichardSmith 我试过index index.html index.htm index.php。不幸的是没有工作。谢谢回复,您还有什么疑问。

标签: nginx url-rewriting


【解决方案1】:

答案归功于@RichardSmith,他提供了正确方向的可能错误。我发现我在 nginx 重写规则中的错误。

rewrite ^/web/(.*) /web/;
location /web/ {
    alias /home/ubuntu/web/client/web/;
    # try_files $uri $uri/ index.html;
} 

我应该有以下内容,而不是上面的-

rewrite ^/web/(.*) web/;
location web/ {
    alias /home/ubuntu/web/client/web/;
    # try_files $uri $uri/ index.html;
} 

只要/ 放置在 web 之前,服务器就会将路径视为绝对路径。因此rewrite 语句尝试重定向到绝对路径中不存在的后备文件。最终,rewrite 语句使循环永无止境。

【讨论】:

    猜你喜欢
    • 2016-06-15
    • 2015-05-18
    • 2014-01-11
    • 2015-06-21
    • 2017-10-01
    • 1970-01-01
    • 2021-09-28
    • 2015-12-22
    • 2011-08-24
    相关资源
    最近更新 更多