Nginx Learning (4)

Source: Internet
Author: User
Tags new set response code

Configuring NGINX Plus as a Web Server

At a-level, configuring NGINX Plus as a Web server is a matter of defining which URLs it handles and how it processes HTTP requests for resources at those URLs. At a lower level, the configuration defines a set of virtual servers that control the processing of requests for Particula R domains or IP addresses.

Setting up Virtual Servers

If a port is omitted, the standard port is used. Likewise, if an address was omitted, the server listens on all addresses.

If there is several servers that match the IP address and port of the request, NGINX Plus tests the request ' s Host header field against the server_name directives in the server blocks.

The parameter to server_name can is a full (exact) name, a wildcard, or a regular expression. A wildcard is a character string that includes the asterisk ( * ) at its beginning, end, or both; the asterisk Matches any sequence of characters.

If Host The header field does not match a server name, NGINX Plus routes the request to the default server for the port O n which the request arrived. The default server is the first one listed in the nginx.conf file, unless you include the default_server parameter to the listendirective to explicitly designate a server as the default.

Configuring Locations

There is types of parameter to the location directive: prefix Strings (pathnames) and regular expressions . For a request URI to match a prefix string, it must start with the prefix string.

A Regular expression is preceded with the tilde ( ~ ) for case-sensitive matching, or the Tilde-asterisk ( ~* ) for case-insensitive matching.

To find the location of the matches a URI, NGINX Plus First compares the URI to the locations with a Prefi X String. It then searches the locations with a regular expression.

Todo

server {    /images/ {        /data;    }     / {        proxy_pass http://www.example.com;     }}

The root directive specifies the file system path in which to search for the static files to serve. The request URI associated with the "appended" to the "the path to obtain" The full name of the "the static file to serve". In the example above, with response to a request For/images/example.png, NGINX Plus delivers the file/data/images/example. Png.

Returning specific Status Codes

The first parameter of is return a response code. The optional second parameter can be is the URL of a redirect (for codes,, 301 302 303 and 307 ) or the Text to return in the response body.

location/permanently/moved/URL {    return301 http://  Www.example.com/moved/here;}
Rewriting URIs in Requests

A request URI can be modified multiple times during request processing through rewrite the use of the directive, which have O Ne optional and required parameters. The first (required) parameter is the regular expression, that the request URI must match. The second parameter is the URI of the substitute for the matching URI. The optional third parameter is a flag that can halt processing of further rewrite directives or send a re Direct (Code 301 or 302 ).

About Last and break

(Ref:https://serverfault.com/questions/131474/nginx-url-rewriting-difference-between-break-and-last)

Different sets of rewrite rules for different locations. When rewrite module meets last , it stops processing the current set and the rewritten request is passed once again 1> to find the appropriate location (and the new set of rewriting rules). If The rule ends break with, the rewriting also stops, and the rewritten request is not passed to another locatio N.

That's, if there is, Locations:loc1 and LOC2, and there's a rewriting rule in loc1 that changes loc1 to LOC2 and end S last with, the request is rewritten and passed to location LOC2. If The rule ends break with, it'll belong to location Loc1.

Rewriting HTTP Responses

Sometimes need to rewrite or change the content in an HTTP response, substituting one string for another. You can use The sub_filter  directive to Define the rewrite to apply.

Another example changes the method from  http://  to http_s_://  and replaces the  localhost  address to the host name from The Request header field. The sub_filter_once  directive tells NGINX to Apply sub_filter  directives Span style= "COLOR: #ff0000" >consecutively within a location:

 location/ {sub_filter   " href=" http://127.0.0.1:8080/  "  " href= "https://$host/  "    ; Sub_filter   " img src="/http/ 127.0.0.1:8080/  " "  img src=" https://$host/  "  

Note that the part of the response already modified with the would not be sub_filter replaced again if another sub_filter ma TCH occurs.

Handling Errors

In the following example, when NGINX Plus cannot find a page, it substitutes code 301 for code 404 , and redirects the Client to http:/example.com/new/path.html. This configuration was useful when clients was still trying to access a page in its old URI.

location/old/path.html {    404 =301 http:/example.com/new/path.html;}

Nginx Learning (4)

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.