使用Nginx的多个Laravel应用程序

使用Nginx的多个Laravel应用程序

本文介绍了使用Nginx的多个Laravel应用程序-Windows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器上有两个不同的laravel应用程序.

I have two different laravel application on my server machine.

它们位于:

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应用程序将完美展示.

If I browse http://x.x.x.x/, my first laravel web application comes out perfectly.

但是,如果我浏览 http://x.x.x.x/application2 ,则表示未指定输入文件.

But if I browse http://x.x.x.x/application2 I am having No input file specified.

我这里想念什么吗?

推荐答案

对于Windows,请使用 fastcgi_pass 作为 127.0.0.1:9000 ,而不是unix套接字.

For windows use fastcgi_pass as 127.0.0.1:9000 instead of unix socket.

请确保您的php cgi正在运行.如果没有,您可以通过

Please make sure your php cgi is running. If not, you can start it by

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 configuration with rewrite module.

# 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应用中进行以下更改.

Note: When we're using session based laravel setup, all the route generator functions(url(), route()) use hostname localhost as root url not localhost/application2. To resolve this issue please do following changes in laravel app.

  1. .env 文件中将 APP_URL 定义为 APP_URL ="localhost/application2"
  2. 转到位于 app/Providers/RouteServiceProvider RouteServiceProvider ,并强制laravel将APP_URL用作应用程序的根URL.
  1. Define APP_URL in .env file as APP_URL="localhost/application2"
  2. Go to RouteServiceProvider which is located at app/Providers/RouteServiceProvider and force laravel to use APP_URL as root url for your app.
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 命令以加载的更新值> APP_URL .

Update: Make sure to run php artisan config:clear or php artisan config:cache command to load the updated value of APP_URL.

对于Linux系统:

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

这篇关于使用Nginx的多个Laravel应用程序-Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 17:42