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

Web page layout should consider IE6 compatibility issues

The figure below shows the browser viewing rate i...

Detailed steps for installing Tomcat, MySQL and Redis with Docker

Table of contents Install Tomcat with Docker Use ...

How to change the domestic image source for Docker

Configure the accelerator for the Docker daemon S...

How to write the Nofollow tag and how to use it

The "nofollow" tag was proposed by Goog...

CSS to achieve horizontal lines on both sides of the middle text

1. The vertical-align property achieves the follo...

Detailed explanation of the use of Vue.js render function

Vue recommends using templates to create your HTM...

mysql5.7 create user authorization delete user revoke authorization

1. Create a user: Order: CREATE USER 'usernam...

Tudou.com front-end overview

1. Division of labor and process <br />At T...

Summary of 11 amazing JavaScript code refactoring best practices

Table of contents 1. Extracting functions 2. Merg...

Several ways to easily traverse object properties in JS

Table of contents 1. Self-enumerable properties 2...

Detailed explanation of webpage screenshot function in Vue

Recently, there is a requirement for uploading pi...

About ROS2 installation and docker environment usage

Table of contents Why use Docker? Docker installa...

js implements the algorithm for specifying the order and amount of red envelopes

This article shares the specific code of js to im...

Real-time refresh of long connection on Vue+WebSocket page

Recently, the Vue project needs to refresh the da...

How to change the tomcat port number in Linux

I have several tomcats here. If I use them at the...