Nginx virtual hosts use different root Paths Based on different domain names
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 bkjia4.bkjia.com, the root path is/data/public/bkjia4.
Bkjia5.bkjia.com, the root path is/data/public/bkjia5.
Bkjia6.bkjia.com, that is, the root path is/data/public/bkjia6
Solution 2
Server
{
Listen 80;
Server_name * .bkjia.com;
Set $ bkjia_name bkjia4;
If ($ host ~ "Bkjia5 ")
{
Set $ bkjia_name bkjia5;
}
If ($ host ~ "Bkjia6 ")
{
Set $ bkjia_name bkjia6;
}
Root/data/public/$ bkjia_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 $ bkjia_name 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 $ bkjia_name bkjia4;
2. if command
If (condition ){...}
Scope:
For example:
If ($ host ~ Bkjia-taiwan-5 ")
{
Set $ bkjia_name bkjia5;
}
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.
-------------------------------------- Split line --------------------------------------
Deployment of Nginx + MySQL + PHP in CentOS 6.2
Build a WEB server using Nginx
Build a Web server based on Linux6.3 + Nginx1.2 + PHP5 + MySQL5.5
Performance Tuning for Nginx in CentOS 6.3
Configure Nginx to load the ngx_pagespeed module in CentOS 6.3
Install and configure Nginx + Pcre + php-fpm in CentOS 6.4
Nginx installation and configuration instructions
Nginx log filtering using ngx_log_if does not record specific logs
Nginx details: click here
Nginx: click here
This article permanently updates the link address: