【问题标题】:Nginx: Serve multiple Laravel apps with same url but two different sub locations in LinuxNginx:为多个 Laravel 应用程序提供相同的 url,但在 Linux 中有两个不同的子位置
【发布时间】:2019-03-29 20:18:07
【问题描述】:

我想在单个 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;
        }

}

但是,nginx 总是返回 404 页面结果。这里出了什么问题?

【问题讨论】:

  • 你的配置出了什么问题,如果你浏览 /app1 它会被重定向到 /var/www/html/app1/public/app1/index.php 并且找不到这个位置并抛出一个404.此外,在每个位置块中指定索引指令是错误的并且不是必需的。 server 块中定义的 index 指令就足够了。
  • 您需要使用alias 而不是root,并且您需要在每个.php URI 中使用嵌套的location 块。见this answer

标签: linux laravel nginx server


【解决方案1】:

在 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;
    }
}

注意:当我们使用基于会话的 laravel 设置时,所有路由生成器函数(url(), route())都使用主机名 www.mywebsite.com 作为根 url,而不是 www.mywebsite.com/app2。要解决此问题,请在 laravel 应用中进行以下更改。

  1. .env文件中的APP_URL定义为APP_URL="www.mywebsite.com/app2"
  2. 转到位于app/Providers/RouteServiceProviderRouteServiceProvider 并强制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'));
}

更新:确保运行php artisan config:clearphp artisan config:cache 命令以加载APP_URL 的更新值。

对于 Windows:Multiple Laravel Applications Using Nginx - Windows

【讨论】:

  • 这个解决方案非常完美,对我有用。但是,有 1 个问题是 Laravel 会话。会议没有正常工作。例如,每次我尝试登录我的应用程序并访问另一个需要我登录的页面时,我都会被重定向回登录页面。这意味着会话未正确保存。有解决办法吗?
  • 回复我之前的评论,我通过在我的 env 文件中设置自定义“SESSION_COOKIE”键解决了这个问题。这允许我为我的其他 laravel 应用程序创建一个自定义会话名称。另一种解决方案是将 APP_NAME 变量从默认的“Laravel”更改为用于创建 cookie 名称。
猜你喜欢
  • 2019-05-09
  • 1970-01-01
  • 2017-01-19
  • 2014-01-15
  • 2021-10-17
  • 1970-01-01
  • 1970-01-01
  • 2017-11-26
相关资源
最近更新 更多