Nginx Virtual machine host uses different root paths depending on the domain name

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.