使用 Nginx 的多个 Laravel 应用程序 - Windows

kap*_*tan 1 windows nginx laravel

我的服务器计算机上有两个不同的 Laravel 应用程序。

他们位于:

D:/APPLICATION/application1

D:/APPLICATION/application2

以下是我的nginx.conf内容:

server {
        listen       80;
        server_name  localhost;        

        location / {
        root "D:/APPLICATION/application1/public";
        try_files $uri $uri/ /index.php?$query_string;
        index index.php index.html index.htm;

        location ~ \.php$ {
            try_files $uri /index.php = 404;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

        }

    location ^~ /application2 {
        alias "D:/APPLICATION/application2/public";     
        try_files $uri $uri/ /index.php?$query_string;
        index index.php index.html index.htm;

            location ~ \.php$ {
               try_files $uri /index.php = 404;
               fastcgi_pass  127.0.0.1:9000;
               fastcgi_index index.php;
               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
               include fastcgi_params;
        }
    }  



     
    }
Run Code Online (Sandbox Code Playgroud)

如果我浏览http://x.x.x.x/,我的第一个 Laravel Web 应用程序就会完美呈现。

但如果我浏览http://x.x.x.x/application2的话No input file specified.

我在这里缺少什么吗?

Jit*_*dav 5

对于 Windows,请使用fastcgi_passas127.0.0.1:9000而不是 unix 套接字。

请确保您的 php cgi 正在运行。如果没有,您可以通过以下方式启动

1. Open command prompt
2. Go to path of php-cgi file. (e.g. C:\php-7.3.11, here you'll find fast-cgi.exe).
2. php-cgi.exe -b 127.0.0.1:9000
Run Code Online (Sandbox Code Playgroud)

带重写模块的 Nginx 配置。

# Nginx.conf
# App 1(Path: D:/APPLICATION/application1, Url: http://localhost)
# App 2(Path: D:/APPLICATION/application2, Url: http://localhost/application2)
server {
    # Listing port and host address
    # If 443, make sure to include ssl configuration for the same.
    listen 80; 
    listen [::]:80;

    server_name localhost;

    # Default index pages
    index index.php;

    # Root for / project
    root "D:/APPLICATION/application1/public";

    # Handle main root / project
    location / {
        #deny all;
        try_files $uri $uri/ /index.php?$args;
    }

    # Handle application2 project
    location /application2 {
        # Root for this project
        root "D:/APPLICATION/application2/public";

        # Rewrite $uri=/application2/xyz back to just $uri=/xyz
        rewrite ^/application2/(.*)$ /$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$ {
        # We don't want to pass /application2/xyz to PHP-FPM, we want just /xyz to pass to fastcgi REQUESTE_URI below. 
        # So laravel route('/xyz') responds to /application2/xyz as you would expect.
        set $newurl $request_uri;
        if ($newurl ~ ^/application2(.*)$) {
                set $newurl $1;
                root "D:/APPLICATION/application2/public";
        }

        # Pass all PHP files to fastcgi php fpm unix socket
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # Use php fastcgi rather than php fpm sock
        fastcgi_pass  127.0.0.1:9000; 
        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;
    }

    # Deny .ht* access
    location ~ /\.ht {
        deny all;
    }
}

Run Code Online (Sandbox Code Playgroud)

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

  1. APP_URL在文件中定义.envAPP_URL="localhost/application2"
  2. 转到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:clearphp artisan config:cache命令加载更新后的值APP_URL

对于 Linux 系统:Nginx:在 Linux 中为具有相同 url 但两个不同子位置的多个 Laravel 应用程序提供服务