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
= starts with an exact match, and it will only take effect if it matches exactly.
location ^~ /uri
^~ Prefix matching is performed on the url path at the beginning, and before the regular expression.
location ~ pattern
~ indicates case-sensitive regular matching.
location ~* pattern
~* indicates case-insensitive regular matching.
location /uri
Without any modifier, it also means prefix matching, but after regular matching.
location /
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; } }
Effect preview
1. Visit a1 project
2. Visit a2 project
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!