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

Linux automatically deletes logs and example commands from n days ago

1. Delete file command: find the corresponding di...

Examples of 4 methods for inserting large amounts of data in MySQL

Preface This article mainly introduces 4 methods ...

HTML basics - CSS style sheets, style attributes, format and layout details

1. position : fixed Locked position (relative to ...

How to view files in Docker image

How to view files in a docker image 1. If it is a...

Record the steps of using mqtt server to realize instant communication in vue

MQTT Protocol MQTT (Message Queuing Telemetry Tra...

IE6 BUG and fix is ​​a preventive strategy

Original article: Ultimate IE6 Cheatsheet: How To...

Vue implements book management case

This article example shares the specific code of ...

JavaScript canvas Tetris game

Tetris is a very classic little game, and I also ...

JavaScript Advanced Custom Exception

Table of contents 1. Concept 1.1 What are errors ...

Several ways to shut down Hyper-V service under Windows 10

When using VMware Workstation to open a virtual m...

A brief discussion on JavaScript scope

Table of contents 1. Scope 1. Global scope 2. Loc...

Hide HTML elements through display or visibility

Sometimes we need to control whether HTML elements...