Nginx Learning Notes (ii)--building a Web server

Source: Internet
Author: User
Tags nginx server

Background introduction

Nginx since the birth of the 2.6 kernel supported by the Epoll model and then make the processing efficiency greatly increased by the majority of users favor, but in terms of Web services, Nginx has the functionality of Apache can be achieved, but in turn, this is the majority of scenes using Nginx as a reverse proxy and can not The reason to replace Apache. This article takes the nginx_1.12.1 version as an example to describe the configuration and usage of Nginx as a Web server.

Software Installation

Although Nginx has been included in the Epel source, it is still recommended to install it in a compiled manner, which is more flexible. The installation steps are:

1. Download the 1.12.1 source package from the Nginx website and extract it to the/USR/LOCAL/SRC directory.

# TAR-XF Nginx-1.12.1.tar.gz-c/usr/local/src/

2. Create a user to run the worker process. Nginx uses Master+worker mode to work, can only have 1 master processes, the worker process may have multiple, master is used to pass parameters and instructions to the worker, the worker is responsible for processing the customer request, The advantage of this approach is that 1 worker problems do not affect master.

# useradd-m-r-s/sbin/nologin Nginx

3. Select the feature you want to enable to compile the installation as required. Note: nginx can customize the log level, but if you want to enable debug level logging, you must turn on the--with-debug option at compile time, or even if you set the debug level to no effect, and if you need to compile a third-party module, you need to use the--add-module option.

# cd/usr/local/src/

#./configure--prefix=/usr/local/nginx/--user=nginx--with-threads--with-file-aio--with-http_ssl_module-- With-http_gzip_static_module--with-http_gunzip_module--with-http_stub_status_module--with-pcre--add-module=/ root/echo-nginx-module-0.60

# Make && make install

Nginx Master configuration file

Nginx configuration file is a nginx.conf file, from the structure of the global configuration segment and local configuration segment, in order to facilitate management, the recommended practice is to divide the main configuration segment and the local configuration segment into 2 files, the main configuration segment to optimize performance and configuration-related events, local configuration segment Configuration service-related content, in the main configuration segment using the include directive Use local configuration segments.

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9D/91/wKioL1mB-gWxq7QOAABfINFb1wc248.png "title=" 1.png "Style=" Float:none; "alt=" Wkiol1mb-gwxq7qoaabfinfb1wc248.png "/>

Worker_processes is the number of start worker processes, the recommended value is the number of Vcpus minus 1; Worker_connections is a worker process can maintain the number of connections by default to 1024, the total number of connections to the Nginx server = Number of workers * number of connections per worker; Worker_priority is the nice value of the set process, and all processes in the Linux system prioritize with nice values, and the nice range is between 20 and 19, and the smaller the value, the higher the priority The nice value for all of the default processes is 0;worker_rlimit_noffile is the maximum number of open files allowed for a worker process, and each socket needs to have a socket file that defaults to 1024. Here, the local configuration segment is stored separately in the server.conf file, which is referenced by the include directive.

Local Configuration segment

1.server Segment Definition

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/9D/91/wKiom1mB-gWQIB-tAAAKzT2MDcA820.png "title=" 2.png "Style=" Float:none; "alt=" Wkiom1mb-gwqib-taaakzt2mdca820.png "/>

The server segment is similar to the VirtualHost segment in Apache, where one HTTP segment can have multiple server segments listening on different sockets, listen as listening ports, and if you want to listen on other sockets you can pass IP behind listen: The port mode is set, server_name sets the host name of the service, the hostname can have multiple, and the wildcard wildcards regular expression (~) is supported, with the order of precedence:

(1) Do the exact match first: www.contoso.com;

(2) left wildcard match: *.contoso.com;

(3) Right wildcard match: www.contoso.*;

(4) Regular expression: ~^.*\.contoso\.com$;

If the hostname cannot match any of the above matches, the first server is matched by default, unless you set which server is Default_server.

2.location Segment Definition

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/9D/91/wKioL1mB-gaxBTI8AABEKJkdsLo041.png "title=" 3.png "Style=" Float:none; "alt=" Wkiol1mb-gaxbti8aabekjkdslo041.png "/>

