Nginx在两个不同的位置提供多个应用程序

时间:2018-10-25 09:10:04

标签: 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。

我当前的nginx conf文件如下:

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页面结果。怎么了?

1 个答案:

答案 0 :(得分:6)

在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. APP_URL文件中将.env定义为APP_URL="www.mywebsite.com/app2"
  2. 转到位于RouteServiceProvider的{​​{1}},并强制laravel将APP_URL用作您的应用的根URL。
app/Providers/RouteServiceProvider

更新:确保运行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:clear命令以加载php artisan config:cache的更新值。

对于Windows:Multiple Laravel Applications Using Nginx - Windows