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/ Project 2 1. Project path: /data/wwwroot/project2/ Project 3 1. Project path: /data/wwwroot/project3/ Knowledge points involved
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:
|
<<: Summary of Mysql table, column, database addition, deletion, modification and query problems
>>: Upgrade MySQL 5.1 to 5.5.36 in CentOS
The figure below shows the browser viewing rate i...
Table of contents Install Tomcat with Docker Use ...
Configure the accelerator for the Docker daemon S...
The "nofollow" tag was proposed by Goog...
1. The vertical-align property achieves the follo...
Vue recommends using templates to create your HTM...
1. Create a user: Order: CREATE USER 'usernam...
1. Division of labor and process <br />At T...
Table of contents 1. Extracting functions 2. Merg...
Table of contents 1. Self-enumerable properties 2...
Recently, there is a requirement for uploading pi...
Table of contents Why use Docker? Docker installa...
This article shares the specific code of js to im...
Recently, the Vue project needs to refresh the da...
I have several tomcats here. If I use them at the...