Nginx one domain name to access multiple projects method example

Nginx one domain name to access multiple projects method example

Background

Recently, I encountered such a problem in the deployment of multiple projects: how to achieve access to multiple projects with one domain name. Because I didn't want to apply for a domain name certificate and domain name configuration by myself, I thought of this solution, combined with Nginx's location function to achieve my needs, and then recorded it. The example uses a PHP project for demonstration, and other languages ​​can be deployed in a similar way. For example, for a node project, you can do a verification in location and then use the porxy_pass reverse proxy module to implement it.

Introduction to matching of location module

1. The "=" prefix instruction matches. If the match succeeds, other matches are stopped.

2. Ordinary string instruction matching, the order is from long to short, if the location that matches successfully uses ^~, then stop other matching (regular matching).

3. Regular expression instructions are matched in the order in the configuration file, and other matches are stopped if they succeed.

4. If there is a match in the third step, use the result, otherwise use the result of the second step.

Note

1. The matching order is to match the normal string first, and then match the regular expression. In addition, the order of common string matching is from long to short according to the length of characters in the configuration. That is to say, the order of locations configured with common strings is irrelevant. In the end, nginx will match according to the length of the configuration. However, it should be noted that regular expressions are tested in the order in the configuration file. The search stops when the first match of the regular expression is found.

2. In general, after successfully matching the ordinary string location, the regular expression location will also be matched. There are two ways to change this behavior. One is to use the "=" prefix, which performs a strict match and stops other matches immediately after a successful match while processing the request. The other is to use the "^~" prefix. If this prefix is ​​used for a regular string, it tells nginx not to test the regular expression if the path matches.

location = /uri

= indicates an exact match, and only a complete match will take effect.

location ^~ /uri

^~ starts with a prefix match on the URL path and precedes the regular expression.

location ~ pattern

~ starts with a case-sensitive regular expression.

location ~* pattern

~* starts with a case-insensitive regular expression.

location /uri

Without any modifiers, it also means prefix matching, but after the regular expression matching.

location /

Universal matching, any request that does not match other locations will be matched, which is equivalent to the default in switch.

Configuration Example

server {
 listen 80;
 server_name test.com;
 index index.html index.htm index.php;
 charset koi8-r;
 access_log /var/log/nginx/host.access.log main;

 #Domain name + project 1 name location ^~ /a1/ {
   alias /usr/share/nginx/html/a1/public/;
 }

 #Domain name + project 2 name location ^~ /a2/ {
   alias /usr/share/nginx/html/a2/public/;
 }

 error_page 404 /404.html;

 # redirect server error pages to the static page /50x.html
 
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
  root /usr/share/nginx/html/500.html;
 }

 #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 
 location ~ \.php$ {
  root html;
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  include fastcgi_params;
 }
 
 location ~ /\.ht {
  deny all;
 }
}

Effect Preview

1. Visit a1 project

2. Visit a2 Project

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Nginx prohibits IP access and only allows domain name access
  • Detailed explanation of implementing Nginx+Tomcat to achieve single IP, multiple domain names, and multiple sites access
  • How to configure Nginx to distinguish between PC or mobile phone access to different domain names
  • Springboot+nginx+https+linux to achieve load balancing and domain name access simple test
  • Implementation of Nginx configuration of multi-port and multi-domain name access

<<:  You may not know these things about Mysql auto-increment id

>>:  Summary of three methods of lazy loading lazyLoad using native JS

Recommend

Detailed explanation of the usage of grep command in Linux

1. Official Introduction grep is a commonly used ...

Native JavaScript carousel implementation method

This article shares the implementation method of ...

Vue sample code for implementing two-column horizontal timeline

Table of contents 1. Implement the component time...

Implementation of MySQL5.7 mysqldump backup and recovery

MySQL backup Cold backup:停止服務進行備份,即停止數據庫的寫入Hot ba...

About the pitfalls of implementing specified encoding in MySQL

Written in front Environment: MySQL 5.7+, MySQL d...

How to optimize the slow Like fuzzy query in MySQL

Table of contents 1. Introduction: 2. The first i...

MySQL Basics in 1 Hour

Table of contents Getting Started with MySQL MySQ...

Example of using JS to determine whether an element is an array

Here are the types of data that can be verified l...

mysql group by grouping multiple fields

In daily development tasks, we often use MYSQL...

How to make a tar file of wsl through Docker

I've been playing with the remote development...

Tutorial on how to remotely connect to MySQL database under Linux system

Preface I recently encountered this requirement a...

A comprehensive summary of frequently used statements in MySQL (must read)

The knowledge points summarized below are all fre...