![](/img/trans.png)
[英]Allow multiple subdomain in laravel without making subdomain as route variable?
[英]Install multiple laravel projects in subfolders without subdomain
我已經嘗試過搜索這個問題,但它與我的不同,所以我在這里發布。 我正在嘗試使用nginx
創建一個Web服務器來托管子文件夾中的多個laravel項目。 這是我的實驗室服務器。 所以我想讓我的項目像這樣:
我正在為每個項目復制以下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 http://domain.com/project1/administracao/funcionarios
創建一個新員工時,它給出了錯誤: SQLSTATE[42S22]: Column not found: 1054 Unknown column '/administracao/funcionarios' in
(它有點預先添加一些網址)
當我設置像project1.domain.com
這樣的子域時,一切正常。 但我不想為每個項目創建一個子域,我希望它在子文件夾url中工作。 可能嗎?
我已經使用簡單的符號鏈接在另一個站點的“子文件夾”中成功運行了Laravel 5.4項目。
Nginx配置中沒有任何時髦的特殊重寫規則。 不復制和粘貼項目的各個部分。 沒有提到路線中的子文件夾。 只是一個常規的Laravel 5項目整齊地包含在服務器上的某個地方,並且從主站點的文檔根目錄到它的公共文件夾的符號鏈接。
/var/www/domain.com/public/project1 --> /var/www/project1/public
所有路線都有效!
在編寫視圖時,您必須在asset()
輔助函數中包裝客戶端資產的路徑,這樣HTML中的路徑將包含子文件夾,瀏覽器可以找到它們。
<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
但這樣做並不會使您的代碼變得不那么靈活,因為如果您在不通過子文件夾訪問它的環境中運行該站點,它就可以工作,因為asset()
與地址欄包含的內容一起使用。
我認為問題可能在你的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。 考慮更改<action type="Rewrite" url="project1/index.php" />
並查看它返回的內容。 在最后一個例子中, var_dump
模型並查找它的來源。
有一個簡單的解決方案“我希望它在子文件夾URL中工作。是否可能?”。
腳步
1.在主域公用文件夾中創建文件夾。 你需要創建3個文件夾
家用/網絡/公共/ PROJECT1
家用/網絡/公共/項目2
家用/網絡/公共/項目3
2.在每個項目文件夾中,您需要粘貼laravel應用程序的公用文件夾的內容
(來自您的Laravel項目)project1 / public / {contents} - 將其復制到 - >(托管服務器)home / web / public / project1 / {contents}
將laravel項目的其余部分上傳到公共根目錄之外,並提供對該文件夾的寫入權限。
現在打開(托管服務器)public / project1 / index.php更新這兩個字段
require __DIR __。'/ .. / .. / PROJECTONE / bootstrap / autoload.php';
$ app = require_once __DIR __。'/ .. / .. / PROJECTONE / bootstrap / app.php';
嘗試這樣的事情....我正在使用以下.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;
}
}
結果是我不必在/sites/customerdemo.example.com/文件夾中執行除git clone
的任何操作,並運行composer install
和php artisan migrate
來添加新項目。
實際上,我已經開發了一個控制面板,我可以點擊“添加項目”並指定項目詳細信息和一個新的bitbucket repo,mysql用戶和數據庫將被創建,customerdemo服務器將被賦予對這個bitbucket repo和webhook的部署訪問權限在這個新的repo中設置,每當有人提交掌握這個repo時會調用customerdemo服務器上的部署腳本,這將觸發git clone或git pull和composer install以及數據庫遷移。 這就是我需要動態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;
}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.