How to access multiple projects with one domain name in Nginx

PHPz
Release: 2023-05-16 21:37:20
forward
2084 people have browsed it

Matching introduction of location module

1.”=" prefix directive matching, if the match is successful, other matching will be stopped.

2. Ordinary string instructions are matched in order from longest to shortest. If the successfully matched location uses ^~, other matching will be stopped (regular matching).

3. Match the regular expression instructions in the order in the configuration file. If successful, other matching will be stopped.

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

Notes

1. The matching order is to match ordinary strings first, and then match regular expressions. In addition, the matching order of ordinary strings is based on the character length in the configuration from long to short, which means that the order of locations configured using ordinary strings is irrelevant. In the end, nginx will match according to the length of the configuration, but it should be noted that Regular expressions are tested in the order specified in the configuration file. Finding the first matching regular expression will stop the search.

2. Under normal circumstances, regular expression location matching will be performed after the ordinary string location is successfully matched. There are two ways to change this behavior. One is to use the "=" prefix. At this time, strict matching is performed, and other matching is stopped immediately after the match is successful, and the request is processed at the same time. The other is to use the "^~" prefix. , if used with a regular string, tells nginx not to test the regular expression if the path matches.

location = /uri
Copy after login

= starts with an exact match, and it will only take effect if it matches exactly.

location ^~ /uri
Copy after login

^~ Prefix matching is performed on the url path at the beginning, and before the regular expression.

location ~ pattern
Copy after login

~ indicates case-sensitive regular matching.

location ~* pattern
Copy after login

~* indicates case-insensitive regular matching.

location /uri
Copy after login

Without any modifier, it also means prefix matching, but after regular matching.

location /
Copy after login

Universal matching, any request that does not match other locations will be matched, which is equivalent to 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;

 # 域名+项目1名称
 location ^~ /a1/ {
   alias /usr/share/nginx/html/a1/public/;
 }

 # 域名+项目2名称
 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;
 }
}
Copy after login

Effect preview

1. Visit a1 project

How to access multiple projects with one domain name in Nginx

2. Visit a2 project

How to access multiple projects with one domain name in Nginx

The above is the detailed content of How to access multiple projects with one domain name in Nginx. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!