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

悠悠山雨發表於2021-02-19

說明

  • 目前最推薦的多專案部署在子目錄的方式:+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://127.0.0.1:8081/;
    }
    # ==============================================================

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

laravel 7.2 配置

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

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

相關文章