使用Nginx配置Laravel

21

我正在尝试设置Laravel PHP框架,使其能够与Nginx一起使用。这是我的目录结构:

/project
   /application
   /laravel
   /public
      index.php
   /legacy
      /index.php
      /stylesheets
         default.css

我的情况是使用标准的Laravel下载,同时有一个名为legacy的文件夹,其中存储了非MVC项目的所有文件。

我需要让Nginx首先检查是否存在位于legacy中的请求页面/文件,如果存在,则使用该文件。否则,我希望回退到位于project/public/中的Laravel的index.php文件。

对于Nginx配置方面,我不是专家,所以非常感谢您提供的任何帮助。


这不是Laravel,但这个教程应该能指导你朝正确的方向前进:https://nealpoole.com/blog/2011/04/setting-up-php-fastcgi-and-nginx-dont-trust-the-tutorials-check-your-configuration/ - mikelbring
1个回答

24
server {
    server_name .laravel.dev;
    root /home/tamer/code/laravel/public;

    index index.php index.html;

    #browse folders if no index file
        autoindex on; 

    # serve static files directly
    location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
        access_log off;
        expires max;
    }

    # removes trailing slashes (prevents SEO duplicate content issues)
    if (!-d $request_filename)
    {
        rewrite ^/(.+)/$ /$1 permanent;
    }

    # enforce NO www
    if ($host ~* ^www\.(.*))
    {
        set $host_without_www $1;
        rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
    }


    # canonicalize codeigniter url end points
    # if your default controller is something other than "welcome" you should change the following
    if ($request_uri ~* ^(/lobby(/index)?|/index(.php)?)/?$)
    {
        rewrite ^(.*)$ / permanent;
    }

    # removes trailing "index" from all controllers
    if ($request_uri ~* index/?$)
    {
        rewrite ^/(.*)/index/?$ /$1 permanent;
    }

    # unless the request is for a valid file (image, js, css, etc.), send to bootstrap
    if (!-e $request_filename)
    {
        rewrite ^/(.*)$ /index.php?/$1 last;
        break;
    }

    # catch all
    error_page 404 /index.php;

        location ~ \.php$ {
        try_files $uri =404;
                fastcgi_pass  unix:/tmp/php.socket;
                fastcgi_index index.php;
                #include fastcgi_params;
                include /home/tamer/code/nginx/fastcgi_params;
        }
        access_log /home/tamer/code/laravel/storage/logs.access.log;
        error_log  /home/tamer/code/laravel/storage/logs.error.log;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接