server { listen 80; server_name www.123.com; Location / {proxy_pass http://127.0.0.1:8080; index index.html index.htm index.jsp; }}Copy the code

If no port number is added, the default port number is 80. Therefore, you will be directed to 127.0.0.1:8080 when you access this domain name

(1), listen,

This command is used to configure network listening. There are three configuration syntax structures:

1. Configure the LISTENING IP address

listen address[:port] [default_server] [setfib=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [deferred]
    [accept_filter=filter] [bind] [ssl];Copy the code

2. Configure listening ports

listen port[default_server] [setfib=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] 
    [deferred] [bind] [ipv6only=on|off] [ssl];Copy the code

3. Configure UNIX Domain sockets

listen unix:path [default_server]  [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] 
    [deferred] [bind] [ssl];Copy the code

The above configuration seems to be more complex, in fact, it is relatively simple to use:

1 listen *:80 | *:8080 Listen on all ports 80 and 8080
2 listen  IP_address:port   Listen for the specified address and port number
3 listen  IP_address     # listen on all ports of the specified IP address
4 listen port     Listen for all IP connections on this portCopy the code

Here’s what each option means:

1. Address: indicates the IP address. For an IPV6 address, use brackets [], for example, [fe80::1].

2. Port: indicates the port number. If only an IP address is defined but no port number is defined, port 80 is used.

3. Path: indicates the socket file path, such as var/run/nginx.sock.

4. Default_server: identifier, set this virtual host to the default host of address:port. (The default directive was used before nginx-0.8.21)

Setfib =number: nginx-0.8.44 setFIB =number: nginx-0.8.44

Backlog =number: Sets the maximum number of pending network connections allowed by the listener listen(). Default is -1 on FreeBSD and 511 on other platforms.

7. Rcvbuf =size: Set the size of the listening socket receiving cache.

Sndbuf =size: set the size of the listening socket send buffer.

9, Deferred: identifier, set Accept () to deferred mode.

10, accept_filter=filter: set the listening port to filter all requests. The filtered content cannot be received or processed. This command is only valid for FreeBSD and NetBSD 5.0+ platforms. Filter can be set to dataready or Httpready.

Nginx uses a separate bind() identifier for this address:port. In general, for multiple connections with the same port but different IP addresses, the Nginx server will use only one listener and bind() for all connections with the same port.

SSL: identifier that sets the session connection to use SSL mode. This identifier is related to the HTTPS service provided by the Nginx server.

(2), server_name

This command is used to configure virtual hosts. It is usually divided into the following two types:

1. Name-based virtual host configuration

The syntax is as follows:

server_name name ... ;Copy the code

For names, there can be only one or more names separated by Spaces. Each name consists of two or three paragraphs separated by “. Separated.

server_name 123.com www.123.comCopy the code

2. The wildcard character * can be used, but the wildcard character can only be used at the beginning or end of a three-part character, or at the end of a two-part character.

server_name *.123.com www.123.*Copy the code

You can also use regular expressions. You can start the regular expression string with ~.

server_name ~^www\d+\.123\.com$;Copy the code

The expression “~” matches the regular expression, starting with WWW (” ^ “indicates the beginning), followed by a number between 0 and 9, followed by”.123.co “, and followed by “m” ($indicates the end).

The priorities of the previous matches are as follows:

Server_name is correctly matched. 2. The wildcard is successfully matched at the start. 3Copy the code

2. Virtual host configuration based on IP address

The syntax is the same as domain-based matching, and you don’t need to worry about wildcards and regular expressions.

Server_name 192.168.1.1Copy the code

(3), the location,

This directive is used to match urls.

The syntax is as follows:

1 location [ = | ~ | ~* | ^~] uri {
2 
3 }Copy the code

1. = : Before a URI without a regular expression is used, the request string must match the URI strictly. If the match is successful, the search is stopped and the request is processed immediately.

2. ~ : Indicates that the URI contains regular expressions and is case sensitive.

3. ~* : Indicates that the URI contains regular expressions and is case insensitive.

4, ^ ~ : Before using urIs without regular expressions, the Nginx server is required to find the location with the highest degree of matching between the identification URI and the request string, and immediately use this location instead of using the regular URI in the Location block to match the request string.

5, / : universal match, any request will be matched

Note: If the URI contains a regular expression, it must be marked with ~ or ~*.

Matching rules: first exact match =-, then match ^~- starting with xx, then regular match in order of file -, and finally give/universal match.

When a match succeeds, the request is stopped and processed according to the current matching rule.

(4), proxy_pass

This directive is used to set the address of the proxied server. The value can be a host name, IP address and port number.

The syntax is as follows:

proxy_pass URL;Copy the code

The URL is the IP address of the proxy server, including the transport protocol, host name, IP address, port number, and URI.

proxy_pass  http://www.123.com/uri;Copy the code

(5), the index

This directive is used to set the default home page of a website.

Grammar:

index filename ... ;Copy the code

The following file names can have multiple names separated by Spaces.

index  index.html index.jsp;Copy the code