【问题标题】:Laravel route redirect to 404 with nginxLaravel 使用 nginx 将路由重定向到 404
【发布时间】:2020-10-29 20:45:11
【问题描述】:

我的虚拟机上有一个 laravel 应用程序,我用 nginx 设置服务器,但每次我调用路由时,它都不起作用并重定向到 404 未找到

这是我的 nginx 配置:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    index index.html index.htm index.nginx-debian.html index.php;

    server_name _;

    location / {
                #deny all;
                try_files $uri $uri/ /index.php$is_args$args;
        }

    location /crm-api {
        root /var/www/crm-api/public; #
        rewrite ^/crm-api/(.*)$ /$1 break; #

        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /index.php$is_args$args;
    }


    location ~ /\. {
        deny all;
    }

    location ~ \.php$ {
        set $newurl $request_uri; #
                if ($newurl ~ ^/crm-api(.*)$) { #
                        set $newurl $1; #
                        root /var/www/crm-api/public; #
                } #
        try_files $uri=404 /index.php=404;
        #fastcgi_split_path_info ^(.+\.php)(/.+)$;

        fastcgi_param REQUEST_URI $newurl; #
        fastcgi_intercept_errors on;
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params; 
    }
}

我创建了 /crm-api 因为我想在我的虚拟机上运行多个应用程序,我尝试了几个解决方案,但仍然无法正常工作。

【问题讨论】:

  • 声明 try_files $uri=404 /index.php=404; 看起来不正确。它应该做什么?

标签: laravel nginx


【解决方案1】:

试试这个代码块而不是你的位置代码块

location / {
    # Redirect everything that isn't a real file to index.php
    try_files $uri $uri/ /index.php?$query_string;
}

希望对你有帮助。

【讨论】:

    【解决方案2】:

    这是我的配置,它可以工作

    location / {
     # First attempt to serve request as file, then
     # as directory, then fall back to displaying a 404.
         try_files $uri $uri/ /index.php?$query_string;
    }
    

    【讨论】:

    • 这个答案并没有从 2019 年的答案中添加任何内容。如果它也适用于您,请投票支持原始答案。
    猜你喜欢
    • 2018-09-26
    • 1970-01-01
    • 2019-06-24
    • 2022-01-26
    • 2015-07-30
    • 2021-12-23
    • 2020-10-27
    • 2021-11-17
    • 2016-02-28
    相关资源
    最近更新 更多