An example of how to use nginx to configure multiple laravel projects with one domain name

An example of how to use nginx to configure multiple laravel projects with one domain name

background

As the company's sub-projects increase, there will be more than a dozen projects of different sizes (backend only). According to the original practice, for each project launched, there must be a second-level domain name mapped to the corresponding project. Ten projects mean that there will be ten second-level domain names (not including test environment, sub-production environment, etc.). Such a large number of domain names is not only difficult to manage, but more importantly, it is a waste of resources. This problem has troubled me for a long time. Today, I finally solved it. I would like to record the pit diary. This article will not explain the principles of each instruction in nginx, but use actual project configuration to practice the usage of nginx instructions and learn from it.

Preparation

domain name

Assume the domain name is: http://www.dev.com

Experimental environment

Alibaba Cloud ECS + CentOS + Nginx + PHP-FPM

Project 1

1. Project path: /data/wwwroot/project1/
2. Access path: http://www.dev.com/project1/

Project 2

1. Project path: /data/wwwroot/project2/
2. Access path: http://www.dev.com/project2/

Project 3

1. Project path: /data/wwwroot/project3/
2. Access path: http://www.dev.com/project3/

Knowledge points involved

  • Nginx location directive, usage can refer to: https://www.jb51.net/article/154637.htm
  • Nginx alias directive, usage can refer to: https://www.jb51.net/article/154640.htm

Implementation steps

In order to achieve the above access form, we need to use the location directive and alias directive in nginx, the configuration is as follows

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?/$1 last;
}

Note: ${PROJECT} and {$PATH} in the above configuration are the parts that need to be replaced in the actual process. ${PROJECT} is the path part of the URL that needs to be accessed, such as project1, and {$PATH} represents the actual access path of the project, such as /data/wwwroot/project1. Taking http://www.dev.com/project1 as an example, the corresponding Nginx configuration is as follows

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

For the configuration of project2 and project3, you only need to follow the above configuration template. The complete nginx configuration is as follows

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;

 # Configuration location of project1 ^~ /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;
 }
 
 # Configuration location of project2 ^~ /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;
 }
 
 # Configuration location of project2 ^~ /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;
 }
 
 
 # Parse all .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;
 }
 
 #Links to pictures and videos, this is for caching, caching for 30 days, and not writing access logs location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
 expires 30d;
 access_log off;
 }
 
 #Configuration of js css files, here is the cache, cache for 7 days, do not write access log location ~ .*\.(js|css)?$ {
 expires 7d;
 access_log off;
 }

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

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to modify the .env configuration file in Laravel
  • Sharing the configuration files of running PHP framework Laravel in Nginx
  • Laravel framework environment and configuration operation example analysis
  • Laravel front-end resource configuration tutorial
  • Example of configuring global variables in laravel config file
  • Laravel database read-write separation configuration method
  • Laravel database encryption and database table prefix configuration method
  • Laravel framework database configuration and database operation example
  • laravel-admin automatically generates modules and related basic configuration methods
  • Detailed explanation of the difference between laravel configuration routing api and web defined routing
  • Detailed configuration of Laravel5.6 framework using CKEditor5
  • Laravel configuration global public function method steps
  • Laravel5 framework custom error page configuration operation example
  • How to configure multiple Redis libraries in laravel
  • Laravel framework configuration 404 and other abnormal pages
  • Laravel 5.5 officially recommended Nginx configuration learning tutorial
  • Analysis of Laravel Memcached cache driver configuration and application methods
  • Detailed explanation of Laravel 5+ .env environment configuration file

<<:  Summary of Mysql table, column, database addition, deletion, modification and query problems

>>:  Upgrade MySQL 5.1 to 5.5.36 in CentOS

Recommend

Vue installation and use

Table of contents 1. Vue installation Method 1: C...

MySQL Basic Tutorial: Detailed Explanation of DML Statements

Table of contents DML statements 1. Insert record...

CSS3 creates web animation to achieve bouncing ball effect

Basic preparation For this implementation, we nee...

Vue login function implementation

Table of contents Written in front Login Overview...

Docker Data Storage Volumes Detailed Explanation

By default, the reading and writing of container ...

In-depth analysis of Vue's responsive principle and bidirectional data

Understanding object.defineProperty to achieve re...

Disable input text box input implementation properties

Today I want to summarize several very useful HTML...

How to upgrade all Python libraries in Ubuntu 18.04 at once

What is pip pip is a Python package management to...

How to update v-for in Vue

Tips: Array change method will cause v-for to upd...

Right align multiple elements in the same row under div in css

Method 1: float:right In addition, floating will ...

How to run top command in batch mode

top command is the best command that everyone is ...

Eight common SQL usage examples in MySQL

Preface MySQL continued to maintain its strong gr...