【问题标题】:All routes returning 404 except '/'所有返回 404 的路由,除了 '/'
【发布时间】:2019-10-28 21:28:17
【问题描述】:

我已经成功地在运行在 Arch linux 上的 nginx 服务器上安装了一个 laravel 应用程序。但是,当我尝试在 Web 浏览器中查看应用程序时,所有路由都返回 404,但默认的“/”除外。我知道这一点是因为我有 make:auth 但我无法访问注册和登录页面。

我按照通过 Google 找到的说明创建了两个文件夹,sites-availablesites enabled in /etc/nginx/。然后,我在可用的站点中为 /etc/nginx/sites-available 中的 laravel 应用程序创建了一个名为 niko 的配置文件,并像 ln --symbolic /etc/nginx/sites-available/niko /etc/nginx/sites-enabled/niko 那样符号链接到它。

这是我的配置文件/etc/nginx/sites-available/niko的内容。

server {
    listen       80;
    server_name  niko;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   /usr/share/nginx/html/niko/public;
        index  index.html index.htm index.php;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
            root /usr/share/nginx/html/niko/public;
            include fastcgi.conf;
    }
    }

在我的/etc/nginx/nginx.conf 中,我在http {} 块的结束} 上方有这一行:
include /etc/nginx/sites-enabled/*; 当我检查 nginx 错误日志 (/var/log/nginx/error.log) 时,我发现以下条目:

2019/06/14 10:59:22 [error] 5429#5429: *3 directory index of "/usr/share/nginx/html/niko/" is forbidden, client: 192.168.1.101, server>
2019/06/14 10:59:36 [error] 5429#5429: *3 open() "/usr/share/nginx/html/niko/public/login" failed (2: No such file or directory), clie>
2019/06/14 10:59:36 [error] 5429#5429: *3 open() "/usr/share/nginx/html/niko/public/login" failed (2: No such file or directory), clie>
2019/06/14 10:59:38 [error] 5429#5429: *3 open() "/usr/share/nginx/html/niko/public/login" failed (2: No such file or directory), clie>
2019/06/14 10:59:38 [error] 5429#5429: *3 open() "/usr/share/nginx/html/niko/public/login" failed (2: No such file or directory), clie>
2019/06/14 11:26:53 [error] 5429#5429: *7 open() "/usr/share/nginx/html/niko/public/login" failed (2: No such file or directory), clie>
2019/06/14 11:26:53 [error] 5429#5429: *7 open() "/usr/share/nginx/html/niko/public/login" failed (2: No such file or directory), clie>
2019/06/14 11:27:01 [error] 5429#5429: *7 open() "/usr/share/nginx/html/niko/index.php/login" failed (2: No such file or directory), c>
2019/06/14 11:27:01 [error] 5429#5429: *7 open() "/usr/share/nginx/html/niko/index.php/login" failed (2: No such file or directory), c>
2019/06/14 11:27:04 [error] 5429#5429: *7 open() "/usr/share/nginx/html/niko/index.php/login" failed (2: No such file or directory), c>
2019/06/14 11:27:04 [error] 5429#5429: *7 open() "/usr/share/nginx/html/niko/index.php/login" failed (2: No such file or directory), 

我已经拉了几个小时的头发,因为我不知道去哪里检查。我尝试了几件事,但似乎没有任何效果。感谢您的支持。

更新: 我的新/etc/nginx/sites-available/niko 在这里

server {
    listen 80;
    server_name niko;
    root /usr/share/nginx/html/niko/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

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

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

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

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

更新 1namei -l /usr/share/nginx/html/niko/ 给出:

f: /usr/share/nginx/html/niko/
drwxr-xr-x root root /
drwxr-xr-x root root usr
drwxr-xr-x root root share
drwxr-xr-x root root nginx
drwxr-sr-x kali root html
drwxr-sr-x kali root niko

【问题讨论】:

    标签: laravel nginx laravel-5


    【解决方案1】:

    Laravel 要求您稍微更改 nginx.conf 文件以使其正常工作。

    查看Laravel Documentation 中提供的这个示例配置文件。

    server {
        listen 80;
        server_name example.com;
        root /example.com/public;
    
        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Content-Type-Options "nosniff";
    
        index index.html index.htm index.php;
    
        charset utf-8;
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }
    
        error_page 404 /index.php;
    
        location ~ \.php$ {
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            include fastcgi_params;
        }
    
        location ~ /\.(?!well-known).* {
            deny all;
        }
    }
    

    【讨论】:

    • 当我使用这个时,我现在得到 403 禁止。
    • 您是否从 nginx 日志中获得任何有用的信息?
    • 您是完全复制了这个示例配置文件还是根据自己的需要对其进行了调整?
    • 最新日志为2019/06/14 12:18:27 [error] 5509#5509: *3 directory index of "/usr/share/nginx/html/niko/" is forbidden,
    • 我编辑了根指令以指向我的应用程序公用文件夹。然后重启 nginx。
    猜你喜欢
    • 2020-08-26
    • 2014-07-31
    • 2018-08-20
    • 2017-11-15
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    相关资源
    最近更新 更多