使用 Nginx 设置 Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8856664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:20:37  来源:igfitidea点击:

Setting up Laravel with Nginx

phpnginxlaravel

提问by Kyle Decot

I'm attempting to set up the LaravelPHP Framework to work with Nginx. Here is my directory structure:

我正在尝试设置LaravelPHP 框架以与Nginx 一起使用。这是我的目录结构:

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

Basically what I have is a standard Laraveldownload w/ a legacyfolder thrown in which holds all of the files from my non-MVC project.

基本上,我拥有的是一个标准的Laravel下载legacy文件,其中包含一个文件夹,其中包含我的非 MVC 项目中的所有文件。

I need Nginx to first check if the requested page/file exists inside of legacy, if it does then I want to use that. Otherwise, I want to fall back to Laravel's index.phpfile which is located in project/public/.

我需要 Nginx 首先检查请求的页面/文件是否存在于 legacy 中,如果存在,那么我想使用它。否则,我想回到index.php位于project/public/.

I'm no expert when it comes to Nginx configurations so any help that you can provide would be most appreciated.

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

回答by RoboTamer

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 ^/(.+)/$ / permanent;
    }

    # enforce NO www
    if ($host ~* ^www\.(.*))
    {
        set $host_without_www ;
        rewrite ^/(.*)$ $scheme://$host_without_www/ 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/?$ / permanent;
    }

    # unless the request is for a valid file (image, js, css, etc.), send to bootstrap
    if (!-e $request_filename)
    {
        rewrite ^/(.*)$ /index.php?/ 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;
}