I. Application scenario description
To meet the needs of development colleagues, Nginx in the development environment can use different root paths based on different domain names.
For example, if the domain name is linuxidc4.linuxidc.com, the root path is/data/public/linuxidc4.
Linuxidc5.linuxidc.com, the root path is/data/public/linuxidc5.
Linuxidc6.linuxidc.com, that is, the root path is/data/public/linuxidc6.
Solution 2
The code is as follows: |
Copy code |
Server { Listen 80; Server_name * .linuxidc.com; Set $ linuxidc_name linuxidc4; If ($ host ~ "Linuxidc5 ") { Set $ linuxidc_name linuxidc5; } If ($ host ~ "Linuxidc6 ") { Set $ linuxidc_name linuxidc6; } Root/data/public/$ linuxidc_name /; Client_max_body_size 5 m; Autoindex off; Location /{ If (! -E $ request_filename ){ Rewrite/(. *)/index. php last; } Index. php; Autoindex off; } Location ~ \. Php $ { Fastcgi_pass 127.0.0.1: 9000; Fastcgi_index index. php; Fastcgi_param SCRIPT_FILENAME $ document_root/$ fastcgi_script_name; Include fastcgi_params; } } |
Set the variable $ linuxidc_name here and use this variable in the root path.
3. Nginx instructions
1. set command
Defines a variable and assigns a value to the variable.
Scope: if, location, server
For example
Set $ linuxidc_name linuxidc4;
2. if command
If (condition ){...}
Scope:
For example:
If ($ host ~ Linuxidc-taiwan-5 ")
{
Set $ linuxidc_name linuxidc5;
}
The if command is used to check whether a condition is met. if the condition is met, content in braces is executed. The if command does not support nesting, and does not support multiple & or |
The following conditions can be specified:
1) variable name
2) variable comparison can use = (equal to) and! = (Not equal)
3) regular expression matching can be used ~ (Case sensitive) and ~ * (Case-insensitive matching)
!~ And !~ * Indicates no matching.
4)-f and! -F is used to determine whether a file exists.
5)-d and! -D is used to determine whether a directory exists.
6)-e and! -E is used to determine whether a file or directory exists.
7)-x and! -X is used to determine whether a file can be executed.
3) Nginx built-in variables
$ Host request host name
$ Request_filename request file name
4) rewrite command
Rewrite regex replacement flag;
Used to redirect URLs
If (! -E $ request_filename ){
Rewrite/(. *)/index. php last;
}
The last rewrite entry is a tag bit. Nginx supports the following tags:
Last indicates rewrite is completed.
Permanent returns 301 permanent redirection. The browser's address bar displays the redirected URL.
After a break rule is matched, it terminates matching of other rules.
Redirect returns 302 temporary redirection
Last and break are used to redirect the URL. The address on the browser is not changed, but the location on the server is changed. Permanent and redirect are used for URL jump. The browser address bar displays the jump URL.
The last command must be used when the alias command is used, and the break command must be used when the proxy_pass command is used.