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

功能实现

  • 域名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 协议》,转载必须注明作者和本文链接
转载请告知
本帖由系统于 3年前 自动加精
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 3

转发到端口

location /module2/ {
    try_files $uri $uri/ /module2/index.php?$query_string;
}

这样就可以吧

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

可以这样写:

if (!-e $request_filename) {
       rewrite  ^/module2/(.*)$  /module2/index.php?s=$1  last;
       break;
}
4年前 评论
tsin (作者) 4年前

你写的重定向方式为什么不需要改动laravel框架内容呢 哪种好呢 感觉还是使用2个域名吧 问题最少

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

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