何在同一域上为Codeigniter和laravel配置NGIN

何在同一域上为Codeigniter和laravel配置NGIN

本文介绍了如何在同一域上为Codeigniter和laravel配置NGINX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to configure nginx for different location on same domain for two php frameworks.example.com/ - codeigniter (root /var/www/html/codeigniter)

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

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

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

here my examples, but they not work.

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;
        }


    }
}

在第一个示例中,可以完美地在路线example.com/*上进行代码编码,但是laravel在路线上不起作用:

In first example perfectly works codeinginter on routes example.com/*,but laravel not work on route:

 /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返回404example.com/api/index.php-laravel返回404

this configuration also not work example.com - codeigniter works perfectly, example.com/api - codeigniter return 404 example.com/api/index.php - laravel return 404

有人可以帮助我找到正确的方法进行配置吗?

Can anyone help me find the right way to configure this?

推荐答案

检查一下:

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

Place your Laravel index file (index.php) inside the directory /public/api

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

Change the the directories in index.php to call the correct bootstrap file

<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <[email protected]>
 */

/*
|--------------------------------------------------------------------------
| 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';

这篇关于如何在同一域上为Codeigniter和laravel配置NGINX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 16:43