Nginx:在 Linux 中为多个具有相同 URL 但有两个不同子位置的 Laravel 应用程序提供服务 [英] Nginx: Serve multiple Laravel apps with same url but two different sub locations in Linux
问题描述
我想在单个 nginx 服务器中提供多个 Laravel 应用程序,第一个在 /var/www/html/app1
中有一个根目录,第二个在 /var/www/html/app2
等.每个应用程序的index.php
文件位于名为/public
的子目录中.
I want to serve multiple Laravel apps in a single nginx server, the first one has a root directory in /var/www/html/app1
, the second one has /var/www/html/app2
, and so on. The index.php
file of each app is in a subdirectory named /public
.
每当用户调用 http://www.mywebsite.com/app1
时,nginx 应该返回 app1,如果用户调用 http://www.mywebsite.com/app2
code>,nginx 应该返回 app2.
Whenever user calls http://www.mywebsite.com/app1
, nginx shoulds return the app1 and if user calls http://www.mywebsite.com/app2
, nginx shoulds return the app2.
我当前的 nginx conf
文件如下:
My current nginx conf
file is as below:
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 页面结果.这里出了什么问题?
But, nginx always returning 404 page result. What's going wrong here?
推荐答案
在 linux 服务器上的一次部署期间,我遇到了您的一些挑战.是这样的
During one of the deployment on linux server, I came across some sort of your challenge. It was as follow
:一个 Laravel 项目需要为此提供服务.<base_url>/<sub_url>
:另一个 Laravel 项目需要为此提供服务.
<base_url>
: One Laravel project needs to served on this.<base_url>/<sub_url>
: Another Laravel project needs to be served on this.
当然,这可以扩展到任何遵循
概念的 Laravel 项目.
Of course this can be extended to any number of Laravel projects which follows <base_url>/<unique_sub_url>
concept.
现在让我们深入了解实际实施
Now let's dive into actual implementation
# 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 应用中进行以下更改.
Note: When we're using session based laravel setup, all the route generator functions(url(), route()
) use hostname www.mywebsite.com
as root url, not www.mywebsite.com/app2
. To resolve this issue please do following changes in laravel app.
- 将
.env
文件中的APP_URL
定义为APP_URL=www.mywebsite.com/app2"
- 转到位于
app/Providers/RouteServiceProvider
的RouteServiceProvider
并强制 Laravel 使用 APP_URL 作为您应用的根 url.
- Define
APP_URL
in.env
file asAPP_URL="www.mywebsite.com/app2"
- Go to
RouteServiceProvider
which is located atapp/Providers/RouteServiceProvider
and force laravel to use APP_URL as root url for your app.
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'));
}
Update:确保运行 php artisan config:clear
或 php artisan config:cache
命令来加载 APP_URL
.
Update: Make sure to run php artisan config:clear
or php artisan config:cache
command to load the updated value of APP_URL
.
对于 Windows:使用 Nginx 的多个 Laravel 应用程序 - Windows
For windows: Multiple Laravel Applications Using Nginx - Windows
这篇关于Nginx:在 Linux 中为多个具有相同 URL 但有两个不同子位置的 Laravel 应用程序提供服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!