The definition of the location segment in Nginx is very flexible, where the position represents the requested resource, followed by a directory or a file, and the absolute path to the file system depends on the root or alias setting. Similar to server_name,location there are several ways to match, using: Location [=|^~|~|~*]/uri, priority order:

(1) =, exact match;

(2) ^~, the first half of the match;

(3) ~, case-sensitive matching;

(4) ~*, case-insensitive matching;

If the hostname cannot accompany any of the above matches, the first location is matched by default.

3.root Value Settings

Root similar to Documentroot,root in Apache can be set in the HTTP segment, the server segment and the location segment, the smaller the scope, the higher the priority, do not set the default inheritance of the first level of settings, root equivalent to the beginning of the path, The following value can be relative to the path or absolute path, if it is a relative path, he is relative to the Nginx installation path, location on the file system position is actually: root/location, for example, the root directory (/) where the location is/usr/local/nginx/ The html,50x.html file is located at/usr/local/nginx/html/50x.html.

4.alias Value Settings

In addition to using root to represent the starting position of a path, you can also use alias for path alias settings

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/9D/92/wKiom1mB-gbh8HRwAAANuSiNfkM973.png "title=" 4.png "Style=" Float:none; "alt=" Wkiom1mb-gbh8hrwaaanusinfkm973.png "/>

Unlike Root, where the revelation location is different, alias is equivalent to the path alias, and the alias directory is located in/var/www/alias, and the/alias directory itself can not exist on the file system.

5. Condition Monitoring

Nginx provides a condition monitoring module, which is opened using the--with-http_stub_status_module option at compile time and set in the configuration file.

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/9D/92/wKiom1mB-gbTbGhRAAAHUuy7WLU647.png "title=" 5.png "Style=" Float:none; "alt=" Wkiom1mb-gbtbghraaahuuy7wlu647.png "/>

The monitor data can be viewed through the browser, and Active connections represents the number of connections that are currently open, the number of connections processed, the number of handle successfully created, each connection can contain multiple requests, and requests represents the number of requests processed. The reading represents the number of header information read to the client, writing represents the number of information returned to the client header, and the value of waiting equals active-(reading + writing) when the keep-alive is turned on. Indicates that Nginx has processed the number of hosted connections waiting for the next request

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/9D/91/wKioL1mB-gfRSrWwAABEtKEgGK0599.png "title=" 6.png "Style=" Float:none; "alt=" Wkiol1mb-gfrsrwwaabetkeggk0599.png "/>

6. Access control

Nginx can be based on IP and user authentication function, in the Status monitoring page, for example, we only allow the specific network segment of the IP to view the monitoring data, can be used to limit the use and Deny directives

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/9D/92/wKiom1mB-gfjAydyAAAM0PdwUF8200.png "title=" 7.png "Style=" Float:none; "alt=" Wkiom1mb-gfjaydyaaam0pdwuf8200.png "/>

If you want to set up further, you can use the HTPASSWD tool to create a user using the User Basic authentication (you can also create a system user directly and save the password in a specified file)

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/9D/91/wKioL1mB-giyYa3QAAAo4_8fT5E346.png "title=" 8.png "Style=" Float:none; "alt=" Wkiol1mb-giyya3qaaao4_8ft5e346.png "/>

Then enable the authentication feature in the configuration file

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9D/92/wKiom1mB-gmw9e5UAAAbvx-ZRSE079.png "title=" 9.png "Style=" Float:none; "alt=" Wkiom1mb-gmw9e5uaaabvx-zrse079.png "/>

Then refresh the page and you'll find the password is needed.

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/9D/91/wKioL1mB-gmgDgC5AAAwzLqs-6s560.png "title=" 10. PNG "style=" Float:none; "alt=" Wkiol1mb-gmgdgc5aaawzlqs-6s560.png "/>



This article from "Rabbit-like rabbit sen Broken" blog, please be sure to keep this source http://arkling.blog.51cto.com/2844506/1953165

Nginx Learning Notes (ii)--building a Web server

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.