【问题标题】:How to configure NGINX for codeigniter and laravel on same domain如何在同一域上为 codeigniter 和 laravel 配置 NGINX
【发布时间】:2017-03-15 17:14:48
【问题描述】:

我需要为两个 php 框架在同一域上的不同位置配置 nginx。 example.com/ - codeigniter(根 /var/www/html/codeigniter)

example.com/api - laravel 5.2 (root /var/www/html/laravel)

这是我的示例,但它们不起作用。

server {

    listen 80;
    server_name example.com www.example.com;
    root /var/www/html/codeigniter;

    index index.php;

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

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }


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

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

        location ~ \.php$ {
            try_files $uri /index.php =500;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_intercept_errors off; 
            fastcgi_buffer_size 16k; 
            fastcgi_buffers 4 16k;                       
        }


    }    
}   

在第一个示例中,codeinginter 在路由 example.com/* 上完美运行, 但 laravel 在路线上不起作用:

 /api - codeigniter return 404 page not found,
 /api/index.php - laravel return 404 because this page not exists,
 /api/user/ -codeigniter also return 404 page

这个配置也不起作用:

server {
        listen 80;

        server_name example.com;

        root /var/www/;

        index index.php index.html index.htm;

        charset utf-8;

        access_log /var/log/nginx/example.com-access.log;
        error_log  /var/log/nginx/examplw.com-error.log error;

        sendfile off;

        client_max_body_size 100m;



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

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

                location ~ \.php$ {
                        try_files $uri /index.php =500;
                        fastcgi_split_path_info ^(.+\.php)(/.+)$;
                        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                        fastcgi_index index.php;
                        include fastcgi_params;
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        fastcgi_intercept_errors off;
                        fastcgi_buffer_size 16k;
                        fastcgi_buffers 4 16k;
                }

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

        location / {
                root /var/www/html/codeigniter;
                index index.php index.html index.htm;

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

                location ~ \.php$ {
                        fastcgi_split_path_info ^(.+\.php)(/.+)$;
                        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                        fastcgi_index index.php;
                        include fastcgi_params;
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        fastcgi_intercept_errors off;
                        fastcgi_buffer_size 16k;
                        fastcgi_buffers 4 16k;
                }

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


}

这个配置也不行 example.com - codeigniter 完美运行, example.com/api - codeigniter 返回 404 example.com/api/index.php - laravel 返回 404

谁能帮我找到正确的配置方法?

【问题讨论】:

    标签: php nginx laravel-5.2 codeigniter-3


    【解决方案1】:

    看看这个:

    server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;
    
        root /var/www/;
        index index.php index.html index.htm;
    
        server_name example.com; 
    
        # CodeIgniter
        location / {
                try_files $uri $uri/ /index.php?$uri&$args;
        }
    
        # deny access to .htaccess files
        location ~ /\.ht {
                deny all;
        }
    
        # Laravel
        location /api {                                                                                                                                                 
                    try_files $uri $uri/ /api/index.php?$query_string;                                                                                                       
            }
    
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
    
    }    
    

    将你的 Laravel 索引文件 (index.php) 放入目录 /public/api

    更改index.php中的目录以调用正确的bootstrap文件

    <?php
    
    /**
     * Laravel - A PHP Framework For Web Artisans
     *
     * @package  Laravel
     * @author   Taylor Otwell <taylorotwell@gmail.com>
     */
    
    /*
    |--------------------------------------------------------------------------
    | Register The Auto Loader
    |--------------------------------------------------------------------------
    |
    | Composer provides a convenient, automatically generated class loader for
    | our application. We just need to utilize it! We'll simply require it
    | into the script here so that we don't have to worry about manual
    | loading any of our classes later on. It feels nice to relax.
    |
    */
    
    require __DIR__.'/../../bootstrap/autoload.php';
    
    /*
    |--------------------------------------------------------------------------
    | Turn On The Lights
    |--------------------------------------------------------------------------
    |
    | We need to illuminate PHP development, so let us turn on the lights.
    | This bootstraps the framework and gets it ready for use, then it
    | will load up this application so that we can run it and send
    | the responses back to the browser and delight our users.
    |
    */
    
    $app = require_once __DIR__.'/../../bootstrap/app.php';
    

    【讨论】:

    • 这个例子有效,但是现在 laravel 公共目录中的所有文件都需要移动到 public/api/ 但这是个坏主意。也许,你知道,如果从 codeigniter 退出,如何配置 nginx 从 laravel 返回文件
    • 其实只是一个文件而已。为什么这么糟糕?
    • 另外,你不能同时公开 Laravel 和 CodeIgniter,因为它们都使用相同的想法,即处理所有请求的单个 index.php 文件。
    • 感谢您的帮助!我使用 /var/www/html/laravel/public/DIR_NAME -> /var/www/html/laravel/public/api/DIR_NAME 的符号链接修复了 laravel 公共文件的问题。现在可以了
    猜你喜欢
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-10
    • 2015-02-13
    • 2016-08-23
    • 2018-08-31
    相关资源
    最近更新 更多