安装 nginx 后找不到 Laravel 路由

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

在我将 ICG 更改为 nginx 之后,除了索引页面之外的所有路由都不起作用。

Laravel 配置:

#/etc/nginx/sites-enabled/laravel
server {
    listen 80;

    root /var/www/home;
    index index.php;

    server_name 192.168.178.71;

    access_log /var/www/home/storage/app/logs/laravel-nginx-access.log;
    error_log  /var/www/home/storage/app/logs/laravel-nginx-error.log error;


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

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


    # ERROR
    error_page 404 /index.php;


    # DENY HTACCESS
    location ~ /\.ht {
        deny all;
    }
}

默认配置:

# /etc/nginx/sites-enabled/default
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm;

        server_name 192.168.178.71 localhost;

        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;

        autoindex on;

         # Remove trailing slash to please routing system.
          if (!-d $request_filename) {
            rewrite     ^/(.+)/$ /$1 permanent;
        }


        }

            location ~ \.php$ {
        #try_files $uri /index.php =404;
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME document_root$fastcgi_script_name;

        }

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

我的 nginx 配置

#/etc/nginx/nginx.conf
user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {
         disable_symlinks off;
        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
       ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";



        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

我尝试了什么:

/var/www/home# (home folder is laravel folder)
sudo chown -R www-data:www-data *


/var/www/home# 
sudo chown -R root:root *

我也试着改变

 try_files $uri $uri/ /index.php?$query_string;
 try_files $uri $uri/ /index.php$is_args$args;
 try_files $uri $uri/ /index.php;


php artisan cache:clear  

我读过的大多数谷歌问题,但没有任何帮助。

我的 phpinfo - 链接

php apache laravel nginx fastcgi
9个回答
72
投票

这是 Laravel 和 Nginx 的正确基本配置:

server {
    listen 443 ssl default_server;

    root /var/www/laravel/public/;
    index index.php;
    ssl_certificate /path/to/cert;
    ssl_certificate_key /path/to/key;

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

    # pass the PHP scripts to FastCGI server listening on /var/run/php-fpm.sock
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_split_path_info ^(.+?\.php)(/.+)$;
        include fastcgi_params;
    }
}

16
投票

当我通过 get 发送参数时我不认识它们,我只需要激活以下内容: try_files $uri $uri/ /index.php$is_args$args;

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

5
投票

更新几行后我遇到了同样的问题 nginx 工作正常..

适用于 Windows(根据您的文件系统更改根目录)

1.root html/laravel; #Update Here - 在html之后添加项目文件夹名称

2.try_files $uri $uri/ /index.php$is_args$args; #Update Here - 为 404 not found error 添加这个

server {
    listen 80; # IPv4
    server_name localhost;

    ## Parametrization using hostname of access and log filenames.
    access_log logs/localhost_access.log;
    error_log logs/localhost_error.log;

    ## Root and index files.
    root html/laravel; #Update Here - add project folder name after html
    index  index.php index.html index.htm;

    ## If no favicon exists return a 204 (no content error).
    location = /favicon.ico {
        try_files $uri =204;
        log_not_found off;
        access_log off;
    }

    ## Don't log robots.txt requests.
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
    ## Try the requested URI as files before handling it to PHP.
    location / {
try_files $uri $uri/ /index.php$is_args$args; #Update Here - Add this for 404 not found error
        ## Regular PHP processing.
        location ~ \.php$ {
            try_files  $uri =404;
            fastcgi_pass   php_processes;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        ## Static files
        location ~* \.(?:css|gif|htc|ico|js|jpe?g|png|swf)$ {
            expires max;
            log_not_found off;
            ## No need to bleed constant updates. Send the all shebang in one
            ## fell swoop.
            tcp_nodelay off;
            ## Set the OS file cache.
            open_file_cache max=1000 inactive=120s;
            open_file_cache_valid 45s;
            open_file_cache_min_uses 2;
            open_file_cache_errors off;
        }

        ## Keep a tab on the 'big' static files.
        location ~* ^.+\.(?:ogg|pdf|pptx?)$ {
            expires 30d;
            ## No need to bleed constant updates. Send the all shebang in one
            ## fell swoop.
            tcp_nodelay off;
        }
        } # / location

} 

4
投票

我遇到了同样的问题,但更新默认配置后就可以了。

location @rewrite {
        rewrite ^/(.*)$ /index.php?_url=/$1;
    }
    location / {
            try_files $uri $uri/ @rewrite;
    }  

如果这对你有用,请告诉我。

sudo service nginx restart
更改配置后。


1
投票

试试吧,为我工作。

sudo nano /etc/nginx/sites-enabled/default

然后 sudo systemctl reload nginx

    server {
    listen 80;
    server_name _ midominioexample.com www.midominioexample.com;
    root /var/www/html/public;

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

    index 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.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

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

0
投票

在我的 Ubuntu 16.04 上,我也遇到了同样的错误,即 Routes not working on Nginx

为了解决路由问题,我尝试了以下代码,它对我来说工作正常。

使用以下命令打开项目配置文件

sudo nano /etc/nginx/sites-available/projectname 

然后在这个文件中做如下修改

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

       root /var/www/project_name/public;

       server_name server_name;

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

}

重要的是改变位置块中的

try_files

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

0
投票

我发现这解决了我的 Laravel 路由问题。 我将 location ~ .php$ 嵌套在 location /.

例子:

server{
    listen 9000;

    server_name _;

    root /var/www/myapp/public;

    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;

        location ~ \.php$ {

            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;

        }
    }
}

0
投票

代替

location / {
    try_files $uri $uri/ =404;
}

下面为我工作。

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

它指定任何匹配位置 / 的请求都应该传递给 try_files 指令。 try_files 指令尝试提供请求的文件,如果它不存在,它将请求传递给参数列表中的下一个回退。

在这种情况下,第一个回退是 $uri,它尝试将请求的 URI 作为文件提供服务,第二个回退是 $uri/,它尝试将请求的 URI 作为目录提供服务。如果这些文件或目录都不存在,则请求最终传递给 /index.php,并附加任何查询字符串。


-1
投票

截至 03/2022当前版本的 Laragon (5.0.0) 我花了很多时间弄清楚为什么我无法打开除我在

web.php
中配置的 index.php 之外的任何链接对于
route:list
.

因为我只是想关闭 SSL 并重新打开它。 这似乎会导致重置所有配置文件。

Laragon 似乎默认添加了这些行:

# Access Restrictions
allow       127.0.0.1;
deny        all;

我在

deny
前面放了一个 # 来取消注释,它再次起作用,像这样:

# Access Restrictions
allow        127.0.0.1;
#deny        all;
© www.soinside.com 2019 - 2024. All rights reserved.