Php 在没有子域的子文件夹中安装多个laravel项目

Php 在没有子域的子文件夹中安装多个laravel项目,php,.htaccess,laravel,nginx,Php,.htaccess,Laravel,Nginx,我已经试着搜索这个问题,但它和我的完全不同,所以我在这里发布这个。我正在尝试使用nginx创建一个Web服务器,以在子文件夹中承载多个laravel项目。这是我的实验室服务器。所以我希望我的项目是这样的: /* |-------------------------------------------------------------------------- | Application Routes |----------------------------------------------

我已经试着搜索这个问题,但它和我的完全不同,所以我在这里发布这个。我正在尝试使用
nginx
创建一个Web服务器,以在子文件夹中承载多个laravel项目。这是我的实验室服务器。所以我希望我的项目是这样的:

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('/', ['middleware' => 'auth','uses' => 'HomeController@index'])->name('home');

// Authentication
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@authenticate');
Route::get('auth/logout', 'Auth\AuthController@getLogout');

// Administração
Route::group(['prefix' => 'administracao', 'middleware' => 'auth'], function() {
    Route::resource('filiais', 'FiliaisController');
    Route::resource('precos', 'PrecosController');
    Route::resource('funcionarios', 'FuncionariosController');
    Route::resource('cargos', 'CargosController');
    Route::resource('vendedores', 'VendedoresController');
});

// Comercial
Route::group(['prefix' => 'comercial', 'middleware' => 'auth'], function() {
    Route::resource('clientes', 'ClientesController');
    Route::resource('fichas', 'FichasController');
});

// Operacional
Route::group(['prefix' => 'operacional', 'middleware' => 'auth'], function() {
    Route::resource('agenda', 'AgendaController');
    Route::resource('os', 'OsController');
    Route::resource('ambientes', 'AmbientesController');
    Route::resource('processos', 'ProcessosController');
    Route::get('relatorios', 'RelatoriosController@index');

    Route::group(['prefix' => 'processo', 'middleware' => 'auth'], function() {
        Route::get('create', 'ProcessoController@create');
        Route::get('index', 'ProcessoController@index');

        Route::post('{os}/parse', 'ProcessoController@parse');

        Route::get('{os}', 'ProcessoController@principal');
        Route::match(['get', 'post'], '{os}/detalhe', 'ProcessoController@detalhe');
        Route::get('{os}/duplicidades', 'ProcessoController@duplicidades');
        Route::get('{os}/restantes', 'ProcessoController@restantes');
        Route::match(['get', 'post'], '{os}/auditoria', 'ProcessoController@auditoria');
        Route::match(['get', 'post'], '{os}/operadores', 'ProcessoController@operadores');
        Route::match(['get', 'post'], '{os}/divergencia', 'ProcessoController@divergencia');
        Route::match(['get', 'post'], '{os}/finalizar', 'ProcessoController@finalizar');
        Route::get('{os}/excluir/{setor}', 'ProcessoController@destroy');
    });
});
  • domain.com/project1
  • domain.com/project2
  • domain.com/project3
我正在为每个项目复制以下nginx
location
block(我不知道这里发生了什么,我只是从互联网上复制的,它成功了):

我的laravel应用程序中的RESTful路线如下:
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('/', ['middleware' => 'auth','uses' => 'HomeController@index'])->name('home');

// Authentication
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@authenticate');
Route::get('auth/logout', 'Auth\AuthController@getLogout');

// Administração
Route::group(['prefix' => 'administracao', 'middleware' => 'auth'], function() {
    Route::resource('filiais', 'FiliaisController');
    Route::resource('precos', 'PrecosController');
    Route::resource('funcionarios', 'FuncionariosController');
    Route::resource('cargos', 'CargosController');
    Route::resource('vendedores', 'VendedoresController');
});

// Comercial
Route::group(['prefix' => 'comercial', 'middleware' => 'auth'], function() {
    Route::resource('clientes', 'ClientesController');
    Route::resource('fichas', 'FichasController');
});

// Operacional
Route::group(['prefix' => 'operacional', 'middleware' => 'auth'], function() {
    Route::resource('agenda', 'AgendaController');
    Route::resource('os', 'OsController');
    Route::resource('ambientes', 'AmbientesController');
    Route::resource('processos', 'ProcessosController');
    Route::get('relatorios', 'RelatoriosController@index');

    Route::group(['prefix' => 'processo', 'middleware' => 'auth'], function() {
        Route::get('create', 'ProcessoController@create');
        Route::get('index', 'ProcessoController@index');

        Route::post('{os}/parse', 'ProcessoController@parse');

        Route::get('{os}', 'ProcessoController@principal');
        Route::match(['get', 'post'], '{os}/detalhe', 'ProcessoController@detalhe');
        Route::get('{os}/duplicidades', 'ProcessoController@duplicidades');
        Route::get('{os}/restantes', 'ProcessoController@restantes');
        Route::match(['get', 'post'], '{os}/auditoria', 'ProcessoController@auditoria');
        Route::match(['get', 'post'], '{os}/operadores', 'ProcessoController@operadores');
        Route::match(['get', 'post'], '{os}/divergencia', 'ProcessoController@divergencia');
        Route::match(['get', 'post'], '{os}/finalizar', 'ProcessoController@finalizar');
        Route::get('{os}/excluir/{setor}', 'ProcessoController@destroy');
    });
});
虽然当它进入业务逻辑(保存到数据库等)时,它似乎可以工作(页面显示等),但它似乎有许多错误。例如,当我尝试在url
http://domain.com/project1/administracao/funcionarios
它给了我一个错误:
SQLSTATE[42S22]:找不到列:1054未知列“/administracao/funcionarios”(这有点像是在一些url路由前加上的)


当我设置一个子域,如
project1.domain.com
时,一切正常。但我不想为每个项目创建子域,我希望它在子文件夹url中工作。可能吗?

我想问题可能出在您的
nginx.conf
文件中。试试这个:

location ^~ /project1 {
        alias /home/web/project1/public;
        try_files $uri $uri/ @project1;

    location ~ \.php {
        fastcgi_pass     unix:/var/run/php5-fpm.sock;
        fastcgi_index    index.php;
        include          /etc/nginx/fastcgi_params;
    }

}

location @project1 {
    rewrite /project1/(.*)$ /project1/index.php?/$1 last;
}
另外,请确保
/home/web/project1/
位于您的web根目录之外

也就是说,不建议在子文件夹中运行Laravel。在子域中容易得多


我从中得到了这个建议的基本想法。

不完全确定解决方案,但我认为您应该尝试以下方法:

  • 首先:在
    config/app.php
    文件中正确设置
    url
  • 第二:查看
    public/web.config
    文件。问题可能是这些配置覆盖了您的nginx。考虑更改<代码> <代码>并查看它返回的内容。

在最后一个例子中,
var\u转储模型并查找它的来源。

您尝试过这种配置吗


我将在明天测试,只要我有时间在我的环境中复制您的情况。

有一个简单的解决方案“我希望它在子文件夹url中工作。可能吗?”

台阶
1.将主域中的文件夹设为公用文件夹。 您需要创建3个文件夹
主页/网站/公共/项目1
主页/网站/公共/项目2
主页/网站/公共/项目3

2.在每个项目文件夹中,您需要粘贴laravel应用程序公用文件夹的内容

(从您的Laravel项目)project1/public/{contents}——将此复制到-->(托管服务器)home/web/public/project1/{contents}

  • 将laravel项目的其余部分上载到公共根目录之外,并授予对该文件夹的写访问权限

  • 现在打开(托管服务器)public/project1/index.php 更新这两个字段

  • 需要uuu DIR uuu.'/../PROJECTONE/bootstrap/autoload.php'


    $app=require_once uuu DIR uuu.'/../PROJECTONE/bootstrap/app.php'

    通过使用简单的符号链接,我成功地在另一个站点的“子文件夹”中运行了一个Laravel 5.4项目

    在Nginx配置中没有时髦的特殊重写规则。不复制和粘贴项目的各个部分。未提及路由中的子文件夹。只有一个普通的Laravel5项目整齐地包含在服务器上的某个地方,以及一个从主站点的文档根指向它的公共文件夹的符号链接

    /var/www/domain.com/public/project1 --> /var/www/project1/public
    
    所有的路线都正常

    在编写视图时,必须将客户端资源的路径包装在
    asset()
    helper函数中,以便HTML中的路径包含子文件夹,浏览器可以找到它们

    <!-- Styles -->
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
    
    
    

    但这样做并不会降低代码的灵活性,因为如果您在不通过子文件夹访问站点的环境中运行站点,它会工作,因为
    asset()
    与地址栏所包含的内容一起工作

    试试这样的。。。。我正在为我的服务器使用以下
    .conf

    server {
        listen  80;
        root /vagrant;
        index index.html index.htm index.php app.php app_dev.php;
    
        server_name 192.168.33.10.xip.io;
    
        access_log /var/log/nginx/vagrant.com-access.log;
        error_log  /var/log/nginx/vagrant.com-error.log error;
    
        charset utf-8;
    
        location ~project(\d*)/((.*)\.(?:css|cur|js|jpg|jpeg|gif|htc|ico|png|html|xml))$ {
            rewrite project(\d*)/((.*)\.(?:css|cur|js|jpg|jpeg|gif|htc|ico|png|html|xml))$ /project$1/public/$2 break;
        }
    
        location /project1{
             rewrite ^/project1/(.*)$ /project1/public/index.php?$1 last;
        }
    
         location /project2 {
            rewrite ^/project2/(.*)$ /project2/public/index.php?$1 last;
        }
    
        location = /favicon.ico { log_not_found off; access_log off; }
        location = /robots.txt  { access_log off; log_not_found off; }
    
        error_page 404 /index.php;
    
        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            set $laravel_uri $request_uri;
            if ($laravel_uri ~ project(\d*)(/?.*)$) {
                set $laravel_uri $2;
            }
            fastcgi_param REQUEST_URI $laravel_uri;
            fastcgi_param LARA_ENV local; # Environment variable for Laravel
            fastcgi_param HTTPS off;
        }
        location ~ /\.ht {
            deny all;
        }
    }
    

    最近我也有同样的问题。我想要

    但我讨厌每次添加新项目时都要修改nginx conf

    以下是我的想法:

    # Capture $project from /$projectname/controller/action
    map $request_uri $project {
    
        ~^/(?<captured_project>[a-zA-Z0-9_-]+)/? $captured_project;
        default / ;
    }
    
    server {
    
        listen 11.22.33.44:80;
    
        server_name customerdemo.example.com www.customerdemo.example.com;
    
        # Use $project/public as root
        root /sites/customerdemo.example.com/$project/public;
    
        # Use index.php as directory index
        index index.php;
    
        # Include the basic h5bp config set (see https://github.com/h5bp/server-configs-nginx)
        include h5bp/basic.conf;
    
        # Process /projectname/the/rest/of/the/url
        location ~ ^/([^/]+)/(.*) {
    
            # Save the rest of the URL after project name as $request_url
            set $request_url /$2;
    
    
            # If the saved url refers to a file in public folder (a static file), serve it,
            # else redirect to index.php, passing along any ?var=val URL parameters
            try_files $request_url /index.php?$is_args$args;
    
        }
    
        # Process any URL containing .php (we arrive here through previous location block)
        # If you don't need to serve any other PHP files besides index.php, use location /index.php here
        # instead, to prevent possible execution of user uploaded PHP code
        location ~ [^/]\.php(/|$) {
    
            # Define $fastcgi_script_name and $fastcgi_path_info
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    
            # Immediately return 404 when script file does not exist
            if (!-f $document_root$fastcgi_script_name) {
                return 404;
            }
    
            # Mitigate https://httpoxy.org/ vulnerabilities
            fastcgi_param HTTP_PROXY "";
    
            # Define PHP backend location (find yours by grepping "listen ="
            # from your PHP config folder, e.g. grep -r "listen =" /etc/php/)
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    
            # Set SCRIPT_FILENAME to execute
            fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    
            # Include the default fastcgi parameters
            include fastcgi_params;
    
            # Overwrite REQUEST_URI (default is $request_uri) with $request_url we saved earlier
            fastcgi_param  REQUEST_URI        $request_url;
        }
    
    }
    
    #从/$projectname/controller/action捕获$project
    映射$request\u uri$项目{
    ~^/(?[a-zA-Z0-9.-]+)/?$U项目;
    违约/;
    }
    服务器{
    听11.22.33.44:80;
    服务器名称customerdemo.example.com www.customerdemo.example.com;
    #使用$project/public作为根目录
    root/sites/customerdemo.example.com/$project/public;
    #使用index.php作为目录索引
    index.php;
    #包括基本h5bp配置集(请参阅https://github.com/h5bp/server-configs-nginx)
    包括h5bp/basic.conf;
    #进程/projectname/the/rest/of/the/url
    位置~^/([^/]+)/(.*)){
    #将项目名称后的URL其余部分另存为$request\u URL
    设置$request_url/$2;
    #如果保存的url引用公用文件夹中的文件(静态文件),请提供该文件,
    #else重定向到index.php,传递任何?var=val URL参数
    try_files$request_url/index.php?$is_args$args;
    }
    #处理任何包含.php的URL(我们通过前面的位置块到达这里)
    #如果您不需要提供index.PHP以外的任何其他PHP文件,请在此处使用location/index.PHP
    #相反,为了防止可能执行用户上传的PHP代码
    位置~[^/]\.php(/|$){
    #定义$fastcgi\u脚本\u名称和$fastcgi\u路径\u信息
    fastcgi_split_path_info^(+?\.php)(/.*);
    #当脚本文件不存在时,立即返回404
    if(!-f$document\u root$fastcgi\u script\u name){
    返回404;
    }
    #减轻https://httpoxy.org/ 弱点
    fastcgi_参数HTTP_代理“”;
    #
    
    server {
    server_name main-app.dev;
    root /var/www/projects/main/public;
    
    
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";
    
    index index.html index.htm index.php;
    charset utf-8;
    # sub_directory
    location ^~ /sub-app {
      alias /var/www/projects/sub/public;
      try_files $uri $uri/ @sub;
    
        location ~ \.php {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_read_timeout 30000;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME /var/www/projects/sub/public/index.php;
        }
        access_log off;
        error_log  /var/www/projects/sub/storage/log/error.log error;
    }
    
    location @sub {
       rewrite /sub/(.*)$ /sub/index.php?/$1 last;
    } # end sub_directory
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    
    access_log off;
    error_log  /var/www/projects/main/storage/log/error.log error;
    
    error_page 404 /index.php;
    
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_read_timeout 30000;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    
    location ~ /\.(?!well-known).* {
        deny all;
    }}