当前位置:首页 >> 服务器

nginx实现一个域名配置多个laravel项目的方法示例

背景

随着公司的子项目越来越多,会有大大小小十几个工程(仅后端),按照原先的做法,每上线一个项目,那么必须要有一个二级域名映射到对应的工程上,十个工程那么就意味着需要有十个二级域名(还不包含测试环境,次生产环境等),如此多的域名不仅仅是难于管理,更重要的是比较浪费资源,这个问题困扰了我很久,今天终于解决了这个问题,特此记录一下采坑日记,本文不会讲nginx中各个指令的原理,而是用实际的项目配置来练习nginx指令的用法并举一反三。

事先准备

域名

假设域名为:http://www.dev.com

实验环境

阿里云ECS + centos + Nginx + php-fpm

项目1

1.工程路径: /data/wwwroot/project1/
2.访问路径:http://www.dev.com/project1/

项目2

1.工程路径: /data/wwwroot/project2/
2.访问路径:http://www.dev.com/project2/

项目3

1.工程路径: /data/wwwroot/project3/
2.访问路径:http://www.dev.com/project3/

涉及的知识点

  • Nginx的location指令,用法可以参考:https://www.jb51.net/article/154637.htm
  • Nginx的alias指令,用法可以参考:https://www.jb51.net/article/154640.htm

实现步骤

为了实现以上的访问形式,我们需要用到nginx里面的location指令和alias指令,配置如下

location ^~ /${PROJECT}/ {
 alias {$PATH};
 try_files $uri $uri/ @${PROJECT};

 location ~ \.php$ {
  fastcgi_pass unix:/dev/shm/php-cgi.sock;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $request_filename;
  include fastcgi_params;
 }
}

location @${PROJECT}{
 rewrite /${PROJECT}/(.*)$ /${PROJECT}/index.php"htmlcode">
location ^~ /project1/ {
 alias /data/wwwroot/project1/public;
 try_files $uri $uri/ @project1;

 location ~ \.php$ {
  fastcgi_pass unix:/dev/shm/php-cgi.sock;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $request_filename;
  include fastcgi_params;
 }
}

location @project1{
 rewrite /project1/(.*)$ /project1/index.php"htmlcode">
server {
 listen 80;
 server_name http://www.dev.com;
 access_log /data/wwwlogs/nginx/access_log/www.dev.com_nginx.log combined;
 error_log /data/wwwlogs/nginx/error_log/www.dev.com_errr_log;
 index index.html index.htm index.php;

 # project1开始的配置
 location ^~ /project1/ {
 alias /data/wwwroot/project1/public;
 try_files $uri $uri/ @project1;
 location ~ \.php$ {
  fastcgi_pass unix:/dev/shm/php-cgi.sock;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $request_filename;
  include fastcgi_params;
 }
 }
 
 location @project1{
 rewrite /project1/(.*)$ /project1/index.php?/$1 last;
 }
 
 # project2开始的配置
 location ^~ /project2/ {
 alias /data/wwwroot/project2/public;
 try_files $uri $uri/ @project2;
 
 location ~ \.php$ {
  fastcgi_pass unix:/dev/shm/php-cgi.sock;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $request_filename;
  include fastcgi_params;
 }
 }
 
 location @project2{
 rewrite /project2/(.*)$ /project2/index.php?/$1 last;
 }
 
 # project2开始的配置
 location ^~ /project3/ {
 alias /data/wwwroot/project3/public;
 try_files $uri $uri/ @project3;
 
 location ~ \.php$ {
  fastcgi_pass unix:/dev/shm/php-cgi.sock;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $request_filename;
  include fastcgi_params;
 }
 }
 
 location @project3{
 rewrite /project3/(.*)$ /project3/index.php?/$1 last;
 }
 
 
 # 解析所有的.php
 location ~ \.php$ {
 fastcgi_pass unix:/dev/shm/php-cgi.sock;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 #fastcgi_param SCRIPT_FILENAME $request_filename;
 include fastcgi_params;
 }
 
 #图片、视频的的链接,此处是做缓存 ,缓存30天,不写入访问日志
 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
 expires 30d;
 access_log off;
 }
 
 #js css文件的配置,此处是做缓存 ,缓存7天,不写入访问日志
 location ~ .*\.(js|css)?$ {
 expires 7d;
 access_log off;
 }

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。