Nginx:在Linux中使用相同的url但两个不同的子位置提供多个Laravel应用程序
我想在一台nginx服务器上提供多个Laravel应用程序,第一个在Nginx:在Linux中使用相同的url但两个不同的子位置提供多个Laravel应用程序,linux,laravel,nginx,server,Linux,Laravel,Nginx,Server,我想在一台nginx服务器上提供多个Laravel应用程序,第一个在/var/www/html/app1中有根目录,第二个在/var/www/html/app2中,依此类推。每个应用程序的index.php文件位于名为/public的子目录中 每当用户调用http://www.mywebsite.com/app1,nginx应该返回app1,如果用户调用http://www.mywebsite.com/app2,nginx应返回app2 我当前的nginxconf文件如下: server {
/var/www/html/app1
中有根目录,第二个在/var/www/html/app2
中,依此类推。每个应用程序的index.php
文件位于名为/public
的子目录中
每当用户调用http://www.mywebsite.com/app1
,nginx应该返回app1,如果用户调用http://www.mywebsite.com/app2
,nginx应返回app2
我当前的nginxconf
文件如下:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location /app1 {
root /var/www/html/app1/public;
index index.php;
}
location /app2 {
root /var/www/html/app2/public;
index index.php;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
}
但是,nginx总是返回404页的结果。这里出了什么问题?在linux服务器上的一次部署中,我遇到了您的一些挑战。情况如下
:一个Laravel项目需要在此基础上提供服务
:需要在此基础上提供另一个Laravel项目/
/
概念的任何数量的Laravel项目
现在让我们深入了解实际的实现
# Nginx.conf
# App 1(Path: /var/www/html/app1, Url: http://www.mywebsite.com)
# App 2(Path: /var/www/html/app2, Url: http://www.mywebsite.com/app2)
server {
# Listing port and host address
# If 443, make sure to include ssl configuration for the same.
listen 80;
listen [::]:80;
server_name www.mywebsite.com;
# Default index pages
index index.php;
# Root for / project
root /var/www/html/app1/public;
# Handle main root / project
location / {
#deny all;
try_files $uri $uri/ /index.php?$args;
}
# Handle app2 project, just replicate this section for further projects app3, app4
# by just replacing app2 with appropriate tag(app3/app4)
location /app2 {
# Root for this project
root /var/www/html/app2/public;
# Rewrite $uri=/app2/xyz back to just $uri=/xyz
rewrite ^/app2/(.*)$ /$1 break;
# Try to send static file at $url or $uri/
# Else try /index.php (which will hit location ~\.php$ below)
try_files $uri $uri/ /index.php?$args;
}
# Handle all locations *.php files (which will always be just /index.php)
# via factcgi PHP-FPM unix socket
location ~ \.php$ {
# At this point, $uri is /index.php, $args=any GET ?key=value and $request_uri = /app2/xyz.
# But we don't want to pass /app2/xyz to PHP-FPM, we want just /xyz to pass to fastcgi REQUESTE_URI below.
# This allows laravel to see /app2/xyz as just /xyz in its router.
# So laravel route('/xyz') responds to /app2/xyz as you would expect.
set $newurl $request_uri;
if ($newurl ~ ^/app2(.*)$) {
set $newurl $1;
root /var/www/html/app2/public;
}
# Pass all PHP files to fastcgi php fpm unix socket
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Use php fpm sock which is installed on your machine like php7.2, php5.6
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
# Here we are telling php fpm to use updated route that we've created to properly
# response to laravel routes.
fastcgi_param REQUEST_URI $newurl;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
# Deny .ht* access
location ~ /\.ht {
deny all;
}
}
注意:当我们使用基于会话的laravel设置时,所有路由生成器函数(url(),route()
)都使用主机名www.mywebsite.com
作为根url,而不是www.mywebsite.com/app2
。要解决此问题,请在laravel应用程序中执行以下更改
.env
文件中将APP\u URL
定义为APP\u URL=“www.mywebsite.com/app2”
app/Providers/RouteServiceProvider
的RouteServiceProvider
,并强制laravel将app\u URL用作应用程序的根URLphp artisan config:clear
或php artisan config:cache
命令以加载APP\u URL
的更新值
对于windows:配置中的错误是,如果浏览/app1,它会被重定向到/var/www/html/app1/public/app1/index.php,并且找不到该位置,并抛出404。此外,在每个位置块中指定索引指令是错误的,并且不是必需的。服务器块中定义的index指令就足够了。您需要使用
别名
而不是根
,并且需要为.php
URI在每个块中使用嵌套的位置
块。看,这个解决方案很完美,对我很有效。然而,有一个问题是拉拉维尔会议。会议没有正常进行。例如,每次我尝试登录到我的应用程序并访问另一个需要我登录的页面时,我都会被重定向回登录页面。这意味着会话未正确保存。有解决方法吗?回复我之前的评论,我通过在我的env文件中设置一个自定义的“SESSION_COOKIE”密钥解决了这个问题。这允许我为我的另一个laravel应用程序创建自定义会话名称。另一个解决方案是将APP_NAME变量从默认的“Laravel”更改为创建cookie名称。
public function boot()
{
parent::boot();
// Add following lines to force laravel to use APP_URL as root url for the app.
$strBaseURL = $this->app['url'];
$strBaseURL->forceRootUrl(config('app.url'));
}