A description of the application scenario
To develop the needs of colleagues, nginx in the development environment can use different root paths depending on the domain name.
For example, if the domain name is game4.xxx.com, use the root path as/data/public/game4
Game5.xxx.com, use the root path as/data/public/game5
game6.xxx.com, the root path is/data/public/game6
Two-way solution
server{ listen 80; server_name *.xxx.com; set $game _name game4; if ($host ~ "Game5") { set $game _name game5; } if ($host ~ "Game6") { set $game _name game6; } root /data/public/$game _name/; client_max_body_size 5m; autoindex off; location / { if (!-e $request _filename) { rewrite /(. *) /index.php last; } index 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; }}
This sets the variable $game_name, and then uses the variable in the root path
Introduction of three related NGINX instructions
1.set instruction
Used to define a variable and assign a value to the variable
Scope of If,location,server
such as the above
Set $game _name game4;
2.if instruction
if (condition) {...}
The scope of action is
Such as:
if ($host ~ "game-taiwan-5")
{
Set $game _name game5;
}
The IF directive is used to check if a condition conforms, and if the condition is met, the contents inside the curly braces are executed.
Reference Documentation:
Http://serverfault.com/questions/241005/nginx-change-root-directory-based-on-server-name
This article is from the Linux SA John blog, so be sure to keep this source http://john88wang.blog.51cto.com/2165294/1616538
Nginx Virtual machine host uses different root paths depending on the domain name