Laravel 配置二級目錄訪問

悠悠山雨發表於2019-08-12

功能實現

  • 域名http://test.local正常訪問,對應專案地址E:/Project/Test
  • 域名http://test.local/module2正常訪問,對應專案地址E:/Project/Module2

廢話不說上配置

Nginx配置檔案test_local.conf

server {
    listen       80;
    server_name  test.local;

    root E:/Project/Test;
    index  index.php index.html;

    # 轉發到埠
    location /module2/ {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Real-PORT $remote_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $proxy_host;
        proxy_set_header X-Forwarded-Port $proxy_port;
        proxy_pass http://127.0.0.1:8081/;
    }

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

server {
    listen       8081;
    server_name 127.0.0.1;
    root E:/Project/Module2;
    index  index.php index.html;

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

module2的Laravel配置Url

.env

APP_URL=http://test.local/module2

app/Providers/AppServiceProvider

    public function boot()
    {
        // 用於修正URL生成的連結
        app('url')->forceRootUrl(config('app.url'));
    }
    public function register()
    {
        // 用於修正分頁的連結
        \Illuminate\Pagination\Paginator::currentPathResolver(function () {
            return $this->app['url']->current();
        });
    }
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章