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

2022-08-30 19:19:56

我已经尝试搜索这个问题,但它与我的问题完全不同,所以我在这里发布。我正在尝试创建一个Web服务器,用于在子文件夹中托管多个laravel项目。这是我的实验室服务器。所以我想让我的项目像这样:nginx

  • domain.com/project1
  • domain.com/project2
  • domain.com/project3

我正在为每个项目复制以下nginx块(我不知道这里发生了什么,我只是从互联网上复制并且它起作用):location

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;
        fastcgi_param                   SCRIPT_FILENAME "/home/web/project1/public/index.php";
    }

}

location @project1 {
     rewrite /avm/(.*)$ /project1/index.php?/$1 last;
}

在我的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中创建新员工时,它会给我一个错误:(它有点在一些url路由之前)http://domain.com/project1/administracao/funcionariosSQLSTATE[42S22]: Column not found: 1054 Unknown column '/administracao/funcionarios' in

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


答案 1

检查这个Nginx配置,我相信它会帮助你

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

答案 2

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

在Nginx配置中没有时髦的特殊重写规则。没有复制和粘贴项目的各个部分。在路由中未提及子文件夹。只是一个常规的Laravel 5项目整齐地包含在服务器上的某个地方,以及从主站点的文档根目录到其公用文件夹的符号链接。

/var/www/domain.com/public/project1 --> /var/www/project1/public

所有路线都有效!

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

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

但是这样做并不会降低代码的灵活性,因为如果您确实在不通过子文件夹访问站点的环境中运行站点,则它之所以有效,是因为可以使用地址栏包含的内容。asset()


推荐