Nginx beginner's Guide
1.1 Introduction
This Guide provides a simple description of the basic functions of nginx. Now nginx should have been installed on the reader's machine. If not, check the installation page. This Guide describes how to start, stop nginx, and reload its configuration file. It explains the structure of the configuration file, how to set static content of the nginx service, and how to configure nginx as a proxy server, how to connect to a FastCGI application.
Nginx has a master process and several worker processes. The master process aims to read and evaluate the configuration file and maintain the worker process. The Woker process actually processes the request. Nginx uses the event-based model and OS-dependent mechanism to efficiently distribute requests to woker processes. The number of worker processes can be defined in the configuration file, you can use a fixed number or automatically adjust it to the number of available CPU cores (see worker_processes ).
The way nginx and Its modules work depends on the configuration file. The default configuration file name is nginx. conf, which is located in the/usr/local/nginx/conf or/usr/local/etc/nginx directory.
1.2 start, stop, and reload configuration files
Generally, nginx is started using the ngixn executable file. The nginx executable file has the following usage.
Nginx-s signal
The signal can be one of the following
· Stop-fast shutdown
· Quit-graceful shutdown
· Reload-reloading the configuration file
· Reopen-reopening the log files
Changes in the configuration file will not take effect immediately, unless you reload the configuration file or restart nginx, you want to reload the configuration and execute it.
Nginx-s reload
Once the main process receives a signal to reload the configuration file, it first checks whether the syntax is correct, and then tries to apply the configuration file. If it succeeds, the main process starts a new worker process and sends a message to the old worker process asking them to close the process. Otherwise, the master process continues to work under the old configuration file until it is changed. The old worker process stops accepting new requests after receiving the close command, but continues to process the current request to know that all requests have been processed. Then the old worker process exits.
1.3 Structure of the configuration file
Nginx is composed of modules, which are controlled by specifying commands in the configuration file. Commands are divided into simple commands and block commands. simple commands are separated by names and parameters by spaces and end with semicolons. Block commands as simple commands have the same results, but they do not end with semicolons, but are curly braces {}. Block Commands include other commands in curly brackets, which are called context, such as events, http, server, and location.
The command is placed outside any context and is viewed as the main context ). Events and http commands are placed in the main context, server is placed in http, and location is placed in server.
Each line is marked as a comment after #
1.4 provide static content
An important task of a web server is a service file (such as an example or static HTML page ). Where are you going to implement the example? Depending on the request, different local directories of files provide services,/data/www (including HTML pages) and/data/images (including images ). You only need to edit two location blocks in the server block in http.
First, create a/data/www directory and put a static file that contains some text information. Then create the/data/iamges directory to store some images.
Next, open the default configuration file of the configuration file and examples containing several server blocks, but most of them are commented out. Now comment out all server blocks and start a new server block.
Http {
Server {
}
}
Generally, this configuration file can contain several server blocks, which are distinguished by the listening port and server name. Once nginx decides which server processes the request, it will test the URI specified in the request and the parameters of the location command block defined in the server block.
Add the following location block to the server block.
Location /{
Root/data/www;
}
This location block specifies the/prefix to compare the request URI. For matched requests, the URI is added to the path of the specified root command, which is/data/www. If multiple location blocks are matched, nginx selects the longest prefix. The preceding location block provides the shortest prefix. Therefore, this block is used only when all other location blocks are not matched.
Now add the second location Block
Location/images /{
Root/data;
}
This will match requests starting with/images/(/will also match, but it is a short prefix)
The configuration result of the server block is as follows.
Server {
Location /{
Root/data/www;
}
Location/images /{
Root/data;
}
}
This is a configuration file that can be normally monitored on port 80 and accessed through the Local Machine http: // localhost. For requests starting with/images/, the server sends files from the/data/images directory. For example, if you reply to http: // localhost/images/example.png, the server will send the/data/images/example.png file. If the file does not exist, nginx will send a 404 error. Files Not starting with/images/are mapped to the/data/www directory. For example, http: // localhost/some/example.html, ngixn will send the/data/www/some/example.html file.
Sometimes, you can try to access. log or error. log to find the cause, which is located in the/sur/local/nginx/logs or/var/log/nginx directory.
1.5 configure a simple Proxy Server
A common method of nginx is to act as a proxy server, which means that the Service receives the request, passes the request to the proxy server, retrieves the response from the proxy server, and then sends it to the client.
We will configure a basic proxy server, the local server serves image requests, and other requests are sent to the proxy server. In this example, both servers are defined as simple nginx instances.
First, define multiple proxy servers by adding one or more server blocks in the configuration file.
Server {
Listen 8080;
Root/data/up1;
Location /{
}
}
This is a simple server that listens to port 8080, maps all requests to the/data/up1 file system directory, creates this directory, and stores the index.html file. Remember to place the root command in the context of the server (context ). When the location block does not contain the root command, this command takes effect.
Next, use the configuration file defined above to modify it as a proxy server. In the first location block, place the proxy_pass command and specify the parameter. Here we use http: // localhost: 8080.
Server {
Location /{
Proxy_pass http: // localhost: 8080;
}
Location/images /{
Root/data;
}
}
We will modify the second location block, which is mapped to the/images/prefix under the/data/images directory. Now, to match a typical image, modify the low.o block as follows.
Location ~ \. (Gif | jpg | png) $ {
Root/data/images;
}
This is a regular expression matching a file ending with .gif, .jpg, or .png. The regular expression must start ~ . Matched requests are mapped to the/data/images directory.
When nginx selects a location block service request, it first checks the location command with the specified prefix, and then checks the regular expression.
The configuration result of the proxy server is as follows.
Server {
Location /{
Proxy_pass http: // localhost: 8080 /;
}
Location ~ \. (Gif | jpg | png) $ {
Root/data/images;
}
}
This server filters all files ending with .gif or .jpg or png and maps them to the/data/images directory. Then all requests are sent to the proxy server.
There are more commands for more in-depth configuration of proxy connections
1.6 configure a FastCGI proxy
Nginx can be used to route requests to the FastCGI server. for the most basic ngxin configuration, you can use fastcgi_pass to enable the fastcgi service to work with the nginx server, instead of using the proxy_pass command. The fastcgi_param command sets the parameters passed to the FastCGI server. In PHP, The SCRIPT_FILENAME parameter is used to determine the script name, And the QUERY_STRING parameter is used to set the parameters for passing requests.
The configuration result 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;
}
}
This will set up a server that will route all requests waiting t for requests for static images to the proxied server operating on localhost: 9000 through the FastCGI protocol.
This will set up a server, route all requests except images to the proxy server, and run on localhost: 9000 through the FastCGI protocol.
Deployment of Nginx + MySQL + PHP in CentOS 6.2
Build a WEB server using Nginx
Build a Web server based on Linux6.3 + Nginx1.2 + PHP5 + MySQL5.5
Performance Tuning for Nginx in CentOS 6.3
Configure Nginx to load the ngx_pagespeed module in CentOS 6.3
Install and configure Nginx + Pcre + php-fpm in CentOS 6.4
Nginx installation and configuration instructions
Nginx log filtering using ngx_log_if does not record specific logs
Nginx details: click here
Nginx: click here
This article permanently updates the link address: