Laravel [NGINX] 404 NOT FOUND 在子目录或图像上,即使它们存在

问题描述 投票:0回答:2

注意: 这个项目在我的本地主机上运行良好。

我在我的服务器上安装了这个 Laravel 项目,并使用 Nginx 作为网络服务器。它安装得很好,因为我按照说明进行操作,但是当我上传图像来替换当前图像时,例如:头像、徽标、网站图标;它们已上传但未显示。图像字段显示损坏的图像。 浏览器控制台显示“无法加载资源:服务器响应状态为 404(未找到)”,当我尝试预览提供的链接后的图像时,它显示“404 | NOT FOUND”,即使它存在,这就是正确的网址。 我已经做了我所知道的所有事情,比如清除 Laravel 的路由、配置和缓存。为了确定起见,我尝试将文件夹的权限设置为 777。我已经按照应该设置的方式设置了所有必要的权限,但仍然无法工作。我什至不得不安装 Apache Web 服务器,因为我认为 Nginx 出现问题只是为了得到同样的错误。我不得不回到 Nginx。 我无法访问我自己创建的子目录(例如“/main/”)或使用正确的 URL 查看上传的图像。

  • 我关闭了NGINX:systemctl stop nginx
  • 我安装了Apache并启动了服务;将项目移动到一个新目录,更新虚拟主机记录(因为我使用了“public”)。同样的问题
  • 我已经为项目目录下的所有文件和目录设置了文件和目录权限:644和755;还是同样的错误。
  • 我确保主项目目录属于www-data;所有这些都是用 chgrp, chown 完成的。同样的问题

项目:solut.website.com

user@localhost:/var/www$ ls -l
total 12
drwxr-xr-x 16 www-data www-data 4096 Apr  8 15:26 solut.website.com

项目根目录和文件

user@localhost:/var/www$ ls -la solut.website.com/
total 83748
drwxr-xr-x 16 www-data www-data     4096 Apr  8 15:26 .
drwxr-xr-x  5 root     root         4096 Apr  8 09:11 ..
drwxr-xr-x 12 www-data www-data     4096 Dec 23  2022 app
-rw-r--r--  1 www-data www-data     1686 Dec 23  2022 artisan
drwxr-xr-x  3 www-data www-data     4096 Dec 23  2022 bootstrap
-rw-r--r--  1 www-data www-data     2727 Apr 20  2023 composer.json
-rw-r--r--  1 www-data www-data   411030 Apr 20  2023 composer.lock
drwxr-xr-x  2 www-data www-data     4096 Apr  3  2023 config
drwxr-xr-x  5 www-data www-data     4096 Dec 23  2022 database
-rw-r--r--  1 ample    www-data      258 Dec 23  2022 .editorconfig
-rw-r--r--  1 ample    www-data      589 Mar 28 09:09 .env
-rw-r--r--  1 ample    www-data      590 Mar 28 08:15 .env_backup
-rw-r--r--  1 ample    www-data      912 Dec 23  2022 .env.example
drwxr-xr-x  2 www-data www-data     4096 May 28  2023 erpgo
-rw-r--r--  1 www-data www-data 84783392 May 28  2023 erpgo.zip
-rw-r--r--  1 ample    www-data      152 Dec 23  2022 .gitattributes
-rw-r--r--  1 ample    www-data       16 Feb 23  2023 .gitignore
-rw-r--r--  1 ample    www-data      931 Dec 23  2022 .htaccess
drwxr-xr-x  3 ample    www-data     4096 Apr 28  2023 .idea
-rw-r--r--  1 www-data www-data      563 Mar  9  2023 index.php
drwxr-xr-x  3 www-data www-data     4096 Dec 23  2022 lang
drwxr-xr-x  2 www-data www-data     4096 May 28  2023 main
-rw-r--r--  1 www-data www-data      682 Apr 26  2023 package.json
-rw-r--r--  1 www-data www-data   406860 Apr 26  2023 package-lock.json
-rw-r--r--  1 www-data www-data      717 May 27  2023 phptest.php
-rw-r--r--  1 www-data www-data     1175 Dec 23  2022 phpunit.xml
drwxr-xr-x  8 www-data www-data     4096 Jun  5  2023 public
-rw-r--r--  1 www-data www-data     3958 Dec 23  2022 README.md
drwxr-xr-x  6 www-data www-data     4096 Feb 17  2023 resources
drwxr-xr-x  2 www-data www-data     4096 Mar 28 17:12 routes
drwxrwxrwx 19 www-data www-data     4096 May 28  2023 storage
-rw-r--r--  1 ample    www-data      162 Dec 23  2022 .styleci.yml
-rw-r--r--  1 www-data www-data      487 Dec 23  2022 tailwind.config.js
drwxr-xr-x  4 www-data www-data     4096 Dec 23  2022 tests
drwxr-xr-x 69 www-data www-data     4096 Apr 20  2023 vendor
-rw-r--r--  1 www-data www-data     5046 Apr 28  2023 verification.php
-rw-r--r--  1 www-data www-data      596 Dec 23  2022 webpack.mix.js

