Nginx series--03http section basic configuration and virtual host

Source: Internet
Author: User
Tags response code

HTTP protocol Configuration format
http {    ...    ...:各server的公共配置    server {        ...    }:每个server用于定义一个虚拟主机;    server {        ...        server_name         root        alias        location [OPERATOR] URL {            ...            if CONDITION {                ...            }        }    }}

Unlike httpd, which has a default host, Nginx must define a virtual host using the server directive.

HTTP protocol Configuration

All configurations are configured in/etc/nginx/nginx.conf if not indicated

1. Socket-related configuration (1) server {...}
    • Role: Configure a virtual host
    • Example:
      server {listen address[:port] | port;    server_name SERVER_NAME;    root /PATH/TO/DOCUMENT_ROOT;}
(2) Listen

Syntax :
Listen port|address[:p ort]|unix:/path/to/socket_file
Listen address[:p ort] [default_server] [SSL] [HTTP2 | spdy] [backlog=number] [rcvbuf=size] [sndbuf=size]
Context :
Can only be placed in the server configuration segment
explanation :

    • port|address[:p ort]|unix:/path/to/socket_file: Indicates the way to listen to the address and port, if the IP does not indicate that the default is native all IP, if the port is not indicated is 80 port. If both are omitted, then listen on the 80 port of all IP on this machine.
      Unix:/path/to/socket_file: Indicates that through the SOCKET file, it can only be used for native communication.
    • Default_server: Indicates the virtual host that handles the request by default, indicating the virtual host that handles the request by default when multiple virtual hosts are listening on the same IP and port.
    • [SSL] [HTTP2 | spdy]: Indicates the protocol used when the client requests the resource.
    • [Backlog=number]: The listener socket when processing the request, if processing does not come, will be too late to process the request into the backup queue. The backlog indicates the size of the backup queue.
    • [Rcvbuf=size]: The listening socket accepts buffer size.
    • [Sndbuf=size]: Listen for socket send buffer size.
      Example Configuration :
      Listen Default_server
      Listen 127.0.0.1:80 SSL backlog=512
(3) Root

Syntax :
Root PATH
Context :
HTTP, server, location, if in location
Placing in a different context implies a difference in the scope of validity.
explanation :
Indicates that the user accesses the resource when it corresponds to the host root directory.
Example :
Root/var/www/vhost;
If a user accesses a URL of http://192.168.239.131/index.html, it gets the/var/www/vhost/index.html resource on the host. This means that the/index.html//var/www/vhost path on the host is represented in the URL. If the path specified by root does not exist, the grammar is detected without error. If there is a root configuration in multiple contexts, the scope is small in effect.

(4) server_name

Specifies the host name of the virtual host, which can be specified as multiple, separated by whitespace characters
Supports wildcard arbitrary length of any character; server_name . baidu.com Www.baidu. . Based on
Supports ~ starting characters for regular expression pattern matching; server_name ~^www\d+.baidu.com$
Matching mechanism: The first is the exact match, then the left
match, then the right * match, and finally the regular expression.

Virtual Host Configuration Example
  • IP-based virtual host
    The Node1 node has two ip,192.168.239.131 and 192.168.239.132. Different IP snooping on the same port, constituting the IP-based virtual host
    The configuration file is:

    http {server {    listen 192.168.239.131:80 ;    server_name node1;    root /var/www/vhost/131;}server {    listen 192.168.239.132:80 ;    server_name node1;    root /var/www/vhost/132;}}

    ~]# mkdir/var/www/vhost/{131,132}-PV
    ~]# echo "131 virtual Host" >/var/www/vhost/131/index.html
    ~]# echo "virtual Host" >/var/www/vhost/132/index.html
    ~]# nginx-t
    ~]# Nginx-r Reload

  • Port-based virtual host
    Different virtual hosts listening on different ports on the same IP address
    Configuration file/etc/nginx/nginx.conf in HTTP-side configuration
    http {server {    listen 192.168.239.131:80;    server_name node1;    root /var/www/vhost/80;}server {    listen 192.168.239.131:808;    server_name node1;    root /var/www/vhost/808;}}

  • Hybrid virtual host based on IP and port
    The virtual host listens on different IP and different ports
    Configuration file/etc/nginx/nginx.conf in HTTP-side configuration
    http {server {    listen 192.168.239.131:80;    server_name node1;    root /var/www/vhost/131_80;}server {    listen 192.168.239.132:808;    server_name node1;    root /var/www/vhost/132_808;}}

  • FQDN-based virtual host
    The virtual host listens on the same IP and port, different from the domain name when it is used to access the virtual host. Nginx will make the request to that virtual host by requesting the value of host in the header.
    Configuration file Configuration/etc/nginx/nginx.conf
    http {server {    listen 192.168.239.131:80;    server_name vhost1;    root /var/www/vhost/vhost1;}server {    listen 192.168.239.131:80;    server_name vhost2;    root /var/www/vhost/vhost2;}}


    Access results

2. Related to Path mapping (1) Location

