單個域名下部署多個專案-配置 Nginx 資料夾 / 子目錄訪問-UNIX代理方式

悠悠山雨發表於2021-03-10

說明

  • 目前最優解的多專案部署在子目錄的方式:+1:
  • 子目錄的路徑修正只用到laravel的代理中介軟體:star2:
  • 實現域名test.cc的路徑module2指向另一個專案:heartbeat:
    • test.cc專案路徑E:/Project/Test
    • test.cc/module2專案路徑E:/Project/Module2

nginx 配置

server {
    listen       80;
    server_name  test.cc;
    root E:/Project/Test;
    index  index.php index.html;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # 此處重要!
    # ==============================================================
    location /module2/ {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Prefix /module2/;
        proxy_pass http://unix:/tmp/nginx_module2.socket:/;
    }
    # ==============================================================

    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      unix:/tmp/nginx_module2.socket;
    # ==============================================================

    server_name _;
    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;
    }
}

laravel 7.2 配置

修改中介軟體 Http\Middleware\TrustProxies.php

protected $proxies = "*";
protected $headers = Request::HEADER_X_FORWARDED_PREFIX;
本作品採用《CC 協議》,轉載必須註明作者和本文連結
轉載請告知

相關文章