请问,ngnix 如何配置,使用(同样的域名)+ (不同后缀)来访问不同的项目?

需求:
使用相同的域名+路径,到达访问不同的laravel项目的目的。

例如:
laravel项目1,放在宿主机/var/www/html/children下;
laravel项目2,放在宿主机/var/www/html/duanwu下。

目标是希望通过:
www.local.com/children 进入laravel项目1;
www.local.com/duanwu 进入laravel项目2;

原来的做法,每个laravel都使用一个二级域名,例如:
laravel项目1,使用children.local.com这个域名访问;laravel项目2,使用duanwu.local.com这个域名访问。

其中的一个虚拟主机配置如下:
server {
listen 80;
server_name children.local.com;

location / {
    root   /usr/share/nginx/html/children/public;
    index  index.php index.html index.htm;
    try_files $uri $uri/ /index.php?$query_string;
}

error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}

location ~ \.php$ {
    root           html;
    fastcgi_pass   php:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /var/www/html/children/public$fastcgi_script_name;
    include        fastcgi_params;
} 

}

这样,访问children.lcoal.com能正常进入项目1,访问duanwu.local.com能正常进入项目2。

系统环境:
CENTOS7+DOCKER

laravel运行环境:
docker分别运行nginx容器,php容器,Mysql容器

目录挂载:
在nginx容器中
将宿主机的/var/www/html挂载到容器的/usr/share/nginx/html下

在php容器中
将宿主机的/var/www/html挂载到容器的/var/www/html下

在Mysql容器中
将宿主机的/var/lib/mysql挂载到容器的/var/lib/mysql下

nginx,php,Mysql的配置文件也放在宿主机上,挂载到各个容器的相关目录。

每个laravel项目都分别从git版本库中克隆不同的项目到/var/www/html下。

使用如下虚拟主机配置,访问www.local.com/children,出现403 Forbidden。

server {
listen 80;
server_name www.local.com;

location / {
    root   /usr/share/nginx/html/;
    index  index.php index.html index.htm;
}

error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}

location ^~ /children/ {
    alias /usr/share/nginx/html/children/public;
    try_files $uri $uri/ @children;

location ~ \.php$ {
    root           html;
    fastcgi_pass   php:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /var/www/html/children/public$fastcgi_script_name;
    include        fastcgi_params;
}
}

location @children {
    rewrite /children/(.*)$ /children/index.php?/$1 last;
}

}

请问,该如何配置nginx,来满足我们的需求?

《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 6
server {
        listen 80;
        server_name  www.local.com;
        location /children {
            root /var/www/html/children;
            index index.php index.html index.htm;
        }
        location /duanwu {
            alias /var/www/html/duanwu;
            index index.php index.html index.htm;
        }
}

这个叫「虚拟目录」,第一次接触是在 IIS,所以知道有这个功能,谷歌了下给你答案,你试试行不。

4年前 评论

尝试了,不行!@likunyan

4年前 评论
小李世界 4年前
tumobi

每个 laravel 项目配置成一个单独的站点(不同端口号),然后使用 location 匹配前缀转发。

4年前 评论
ruke

这个和 fastcgi_param SCRIPT_FILENAME 有关好像, 我之前配置过一个普通项目laravel项目

server {
        listen 80;
        server_name ts.itruke.com;
        root /var/www/test/app;
        location ^~ /api {
                alias /var/www/test/api/public;
                try_files $uri $uri/ @laravel;
                index index.php;

                location ~ \.php$ {
                        fastcgi_split_path_info ^(.+\.php)(/.+)$;
                        fastcgi_pass unix:var/run/php/php7.2-fpm.sock;
                        include fastcgi_params;
                        fastcgi_param SCRIPT_FILENAME /var/www/test/api/public/index.php;
                }
        }
        location @laravel {
                rewrite /api/(.*)$ /api/index.php?/$1 last;
        }
        location / {
                index index.php;
                location ~ \.php$ {
                        fastcgi_pass unix:var/run/php/php7.2-fpm.sock;
                        fastcgi_param  SCRIPT_FILENAME  /var/www/test/app/$fastcgi_script_name;
                        include fastcgi_params;
                }
        }
}
4年前 评论
sane
server {
    listen 80;
    charset utf-8;
    server_name www.local.vip;
    index index.html index.htm index.php;
    root /data/www/;

    access_log /data/logs/pages/access.log main;
    error_log  /data/logs/pages/error.log error;

    location ~* /pages {
         try_files $uri $uri/ /pages/index.html;
    }

   location / {
        alias /data/www/www/;
        access_log /data/logs/www/access.log main;
        error_log  /data/logs/www/error.log error;
        index index.html index.htm index.php;
        try_files $uri $uri/ /index.html;
    }

    location ~* /market_pages {
        alias /data/www/pages/;
        try_files $uri $uri/ /market_pages/index.html;
    }

    location /ueditor {
        proxy_pass http://admin.local.vip;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    sendfile off;

    client_max_body_size 100m;

    location ~ /\.ht {
        deny all;
    }
}

这个是我们多个 SPA 项目需要在同一个域名下的配置, 和一楼的相差不大, 可以参照下;

4年前 评论

project1开始的配置

location ^~/wxapp/ {
   alias D:/xxx/xxx/public/;
   try_files $uri $uri/ @wxapp;
   location ~ \.php$ {
      fastcgi_pass   127.0.0.1:4571;
      fastcgi_index  index.php;
      fastcgi_param SCRIPT_FILENAME $request_filename;
      include        fastcgi_params;
   }
}
location @wxapp{
    rewrite /wxapp/(.*)$ /wxapp/index.php?/$1 last;
}

# project2开始的配置
location ^~ /fzcms/ {
   alias D:/xxx/xxx/public/;
   try_files $uri $uri/ @fzcms;
   location ~ \.php$ {
      fastcgi_pass   127.0.0.1:4571;
      fastcgi_index  index.php;
      fastcgi_param SCRIPT_FILENAME $request_filename;
      include        fastcgi_params;
   }
}
location @fzcms{
    rewrite /fzcms/(.*)$ /fzcms/index.php?/$1 last;
}
把路径配置部分改成这样区分不同项目,其它相同; 注意public后面的/要加上,不然会报404
4年前 评论

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