Let's briefly sort out the configuration of server and location in nginx. For example, URL: www.mask_dev2.com:9999/login/ The first half of the server pipe, that is: www.mask_dev2.com:9999 Location controls the second half, namely: /login/ One nginx can be configured with multiple servers. Each server can be configured with multiple locations. The first half of the URL controls which server is selected, and the second half controls which location is selected, which ultimately determines where the request is made. Server configuration server { listen 9999; server_name www.mask_dev2.cn; location / { default_type text/html; content_by_lua ' ngx.say("<p>first</p>") '; } } server { listen 9999; server_name www.mask_dev2.*; location / { default_type text/html; content_by_lua ' ngx.say("<p>second</p>") '; } } server { listen 9998; server_name _; location / { default_type text/html; content_by_lua ' ngx.say("<p>third</p>") '; } } First of all, the address requested for nginx must be the server where the requested nginx is located, that is, the IP address is fixed. In other words, it doesn't matter what server_name is, it refers to the current server. So how does the current server correspond to multiple domain names? This only needs to be added in the corresponding DNS server. For example, temporarily treat the local machine as a DNS server and modify the hosts
Server matching order The matching priority of server_name and host is as follows: 1. Exact match If none of them match 1. Prefer default or default_server after the listen configuration item Location configuration After finding the server, find the specific location server { listen 9998; server_name _; location = / { #Rule A } location = /login { #Rule B } location ^~ /static/ { #Rule C } location ~ \.(gif|jpg|png|js|css)$ { #Rule D } location ~* \.png$ { #Rule E } location !~ \.xhtml$ { #Rule F } location !~* \.xhtml$ { #Rule G } location / { #Rule H } Syntax rules: location [=||*|^~] uri { … }
First, it matches =, then ^~, then the regular expression in the order in the file, and finally the general match of /. When a match is successful, stop matching and process the request according to the current matching rules. But it is generally not that complicated, there are 3 points.
#Directly match the website root. It is more frequent to access the website homepage through the domain name. Using this will speed up the processing, the official website said. #This is forwarded directly to the backend application server, or it can be a static homepage# The first required rule location = / { proxy_pass http://tomcat:8080/index } # The second mandatory rule is to process static file requests, which is the strength of nginx as an http server. # There are two configuration modes, directory matching or suffix matching, choose one or use both location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ { root /webroot/res/; } #The third rule is a general rule, which is used to forward dynamic requests to the backend application server. #Non-static file requests are dynamic requests by default, and you can grasp it according to your actual situation. #After all, some popular frameworks now rarely have .php and .jsp suffixes. location / { proxy_pass http://127.0.0.1:8080/ } Summarize For example, if you start the front-end system and the back-end system at the same time, you can use two servers (you can configure the host to be api, admin, or directly change the port), and each server has 3 locations to determine the specific page request. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. |
<<: MySQL 5.5.27 installation graphic tutorial
>>: Write a React-like framework from scratch
CSS 3 animation example - dynamic effect of Tab b...
The first parameter passing method is dynamic rou...
Table of contents Passing parameters between pare...
VMware Tools is a tool that comes with VMware vir...
background Today, I was browsing CodePen and saw ...
difficulty Two mask creation of svg graphics Firs...
Table of contents Class component event binding F...
Install mysql5.7.21 in the window environment. Th...
This article installs Google Input Method. In fac...
I just started working a few days ago and install...
Docker download address: http://get.daocloud.io/#...
During the crawler development process, you must ...
1. In the previous chapter, we learned that we ca...
This article shares the specific code for JavaScr...
Table of contents Preface Standard sfc writing me...