【问题标题】:Multiple Laravel Applications Using Nginx - Windows使用 Nginx 的多个 Laravel 应用程序 - Windows
【发布时间】:2020-12-31 23:21:41
【问题描述】:

我的服务器机器上有两个不同的 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;
        }
    }  



     
    }

如果我浏览 http://x.x.x.x/,我的第一个 laravel web 应用程序就完美了。

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

我在这里缺少什么?

【问题讨论】:

  • 更好地使用laragon.org 进行开发
  • 对于两个 url,我在 nginx 中使用了 1 个配置文件,而不是在与特定端口和服务器名称的 nginx 列表相同的文件中
  • 所以你想要动态的。?

标签: laravel windows nginx


【解决方案1】:

对于 Windows,使用 fastcgi_pass 作为 127.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

带有重写模块的 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;
    }
}

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

  1. .env文件中的APP_URL定义为APP_URL="localhost/application2"
  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的更新值。

Linux 系统:Nginx: Serve multiple Laravel apps with same url but two different sub locations in Linux

【讨论】:

  • 感谢您的回答,但我在 Windows 机器上运行。
  • 非常感谢,如果我们解决了这个问题,我会给你+100 奖励。非常感谢
  • 我不确定我是否有你的问题,但我将它用于我的 laravel 应用程序。但我手动安装了 nginx。
  • 我手动安装的,没有第三方。我从nginx.org/en/download.html得到它
  • SO 说我可以在 3 小时内奖励另一个问题的 +100 代表。届时将给予。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-24
  • 2016-07-10
  • 2022-09-27
  • 1970-01-01
  • 1970-01-01
  • 2017-01-10
  • 2015-02-22
相关资源
最近更新 更多