1

1 Introduction

1.1 Process

There is a master process: read, evaluate conf; maintain worker processes;
There are multiple worker processes: processing requests;

1.2 nginx process model

nginx uses 事件驱动模型 and operating system mechanism to realize the request distribution worker process;

1.3 Number of Worker Processes

The worker worker process is configured in conf, and can also be automatically adjusted by the number of CPU cores

worker_processor number|auto;
// The default is number; if it is auto, the default number of CPU cores

1.4 Configuration file location

Configuration file: nginx.conf is located at:

  • /usr/local/nginx/conf
  • /usr/local/etc/nginx
  • /etc/nginx

1.5 start stop reload

1.5.1 Signaling via nginx -s (signal)

signal=>

 stop: 快速终止
quit: 优雅终止
reload: 重加载配置
reopen: 重新打开日志文件

1.5.2 Stop nginx

nginx -s quit Wait for the worker process to finish processing the current request and stop nginx
nginx -s stop Stop the nginx process immediately

1.5.3 Reload configuration

nginx -s reload After the master process receives the reload operation command:

  1. Check the legality of configuration syntax;
  2. application configuration;
  3. If the application configuration is successful, the master process starts a new worker process and sends a message to the old worker process to request them to stop; if the application configuration fails, the master process rolls back and still uses the old worker process to work;
  4. The old worker process receives a shutdown command from the master and will stop accepting new connections, but continue to serve the current request on hand. Then, the old worker process exits.

1.5.4 List all nginx processes

ps -ax|grep nginx

2. Configuration file structure

2.1 Modules

nginx consists of modules, which are controlled by directives in configuration files.
The instructions are divided into 简单指令 and 块指令 .

2.2 Simple Instructions

Consists of name and parameter (with spaces in between) and ends with a semicolon (;).

2.3 Block Instructions

Has the same structure as a simple directive, but it is surrounded by a pair of curly braces {} instead of ending with a semicolon (;).

2.4 Context

If a block instruction can contain other instructions within curly braces, it (the block) is called a context. E.g

events, http, server, location

2.5 main context

Instructions outside other contexts, considered main上下文 (main context)

  • events and http instructions belong to the main context;
  • server belongs to http context;
  • location belongs to the server context;

2.6 The content in the line after the "#" pound sign is a comment

3. Serving Static Content

3.1 Documentation

a static html /data/www ->index.html
an image /data/images *.jpg

3.2 Structure

 http {
    server {
        location / {
            root /data/www;
        }
    }
}

The "/" of the location will be compared with the request address in the URI, and if it matches, the request information will be located from the path specified by root:

If there are multiple locations: select the longest prefix match; here "/" is the shortest match, and only when all matches cannot be matched, it will be located in its root.

3.3 Then add a second location

location /images/ {

 root /data;

}

It will match requests starting with /images/. (/ also matches, but it is the shortest match)

3.4 Final configuration

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

Requests starting with /images/ will be responded to by looking in the /data/images directory; for example:
http://localhost/images/a.jpg , nginx will send --- /data/www /data/images/a.jpg file to you, if not found, it will 404; .

3.4 Logs

To make nginx work: nginx -s reload
If there is any problem, you can check the log: access.log / error.log
/usr/local/nginx/logs or /usr/log/nginx

...to be continued (the old notes have been copied, not finished)


丰木
322 声望19 粉丝

遇见超乎想象的自己!


下一篇 »
CSS杂记