首页 > 解决方案 > 在 nginx 中获取别名重定向规则的 500 内部错误

问题描述

我是 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;
    }
}

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

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

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

我在这里做错了什么。

标签: nginxurl-rewriting

解决方案


答案归功于@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语句使永无止境的循环。


推荐阅读