1987WEB视界-分享互联网热点话题和事件

您现在的位置是:首页 > 域名 > 正文

域名

单域名服务器下部署多个二级目录的laravel应用

1987web2022-09-03域名628
一个服务器不能配置多个子域名的时候很是尴尬,无法使用虚拟目录,只能通过二级目录访问。在根目录/data/wwwroot/default部署了多个laravel应用,laravel1,-laravel2

一个服务器不能配置多个子域名的时候很是尴尬,无法使用虚拟目录,只能通过二级目录访问。在根目录/data/wwwroot/default部署了多个laravel应用,laravel1,-laravel2...,然后按文档配置所谓的优雅链接,遇到了诸多问题:

  • apache服务器:XAMPP的lamp环境
  • nginx服务器:OneinStack自动部署lnmp环境

Apache服务器

在lamp环境下使用还是蛮简单,配置Laravel/public/.htaccess:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

即可,访问的地址:

http://test.comlaravel1/public/admin/index

http://test.com/laravel2/public/admin/index

nginx服务器

文档中说,只要在nginx.conf下配置:

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

即可,访问优雅链接,但结果有点失望,你会遇到问题:

  1. Access denied.
  2. No input file specified.
  3. 504
  4. ...

归根结底是nginx.conf配置问题,本人遇到最多就是Access denied,因为使用OneinStack自动部署lnmp环境,nginx.conf已经默认配置了,结果仔细对比,少了下面配置

fastcgi_split_path_info ^(.+\.php)(.*)$;

参考:https://segmentfault.com/a/1190000002667095

难怪laravel优雅链接index.php后面一直无法访问,目前为题已解决,附上一段网上的配置:

server {
 listen 80; 
	server_name _; 
	set $root_path /data/www/default; 
	root $root_path; 
 
	index index.php index.html index.htm; 
 
	try_files $uri $uri/ @rewrite; 
 
	location @rewrite { 
		rewrite ^/(.*)$ /index.php?_url=/$1; 
	} 
 
	location ~ \.php { 
 
		fastcgi_pass 127.0.0.1:9000; 
		fastcgi_index /index.php; 
 
		fastcgi_split_path_info ^(.+\.php)(/.+)$; 
		fastcgi_param PATH_INFO $fastcgi_path_info; 
		fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; 
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
		include fastcgi_params;
	} 
 
	location ~* ^/(css|img|js|flv|swf|download)/(.+)$ { 
		root $root_path; 
	} 
 
	location ~ /\.ht { 
		deny all; 
	} 
}

http://test.com/laravel1/public/index.php/admin/index

http://test.com/laravel2/public/index.php/admin/index

都可以正常访问了,OK!

最后隐藏index.php

虽然上述配置在laravel中可以正常访问,但是一些放在public里面的静态资源可能无法访问,比如无法使用asset()函数时,路径中就包含index.php导致资源无法加载,所以建议隐藏index.php

if (!-e $request_filename) {
 rewrite ^/(.*)/public/(.*)$ /$1/public/index.php/$2 last;
 break;
}

http://test.com/laravel1/public/admin/index

http://test.com/laravel2/public/admin/index

以上就无需加index.php,即可正常访问