Nginx User Guide

Source: Internet
Author: User
Tags regular expression

1, running Nginx

You can run the Nginx command to open Nginx:

Nginx

If Nginx is already open, you can run Nginx command plus-s parameter to control Nginx operation.

Nginx-s Signal
Value of signal:

stop-Quick Close
quit-Graceful shut-off
reload-Reload the configuration file reopen-reopen the log file

For example, to wait for Nginx to complete the current request to close Nginx can use the following command

Nginx-s quit

After modifying the configuration file, you need to run the following command

Nginx-s Reload

2, simple configuration Nginx

Open the configuration file, usually in/etc/nginx/nginx.cnf, depending on your installation parameters.

Nginx.conf already contains a configuration case for a server block, but it's commented out. The following is a basic configuration of a server block

HTTP {
    server {
    }
}

Under the server block, you can configure some location to specify the local resources for the request URL

Location/{
    root/data/www;
}
The above indicates that all/the following access resources are under the/data/www folder


location/images/{
    root/data;
}
This indicates that all/images/path access images are under/data.


Then the above unified configuration is

server {
    
    Listen 8080;

Location/{root/data/www;}
location/images/{root/data;}
}

If I visit Http://localhost/images/example.png, Nginx will return the file directory/data/images/the example.png image below to return to the client

If I visit http://localhost/some/example.html, Nginx will return the file directory/data/www/the example.html image below to return to the client

Listen can not be specified, default is 8080

If configuration runs are modified during run time

Nginx-s Reload

If the configuration is authenticated, but you do not have access to the specified file as agreed, you can view the log files under/usr/local/nginx/logs or/var/log/nginx Access.log and Error.log


3, configuring the reverse proxy

server {location
    /{
        proxy_pass http://localhost:8080;
    }

    location/images/{
        root/data;
    }
}

PROXY_PASS specifies the path of the reverse proxy, and all compliant/paths are fetched to the http://localhost:8080 resource

For example, the resources accessed by http://192.168.1.100/some/example.html are actually http://localhost/some/example.html resources that are transparent to the client.


4, host name

The server name is specified by the server_name directive, which determines which server handles which request. Server_Name can be specified by wildcard characters.

server {
    listen       ;
    server_name  example.org  www.example.org;
    ...
}

server {
    listen       ;
    server_name  *.example.org;
    ...
}

server {
    listen       ;
    server_name  mail.*;
    ...
}

server {
    listen       ;
    server_name  ~^ (? <user>.+) \.example\.net$;
    ...
}
When a request satisfies multiple host names at the same time, the preferred host name has the following order.

1, full name, exact name.

2, longest wildcard name "*.example.org" through *

3, longest wildcard name "mail.*" by * end

4, the first virtual host name to match the regular expression

Wildcard characters can only be used at the beginning and end of the hostname, "www.*.example.org" and "w*.example.org" are the wrong way of writing, if you need to match this pattern can be specified through regular expressions, such as "~^www\. +\.example\.org$ "and" ~^w.*\.example\.org$ ". A part of the asterisk proxy hostname "*.example.org" not only represents www.example.com but also represents www.sub.example.com: example.org can represent either example.org or *. example.org.

If you want to use a regular expression, the host name must begin with a tilde ~

server_name  ~^www\d+\.example\.net$;
If it is not a wave symbol ~ or, then it is considered to be an all-host name.

If the regular expression host name contains *, then it is considered a wildcard host name. ^ and $ are required and they are grammatical and logical requirements.

Can be referenced by a regular capture in a later variable

server {
    server_name   ~^ (www\.)? (?<domain>. +) $;

    Location/{
        root   /sites/$domain;
    }
}
Regular expression captures can support the following syntax

?<name> Perl 5.10 Compatible syntax, supported since PCRE-7.0
?' Name Perl 5.10 Compatible syntax, supported since PCRE-7.0
? P<name> Python compatible syntax, supported since PCRE-4.0
Regular captures can also be obtained by numerical parameters

server {
    server_name   ~^ (www\.)? (.+)$;

    Location/{
        root   /sites/$;
    }
}
The $ match matches the regular expression in the second parenthesis.

Mixed host Name

server {
    listen       ;
    server_name  example.org  www.example.org  "";
    ...
}
If Nginx does not have a server module that can match the requested URL hostname, the default is to return an empty hostname in response to the request.

If the user is accessed via IP, this server_name can configure the IP hostname to respond to the request

server {
    listen       ;
    server_name  example.org
                 www.example.org
                 ""
                 192.168.1.1
                 ;
    ...
}
-Represents the hostname of the host name that represents all the errors

server {
    listen  default_server;
    server_name  _;
    return       444;
}

At some point you may visit *.example.com but the packages are good www.example.com and example.com and the two visits are frequent, preferably configured like this

server {
    listen       ;
    server_name  example.org  www.example.org  *.example.org;
    ...
}
And not like that.

server {
    listen       ;
    server_name  . example.org;
    ...
}

If the host name is too long, you need to modify the parameters in the HTTP module

Server_names_hash_bucket_size
The value of this parameter can be 32 or 64, depending on the size of your CPU cache stack

If you set him to 32, but your server name is very long such as: too.long.server.name.example.org, then it will error

Could not build the Server_names_hash, you
should increase server_names_hash_bucket_size:32
You have to set the parameter to twice times.


HTTP {
    server_names_hash_bucket_size  ;
    ...

If you configure too many host names, you will be reported with this error.

Could not build the Server_names_hash, you
should increase either server_names_hash_max_size:512
or Server_ Names_hash_bucket_size:32
The workaround is to set the server_names_hash_max_size as much as possible, if the configuration does not work, or the configuration causes Nginx to boot for too long, then increase the server_names_hash_bucket_ The value of size

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.