Grammar
Location [= | ~ | ~* | ^~] URI {...}
Explain
Used to implement path mappings from URIs to file systems; Ngnix checks all the defined location based on the URI requested by the user, finds a best bet, and then applies its configuration. There can be more than one location in a server, and location can be nested. However, it is important to note that the "=" is nested when the exact match is made, and the NGINX-T will error when the syntax is detected. There are several ways to match URIs:

=:对URI进行精确匹配;精确匹配一旦匹配到,URI匹配机制就会关闭,起到了加速匹配的作用location = / {...此种配置只有http://192.168.239.131/ 能够匹配到,而http://192.168.239.131/index.html 不能匹配到,不然怎么称之为精确匹配呢。}^~:对URI左半部分做匹配检测,不区分大小写。此匹配一旦匹配到就会终止匹配,不会进行正则表达式匹配。~:正则表达式匹配,区分大小写。~*:正则表达式匹配,不区分大小写。不带符号:匹配起始于此uri的所有URI。

Since there are multiple ways of matching, there must be a priority problem. Match priority is: =,^~,~/~*, without symbol;
Priority example

location = / {    [ configuration A ]}location / {    [ configuration B ]}location /documents/ {    [ configuration C ]}location ^~ /images/ {    [ configuration D ]}location ~* \.(gif|jpg|jpeg)$ {    [ configuration E ]}

If our access URI is "/", it will be matched to configuration A; If the access URI is "/index.html", it will be matched to configuration B; If we access the URI "/document/index.html", it will match to configuration C; If we access the URI "/ Images/1.jpg ", will match to configuration D; If the access URI is"/document/1.jpg ", it will be matched to configuration E.
Root and location simultaneous configuration examples
Configuration file Configuration/etc/nginx/nginx.conf

server {    listen 80;        server_name node1;    location /admin/ {            root /var/www/vhost;        }}

Then ask, http://192.168.239.131/admin/will get the host/var/www/vhost/index.html file, or/var/www/vhost/admin/index.html?

As can be seen from the results, the path specified by root is equivalent to "/admin/" in the Left "/", so get the host resource path to "/var/www/vhost/admin/index.html". The right slash in the path indicated by root is optional, that is,/var/www/vhost and/var/www/vhost/. However, it is important to note that/admin/and/admin are different in the location. We can look at the following example:

http {    server {            listen 80;          server_name node1;          location /admin {              root /var/www/vhost/;          }     }}


From what we can see, the user accesses the URI as/admin, the server returns a 301 temporary redirect, the redirected URI is/admin/, and then the browser initiates the request again, requesting a URI of/admin/. We may not need this feature in reverse proxy scenarios, so we can use exact match

http {    server {            listen 80;          server_name node1;          location = /admin {              root /var/www/vhost/;          }     }}
(2) Alias

Grammar
Alias Path;
Explain
Define the path alias and replace the established location with the path that alias has made. Can only be placed in the location, not with the root command.
Example

http {    server {        listen 80;          server_name node1;        location /admin/ {              alias /alias/;        }    }}


You can see that the path specified by alias is relative to the root file system.
Also note that the path in the location and alias's path right slash to exist or not exist, the resource will not be accessible. For example:

location /admin {    alias /alias/;}或location /admin/ {    alias /alias;}都是错误的,不可取
(3) Index

Usage
Index file ... ;
Explain
Used to specify the default access resource, available in http,server,location. When the path that is used to access the resource corresponds to a directory, the file that accesses this directory by default
Example

http {    server {                    listen 80;                    server_name node1;                    location / {                            root /var/www/vhost/;                            index a.html;                    }    }}

(4) Error_page

Grammar
Error_page code ... [=[response]] URI;
Explain
Returns a specified URI for a specific error. Code corresponds to a specific error. =response, indicating the status code returned to the user.
Example 1
We will be 404 error directed Baidu page

http {    server {        listen 80;        server_name node1;          location / {              root /var/www/vhost/;              error_page 404 http://www.baidu.com;      }    }}


Example 2
In the normal online environment, we will have the corresponding error page back, not the above to redirect to the Baidu page. And in the online environment our configuration is often the case

http {    server {            listen *:80;                server_name node1;                locastion / {                    root /var/www/vhost/;                        error_page 404 /404.html                }                location /404.html {                    root /etc/nginx/error_pages/;                        index 404.html;                }        }}


Access a page that does not exist.

Example 3--returns a specified response code

http {    server {            listen *:80;                server_name node1;                locastion / {                    root /var/www/vhost/;                        error_page 404 =200 /404.html                }                location /404.html {                    root /etc/nginx/error_pages/;                        index 404.html;                }        }}

Note: Example 1 and example 2,3 are different, example 1 is a redirect, and the example 2,3 is nginx internally re-made path mapping. So one example of using =reponse is the effect of not specifying a response code.

Reference

Https://nginx.org/en/docs/http/ngx_http_core_module.html

Nginx series--03http section basic configuration and virtual host

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.