单个域名下部署多个项目-配置 Nginx 文件夹 / 子目录访问-UNIX代理方式

说明

  • 目前最优解的多项目部署在子目录的方式:+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 协议》,转载必须注明作者和本文链接
转载请告知
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 4

如果是静态资源怎么办?比如 laravel-admindcat-admin

2年前 评论
悠悠山雨 (楼主) 2年前

采用 unix 方式 laravel 在验证代理请求时,校验 ip 失败,server 返回:

file

2年前 评论
悠悠山雨 (楼主) 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!