【问题标题】:nginx configuration for Laravel 4Laravel 4 的 nginx 配置
【发布时间】:2014-02-01 04:56:13
【问题描述】:

我正在尝试使用 nginx 设置我的 Laravel 4 项目。这是我用于 laravel 的 nginx 服务器块:

server {
        listen 80;

        root /home/prism/www/laravel/public;
        index index.php index.html index.htm;

        server_name example.com;

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

        }
               location ~ \.php$ {

                try_files $uri =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;
        }

但我的问题是,除了默认安装的默认路由之外,所有其他路由都显示“404 not found”错误。

【问题讨论】:

    标签: nginx laravel laravel-4


    【解决方案1】:

    你可以试试这个位置 / { ... }

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

    $query_string 对我有用。

    【讨论】:

    • 我在位置/工作下设置这个:try_files $uri $uri/ /index.php?$args;
    【解决方案2】:

    这是我在 Laravel 4 和 Laravel 4.1 中使用的 NGINX 配置。

    server {
    
        listen  80;
        server_name sub.domain.com;
        set $root_path '/var/www/html/application_name/public';
        root $root_path;
    
        index index.php index.html index.htm;
    
        try_files $uri $uri/ @rewrite;
    
        location @rewrite {
            rewrite ^/(.*)$ /index.php?_url=/$1;
        }
    
        location ~ \.php {
    
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index /index.php;
    
            include /etc/nginx/fastcgi_params;
    
            fastcgi_split_path_info       ^(.+\.php)(/.+)$;
            fastcgi_param PATH_INFO       $fastcgi_path_info;
            fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    
        location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
            root $root_path;
        }
    
        location ~ /\.ht {
            deny all;
        }
    
    }
    

    【讨论】:

    • 这对我有用,但是我无法使用您的所有配置设置。我所做的只是使用 Nginx 的当前默认值并将 location / {} 中的 try_files 更改为您所拥有的,然后添加 @rewrite 位置。现在一切正常。
    • 没有。 wiki.nginx.org/Pitfalls#Taxing_Rewrites@RobGordijn 有一个更好的解决方案。
    猜你喜欢
    • 2016-09-24
    • 2017-07-25
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 2015-01-05
    • 2018-09-25
    相关资源
    最近更新 更多