php - 安装 nginx 后找不到 Laravel 路由

标签 php apache laravel nginx fastcgi

在我将 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 - link

最佳答案

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

server {
    listen   80 default_server;

    root /var/www/laravel/public/;
    index index.php index.html index.htm;

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

    # pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
    location ~ \.php$ {
            try_files $uri /index.php =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }
}

编辑: 而不是:

fastcgi_pass unix:/var/run/php5-fpm.sock;

截至 2018 年 11 月,随着 PHP 7.2 的发布,它将是:

fastcgi_pass unix:/var/run/php7.2-fpm.sock;

关于php - 安装 nginx 后找不到 Laravel 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35634530/

相关文章:

django - 如何在ubuntu上为django restful服务配置apache2

php - 在本地开发 Heroku PHP Facebook 应用程序时出现 500 错误

javascript - 具有局部 View 的 Laravel 4 ajax

php - 使用 php mysql 和 ajax 刷新 Div 中特定单元格中的数据

PHP整数问题

PHP:从文件中的某个点读取

php - jQuery - <span> 返回 'undefined' 的 html

Apache 缓存问题

laravel - 将 laravel 项目从 VSTS 发布到 Azure 到正确的文件夹

php - 基于lng和lat实现半正矢距离查询