Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example of a method for Nginx to access multiple projects with one domain name

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Background introduction

Recently, in the deployment of multiple projects, we have encountered the problem of how to access multiple projects with one domain name. Because I don't want to apply for domain name certificate and domain name configuration separately, I thought of this solution. I realized my own needs by combining the location function of Nginx, and recorded it. In the example, the project is demonstrated by PHP, and other languages are deployed in the same way. For example, the node project can be validated in location and then implemented using the porxy_pass reverse proxy module.

Introduction to the matching of location modules

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

two。 The normal string instruction matches, in the order from long to short, and the successful location stops other matches (regular matching) if it uses ^ ~.

3. The regular expression instruction matches, according to the order in the configuration file, and stops other matches if it succeeds.

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

Pay attention

1. The order of matching is to match the normal string first and then the regular expression. In addition, the matching order of ordinary strings is based on the length of characters in the configuration from long to short, that is, the order of location configured with ordinary strings is irrelevant. Anyway, nginx will match according to the length of the configuration, but it is important to note that regular expressions are tested according to the order in the configuration file. Finding the first matching regular expression stops the search.

two。 In general, regular expression location matching is performed after a successful match of the normal string location. There are two ways to change this behavior, one is to use the "=" prefix, which performs a strict match, and immediately stops other matches after a successful match, while processing the request; the other is to use the "^ ~" prefix, which tells nginx not to test the regular expression if the path matches.

Location = / uri

The beginning of = indicates an exact match, which can only take effect on an exact match.

Location ^ ~ / uri

^ ~ begins with a prefix match on the URL path and comes before the regular.

Location ~ pattern

~ begins with a case-sensitive regular match.

Location ~ * pattern

The beginning of ~ * indicates a case-insensitive regular match.

Location / uri

Without any modifiers, it also means that the prefix matches, but after the regular match.

Location /

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

Configure an instance

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.htl; location = / 50x.html {root / usr/share/nginx/html/500.html;} # pass the PHP scripts to FastCGI server listening on 127.0.0.1 root html; fastcgi_pass 9000 location ~. Php$ {root html; fastcgi_pass 127.0.0.1 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

two。 Visit the a2 project

Summary

The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report