Home >Operation and Maintenance >Nginx >Location matching and rewrite jump methods in Nginx
Rewrite module set instruction: create new variables and assign values to them2. Rewrite execution sequence①Execute the rewriter instruction in the server block②Execution Location matching③Execute the rewrite command in the selected location3. Rewrite jump scenarioAdjust the URL browsed by the user to look more standardized and in line with development and Needs of product personnelIn order to allow search engines to search website content and provide better user experience, companies will disguise dynamic URL addresses as static addresses to provide servicesAfter the URL is changed to a new domain name, the old The access will jump to a new domain name; for example, access to JD.com's 360buy.com will jump to jd.com Certain business adjustments on the server, such as URL based on special variables, directories, and client information Adjustment, etc.4. Grammar and flg tag descriptionGrammar rewrite
replacement: Indicates the content after the jump flag: Indicates the flag mark supported by rewrite flag mark description:
location /images/abc/1.html {} Matches the /images/abc/1.html file. If compared with the regular ~ /images/abc/1.html, the regular priority is higher High priority summary: (location = full path) > (location ^~ path) > (location ,* regular sequence) > (location part starting path) > ; (location /) 5. Matching rule definition ① The first required rule: directly matches the website root. Accessing the website homepage through the domain name is more frequent. Use This will speed up the processing, for example, the official website
It can be a static homepage, or it can be forwarded directly to the back-end "application server" - PHP, Apache (the application server is shown in the figure below)
##
location / { root html; index index.html index.html; }②The second required rule: Processing static file requests, which is the strength of nginx as an http server (①static request processing capability②high concurrency processing capability③low resource consumption)There are two configuration modes, directory matching or suffix matching, choose one or use them together
location ^~ /static/ { root /webroot/static/; } location ~* \.(html|gif|jpg|jpeg|png|css|js|ico)$ { root /webroot/res/ ; }③The third required rule: is a general rule, such as using To forward dynamic requests with .php and .jsp suffixes to the back-end application serverNon-static file requests are dynamic requests by default (jump/reverse proxy)
upstream tomcat_server { 192.168.126.12:80 192.168.126.23:80 }
location / { proxy_ pass http://tomcat_server;Summary:
1. Compare rewrite and location
Same points: both implement jumpsDifferences: rewrite changes the path to obtain resources within the same domain name Location is to control access or reverse proxy to a class of paths. You can also proxy_pass to other machines2.rewrite will be written in the location, and the execution sequence
#3. How are the location priorities arranged?
Match a specific file(location = full path) > (location ^~ full path) > (location ~* full path) > (location ~ complete path Path)> (location /)Use directory to match to access a file(location = directory) > (location ^~ directory) > (location ~ directory) > (location ~* directory)> (location /)4. Why do files and directories only change in case insensitivity?
Regular expression Formula: The purpose is to match files as accurately as possible - try to match as accurately as possible, case-sensitive and accurate, and not distinguish directories - try to match more accurately Exact match, case-sensitive, more precise, higher priorityThe above is the detailed content of Location matching and rewrite jump methods in Nginx. For more information, please follow other related articles on the PHP Chinese website!