One: set up a virtual server
1. settings
http { server {
listen 127.0.0.1:8080;
server_name example.org www.example.org;
}
2. explanation
If more than one server matches the requested IP address and port, nginx server_name
tests the Request's host header domain based on the instructions in the server Block.
server_name
parameters can be full (exact) names, wildcard characters, or regular expressions. A wildcard character is a string whose beginning, end, or both contain an asterisk (), and an *
asterisk matches any sequence of Characters. Nginx uses Perl syntax for regular expressions; Use the tilde () before them ?
.
That is, server_name access to the above IP and port.
If the host header names are matched, Nginx chooses one by searching for the name in the following order and using the first match it finds:
- Exact name (full and accurate Name)
- The longest wildcard character starting with an asterisk, for example:
*.example.org
- The longest wildcard character ending with an asterisk, such as:
mail.*
- First matching regular expression (in the order in which they appear in the configuration File)
If the Host header field does not match the server name, Nginx routes the request to the default server that requests the arrival Port. The default server is nginx.conf
the first server listed in the file
3. A complete example
Demo configuration two virtual machines, corresponding domain names are: vhost1.com
andvhost2.com。
vhost1.com
The Site's home directory is/data/www/vhost1。
vhost2.com
The Site's home directory is/data/www/vhost2。
4.conf
server { listen 80; server_name vhost1.com www.vhost1.com; index index.html index.html; root /data/www/vhost1; access_log /var/log/vhost1.com.log;}server { listen 80; server_name vhost2.com www.vhost2.com; index index.html index.html; root /data/www/vhost2; access_log /var/log/vhost2.com.log;}
Two: Set Location
1. explanation
Nginx can send traffic to different agents or provide different files depending on the request Uri. These blocks are defined using directives that are placed in server
directives location
.
location
Directives have two types of parameters: a prefix string (pathname) and a regular expression. For the request URI to match the prefix string, you must start with the previous prefix string.
2. For example
The regular expression precedes a case-sensitive waveform symbol (), or a case-insensitive ~
waveform symbol ( ~*
).
The following example .html
.html
matches any location that contains a string or Uri.
location ~ \.html? { ... }
Use ^~
modifiers to give a higher priority to regular expressions
3. The exact logic for choosing where to process the request is given below
- Tests the prefix string for all Uris.
=
The (equals) modifier defines an exact match between the URI and the prefix string. If an exact match is found, the search stops.
- If the
^~
(caret) modifier pre-adds the longest match-prefix string, the regular expression is not checked.
- Stores the longest matching prefix string.
- Test the URI according to the regular Expression.
- Break the first matching regular expression and use the appropriate Location.
- If there is no regular expression match, the location corresponding to the stored prefix string is Used.
=
A typical use case for a modifier is a /
(forward slash) Request. If the request /
is frequent, the =/
parameter specified as location
the instruction is accelerated because the search match stops after the first Comparison.
Three: return a specific status code
1. Some web site URIs need to immediately return a response with a specific error or redirect code
No status Code found:
location /wrong/url { return 404; }
2. details
The first parameter returned is the response Code. The optional second parameter can be a redirected URL (code 301
, 302
303
and 307
) or return text in the response body.
location /permanently/moved/url { return 301 http://www.example.com/moved/here; }
Four: Rewrite URL requests
1.rewrite instruction
The request URI is modified multiple times during request processing with an optional parameter and two required Parameters.
The first (required) parameter is a regular expression that the request URI must Match.
The second parameter is the URI used to replace the matching Uri.
The optional third parameter is a flag that can stop the processing of further rewrite instructions or send redirects (code 301
or 302
)
location /users/ { rewrite ^/users/(.*)$ /show?user=$1 break; }
2. Rewrite combined with the return instruction
server { ... rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last; rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last; return 403; ... }
3. Interrupt processing Instructions
last
-stops execution of the rewrite instruction in the current server or location context, but Nginx searches for a location that matches the overridden uri, and applies any rewrite instructions in the new location (the URI can be changed again, and the match continues down).
break
-like break
An instruction, stops processing the rewrite instruction in the current context and cancels the search for a location that matches the new Uri. location
directives in the new location () block rewrite
do not Execute.
V: Re-http Request
1. explanation
Sometimes you need to override or change the content in the HTTP response to replace one string with Another.
You can use sub_filter
directives to define the overrides to Apply. The directive supports variables and substitution chains, making it possible to make more complex changes
location / { sub_filter /blog/ /blog-staging/; sub_filter_once off; }
2.sub_filter_once directive
Tells Nginx to location
apply pseudo-directives in a single position () sub_filter
location / { sub_filter ‘href="http://127.0.0.1:8080/‘ ‘href="http://$host/‘; sub_filter ‘img src="http://127.0.0.1:8080/‘ ‘img src="http://$host/‘; sub_filter_once on; }
如果发生另一个sub_filter
匹配,则使用sub_filter
修改的响应部分将不再被替换。
3. error_page
directives
error_page 404 /404.html;
Specifies 404
the page () to return the page error code /404.html
.
When Nginx cannot find the page, it replaces the code with 301
code and redirects the 404
client to http:/example.com/new/path.html
.
location /old/path.html { error_page 404 =301 http:/example.com/new/path.html; }
Nginx Configuration Web server