Configure virtual hosts in nginx

Source: Internet
Author: User

 

NginxConfiguration Virtual Host[Sorting] ultra-detailed

11:58:13 | category:
Linux Service Security | font size subscription

NginxFirst, it determines which server handles a request.

That is, the host value we see when we open httpwatch.

Server {

Listen 80;

SERVER_NAME
Nginx. Org www.Nginx. Org;

...

}

Server {

Listen 80;

SERVER_NAME
Nginx. Net www.Nginx. Net;

...

}

Server {

Listen 80;

SERVER_NAME
Nginx. Com www.Nginx. Com;

...

}

In this way, we can configure three domain names. That is, three domain names are bound to the same IP address. If a domain name does not match, a default domain name is defined.

Server {

Listen 80 default_server;

SERVER_NAME
Nginx. Net www.Nginx. Net;

...

}

We can handle such domain names in this way

Server {

Listen 80 default_server;

SERVER_NAME
Www.Nginx. Net; // you must enter


Return 444;

}

 

Domain Name and IP address-basedVirtual Host

Server {

Listen 192.168.1.1: 80;

SERVER_NAME
Nginx. Org www.Nginx. Org;

...

}

Server {

Listen 192.168.1.1: 80;

SERVER_NAME
Nginx. Net www.Nginx. Net;

...

}

Server {

Listen 192.168.1.2: 80;

SERVER_NAME
Nginx. Com www.Nginx. Com;

...

}

 

Now, the virtual machine configuration has been completed!

 

Example:

Server {

Listen 80;

SERVER_NAME
Nginx. Org
Www.Nginx. Org;

Root/DATa/WWW; // This is a bit equivalent to the root directory in Resin

 

Location /{

Index index.html index. php;

}

 

Location ~ * \. (GIF | JPG | PNG) $ {

Expires 30d;

}

 

Location ~ \. Php $ {


Fastcgi_pass localhost: 9000;

Fastcgi_param script_filename

$ Document_root $ fastcgi_script_name;

Include fastcgi_params;

}

}

The location is "/", which indicates that it can match any request.

Oh! The original location is used to test the URI!

 

Experiences and notes:

Our server is the host that is configured.

Location is the URI configuration.

 

For example: http://www.sina.cn/blog/index.php, the host here is www.sina.cn

Uri is our/blog/index. php value.

 

A "/logo.gif" request first matches the character location "/", and then matches the regular expression "\. (GIF | JPG | PNG) $ "match. Therefore, it is processed by the character location. Command "root"
/DATa/www"Direct the request to a file "/DATa/www/logo.gif"And the file will be sent to the client.

 

 

Oh, the original role of root is actually a concept with document-root in resin!

 

 

A "/index. php" request is also the first character location "/"
Then, it is matched by the regular expression "\. (PHP) $. Therefore, it is processed by the character location, and the request is sent through a FastCGI listener on localhost: 9000
Server. "fastcgi_param"
Set the script_filename parameter of FastCGI to "/da ".Ta/www/index. php ", FastCGI Server
Execute this file. $ document_root variable with the value equal to "root"
Command, $ fastcgi_script_name variable equals URI
The request value, that is, "/index. php ".

 

Notes:NginxIs to let the client program find the directory location of the file. How can we deal with this problem? The backend must handle it.

 

 

A "/about.html" request is only matched by the character location,
Therefore, it is processed by this location. Use "root/DA"Ta/www"
The request is forwarded to "/DATa/www/about.html ",
And the file will be sent to the client.

 

 

Understand!

 

Note: location must be sequential. By Location/
Processing allows the client to find the desired file. Then look down to see if there are any matching location items. If it is like a PHP file, there will be!

Threw a FAST-CGI Handler

 

 

 

 

Summary:

Experiences and notes:

Our server is the host that is configured. Multiple Domain Names are definedVirtual HostYou can.

Location is the URI configuration.

For example: http://www.sina.cn/blog/index.php, the host here is www.sina.cn

Uri is our/blog/index. php value.

 

 

Location is matched by multiple parties. Example:

Location /{

Index index.html index. php;

}

 

Location ~ * \. (GIF | JPG | PNG) $ {

Expires 30d;

}

If I request an abc.gif image, the first UIR will first locate the image location, and then the second uri Will process it to get the expiration time.

Of course, the following options are available in location.

1. This is basically used for last. Indicates that the completed rewrite does not match the subsequent rules.

2. The break terminates rewrite and does not continue matching.

3. Redirect returns the HTTP status 302 of the temporary redirection.

4. Permanent returns the permanently redirected HTTP status 301

Note: The original URL supports regular expressions, and the rewritten URL does not support regular expressions.

Location /{

Index index.html index. php;

Break;

}

The subsequent expiration limit does not take effect.

 

Manually test: only handle static files

Site Directory:

Virtual Host1: The Directory is placed under D: \ myweb \ proj3

Virtual Host2: directory under D: \ myweb \ proj4

Server {

Listen 80;

SERVER_NAME www.aaa.com;

Root D: \ myweb \ proj3;

Location /{


Index index.html index.htm;

}

Location ~ * \. (GIF | JPG | PNG) $ {


Expires 30d;

}

}

Server {

Listen 80;

SERVER_NAME www.bbb.com;

Root D: \ myweb \ proj4;

Location /{


Index index.html index.htm;

}

Location ~ * \. (GIF | JPG | PNG) $ {


Expires 30d;

}

}

OK! TwoVirtual Host. The domain name can be resolved as soon as it comes.

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.