项目.htaccess 文件

user@localhost:/var/www$ cat solut.website.com/.htaccess
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.jpeg|\.gif|robots\.txt|\.ico|\.woff|\.woff2|.ttf|\.svg)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(css|assets|landing|storage|installer|js)/(.*)$ public/$1/$2 [L,NC]
</IfModule>

## NGINX Virtual Host
user@localhost:/var/www$ cat /etc/nginx/sites-available/solut.website.com.conf
server {
        # Server
        server_name             solut.website.com; # managed by Certbot

        # Root directory
        root                    /var/www/solut.website.com/public;

        # Laravel Deployment: laravel.com/docs/10.x/deployment#nginx
        # LD 1
        add_header              X-Frame-Options         "SAMEORIGIN";
        add_header              X-Content-Type-Options  "nosniff";

        # Index page
        index                   index.php index.html index.htm; #was previously DISABLED

        # LD 2
        charset                 utf-8;

        location / {
                try_files       $uri $uri/ /index.php?$query_string; #because of LARAVEL
        }

        # LD 3
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt { access_log off; log_not_found off; }

        # LD 4
        error_page              404                     /index.php;

        # Setup PHP
        location ~ \.php$ {
                fastcgi_pass    unix:/var/run/php/php8.1-fpm.sock;
                include         fastcgi_params;
                fastcgi_param   SCRIPT_FILENAME         $document_root$fastcgi_script_name;
                fastcgi_param   SCRIPT_NAME             $fastcgi_script_name;
        }

        # LD 6
        location ~ /\.(?!well-known).* {
                deny all;
        }

        # Disable .htaccess from being served
        location ~ /\.ht {
                deny all;
        }

        # Serving static files
        location ~* \.(css|js|png|jpg|jpeg|gif|ico|woff|woff2|ttf|svg)$ {
                expires         max;
                log_not_found   off;
                access_log      off;
        }

        # Serving public directory
        location ~* ^/public/(css|assets|landing|storage|installer|js)/(.*)$ {
                rewrite         ^/public/(css|assets|landing|storage|installer|js)/(.*)$        /public/$1/$2   break;
        }

        #location ~ /* {
                #rewrite                ^/(.*)$         /index.php?_url=/$1;
        #}


        listen [::]:443 ssl ipv6only=on; # managed by Certbot
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/solut.website.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/solut.website.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
        if ($host = solut.website.com) {
                return 301 https://$host$request_uri;
        } # managed by Certbot


        listen                  80                      ;
        listen                  [::]:80                 ;
        server_name             solut.website.com;
        return                  404; # managed by Certbot
}

日志中除了“404”之外没有任何帮助

php laravel ubuntu nginx
2个回答
0
投票

它说“404 | 未找到”

注意竖线,它将“it”标识为 Laravel。

只有你使用 Nginx 来运行 Laravel,这是 Laravel 现在处理的路由,而不是 Web 服务器的路由。

即使它存在并且这是正确的 URL。

请注意,这里的“它”是一个文件,并且“是正确的 URL”与您的 Laravel 无关,您想知道路由是否存在(而不是文件)以及正确的路由是否正在使用(而不是仅正确的 URL)。

TLDR:听起来像是配置问题。销毁服务器并配置新服务器,然后部署应用程序。

它通常可以工作,即使不能,您也已将配置错误限制在部署例程和服务器配置中。然后继续进行故障排除,因为您现在已经成功地使问题重现。


0
投票

尝试

php artisan storage:link
并确保文件夹
public/storage
存在。另请确保所有图像都放置在文件夹
public
中,并且您使用
asset()
在模板中访问它们。

此外,在

.env
中确保您正确定义了 APP_URL。如果您在端口 8000 上使用
php artisan server
,则需要设置
APP_URL=http://localhost:8000

© www.soinside.com 2019 - 2024. All rights reserved.