Sat*_*rta 4 linux nginx laravel 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 {
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;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,nginx 总是返回 404 页面结果。这里出了什么问题?
在 linux 服务器上的一次部署期间,我遇到了您的一些挑战。如下
<base_url> :一个 Laravel 项目需要为此服务。<base_url>/<sub_url> : 另一个 Laravel 项目需要服务于此。当然,这可以扩展到任何遵循<base_url>/<unique_sub_url>概念的 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;
}
}
Run Code Online (Sandbox Code Playgroud)
注意:当我们使用基于会话的 Laravel 设置时,所有路由生成器函数(url(), route())都使用主机名www.mywebsite.com作为根 url,而不是www.mywebsite.com/app2. 要解决此问题,请在 laravel 应用程序中进行以下更改。
APP_URL在.env文件中定义为APP_URL="www.mywebsite.com/app2"RouteServiceProvider位于app/Providers/RouteServiceProvider并强制 laravel 使用 APP_URL 作为您应用程序的根 url。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'));
}
Run Code Online (Sandbox Code Playgroud)
更新:确保运行php artisan config:clear或php artisan config:cache命令加载 的更新值APP_URL。
对于 Windows:使用 Nginx 的多个 Laravel 应用程序 - Windows
| 归档时间: |
|
| 查看次数: |
1826 次 |
| 最近记录: |