Nginx Instructions for use (ii) Beginner's Guide

Source: Internet
Author: User

This section describes how to start and stop Nginx, reload configuration, configuration file structure, how to configure Nginx to allocate static content, how to configure Nginx as a proxy server, how to associate with the FASTCGI program.

Nginx contains a master process and multiple worker processes. The main process reads and processes the configuration and maintains the worker process. The worker process is responsible for the actual request processing. Nginx uses event-driven and operating system-related mechanisms to efficiently allocate requests between worker processes. The number of worker processes can be set in the configuration file, can be configured as a fixed number, or according to the CPU core number of dynamic adaptation.

By default, the configuration file is nginx.conf, the general path is /usr/local/nginx/conf , /etc/nginx or/usr/local/etc/nginx。像上一节我们设置的安装路径,配置文件放在/usr/local/nginx/conf。

启动,停止nginx,更新配置

Run the executable file to start Nginx, as

[Email protected] nginx]#/usr/local/nginx/sbin/nginx

After starting, you can use the following command to operate Nginx

Nginx-s Signal

signal can be the following command

Stop: Fast Stop

Quit: Safe exit

Reload: Reload configuration file

Reopen: Open log file

If you want to wait until the Nginx worker process finishes processing all of the current requests, you should use the following command:

Nginx–s quit

After the configuration file is modified, it will need to reload or restart Nginx to function, reload the configuration to execute the following command:

Nginx–s Reload

After the main process receives the command to reload the configuration, first check that the new configuration syntax is valid and try to apply the new configuration. If the new configuration app succeeds, the main process starts a new worker process and sends a message to notify the old process to stop working. If the new configuration app fails, roll back all changes and continue using the old configuration, the old process discards the new configuration, and continues to process the current request with the old process.

You can also send a signal directly to Nginx's process ID, such as the Kill command, via the UNIX command tool. Nginx's main process ID is configured by default in the Nginx.pid file in the/usr/local/nginx/logs or/var/run directory. Assuming the main process ID is 1628, to use the QUIT command to let Nginx safely exit, execute the following command:

Kill–s quit 1628

Use the PS command to get nginx all running

Ps-ax | grep nginx

Configuration file Structure

The various modules of Nginx are controlled by the instruction set in the configuration file. The instruction set is divided into simple instructions and block directives, a simple instruction consists of a name and a parameter, the middle is separated by a space, with a semicolon (;) end, the structure of the block instruction is the same as a simple instruction, but an additional instruction set surrounded by braces ends. The curly braces of a block instruction can contain other directives, called contexts (for example: Events,http,server and location).

Instructions placed outside of all contexts are considered to be in the main context. The main context contains events and HTTP directives, and HTTP contains server,server containing the location.

Note Each line starts with #.

static files

One of the important functions of a Web server is to allocate static files (slices and HTML files). You can implement an example that assigns files from different directories according to the request:/data/www and/data/images. Modify the configuration file, set a server block in the HTTP block, and include two Locaton blocks in the server block.

First, create a/data/www directory, add a Html.index file, define your own content, create a/data/images directory, and put a few pictures.

Then open the configuration file, which already contains some annotated examples by default. Regardless of the content of the comment, add a new server block,

http{

server{

}

}

A configuration file can contain multiple server blocks, which are distinguished by the listening port (listen) and the Service Name (server_name), and the URI in the request message header matches the parameters of Loation in the server to determine which server block is processing the request.

Add the following directive to the server block:

Location/{

Root/data/www

}

The location block defines the "/" prefix to be compared to the requested URI, and the matching request URI is added to the path defined by the root directive, which is/data/www, which makes up the local path to the request file. If there are multiple location blocks to match, Nginx chooses one of the longest prefixes. The Locatoin block above uses the shortest length prefix, so if other location does not match, the location block will be used.

Add a second location block:

location/images/{

Root/data;

}

Matches all requests that begin with/images/.

The final content of the configuration file server is as follows:

server{

Location/{

root/data/www;

}

location/images/{

Root/data;

}

}

Configured to listen on port 80 and can be accessed via http://localhost/. URIs the request begins with/images/, the server sends the file from/data/images, such as the corresponding request Http://localhost/images/example.png,nginx sends the file/data/images/ Example.png, if the file does not exist, Nginx sends a 404 response exception. Requests that do not start with/images/are mapped to the/data/www directory. For example, request http://localhost/some/example.html will send file/data/www/some/example.html.

Restart the Nginx or send the reload command for the new configuration to take effect:

Nginx–s Reload

If an exception occurs, you can find the cause in Access.log and Error.log, whose directory is/usr/local/nginx/logs or/var/logs/nginx

Configure a simple Proxy service

Nginx is often used as a proxy server. The proxy server receives the request, passes them to the proxy server, gets the response, and returns to the client.

We are going to configure a proxy server to process the picture file requests from the local directory, and other requests are sent to the proxy server for processing. In this example, two servers are defined in the same Nginx instance.

First, define the proxy server and add another server block in the configuration file, as follows:

server{

Listen 8080;

Root/data/up1;

Location/{

}

}

This service is configured to listen on port 80, mapping all requests to the/data/up1 directory. The root command here is placed in the server context, and you can configure the root command like this when there is no root command in the location command.

Then, modify the configuration of the previous section as a proxy server. In the first location block, configure the Proxy server protocol, name, and port settings for the Proxy_pass parameter values:

server{

Location/{

Proxy_pass http://localhost:8080;

}

location/images/{

Root/data;

}

}

Modify the second location block so that it matches only the pictures of a specific extension.

Location ~\. (gif|jpg|png) ${

Root/data/images;

}

parameter is a regular expression that matches all URIs that end in. gif,.jpg or. png, the regular expression must begin with a ~, and requests that match that configuration are mapped to the/data/images directory.

When Nginx processes a request, it first checks the longest prefix defined by the location directive, compares it to a regular expression, and if so, Nginx uses the location, otherwise it is defined with other directives.

The final configuration of the proxy server is as follows:

server{

Location/{

Proxy_pass http://localhost:8080//;

}

Location ~\. (gif|jpg|png) ${

Root/data/images;

}

}

The server filters requests that end with. gif,.jpg or. png, maps them to the/data/images directory, and passes other requests to the proxy server.

Setting up the fastcgi agent

Nginx can route requests to various frameworks and language-developed FASTCGI application servers, such as PHP.

Most basic, change the proxy_pass instruction to the fastcgi_pass instruction, and use the Fastcgi_param to set the parameters to the FASTCGI server. Assuming that the access address of the FASTCGI server is localhost:9000, using the configuration of the proxy server in the previous section, change Proxy_pass to Fastcgi_pass and update the parameter value to localhost:9000. In PHP, the Script_filename parameter is used to determine the script name, Query_string is used to pass the request parameters, and the final configuration file is as follows:

server{

Location/{

Fastcgi_pass localhost:9000;

Fastcgi_param script_filename $document _root$fastcgi_script_name;

Fastcgi_param query_string $query _string;

}

Location ~\. (gif|jpg|png) $ {

Root/data/images;

}

}

With this configuration, all requests are routed through the FASTCGI protocol to the proxy server localhost:9000, in addition to the static files.

Nginx Instructions for use (ii) Beginner's Guide

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.