Nginx common command configuration file
Common commands
After installing nginx, enter nginx -? Query nginx command line parameters
nginx version: nginx/1.22.1
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
[-e filename] [-c filename] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop— 快速关机, quit— 优雅地关闭, reopen— 重新打开日志文件, reload— 重新加载配置, 使用新配置启动新的工作进程, 正常关闭旧的工作进程。
-p prefix : set prefix path (default: /usr/share/nginx/)
-e filename : set error log file (default: /var/log/nginx/error.log)
-c filename : set configuration file (default: /etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file
Start and restart Nginx
start up
sudo systemctl start nginx
Automatically start the nginx service when the system starts
sudo systemctl enable nginx
Restart nginx
The restart option is a quick action to stop and then start the Nginx server.
sudo systemctl restart nginx
Reload configuration, start new worker process with new configuration, shut down old worker process gracefully
nginx -s reload
fast shutdown
nginx -s stop
close gracefully
nginx -s quit
configuration file
The main configuration file of Nginx is in /etc/nginx/nginx.conf.
The entire configuration file is composed of command-controlled modules. Commands are divided into simple commands and fast commands. A visible directive consists of a name and parameters separated by spaces and ends with a semicolon ";". A block directive has the same structure as a simple directive, but it is surrounded by a set of braces "{}". Of course, there may be other instructions and block instructions in the fast instruction.
The configuration file can be divided into four types of layers: main layer, events layer, http layer, server layer, location layer
main
Main layer: It mainly sets some configuration instructions that affect the overall operation of the nginx server, mainly including configuring the users (groups) running the Nginx server, the number of worker processes allowed to be generated, the process PID storage path, log storage path and type, and the introduction of configuration files wait.
user nginx; #进程用户
worker_processes auto; #工作进程,配合和CPU个数保持一致
error_log /var/log/nginx/error.log notice; #错误日志路径及级别
pid /run/nginx.pid; #Nginx服务启动的pid
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events
It mainly affects the network connection between the Nginx server and users. Commonly used settings include whether to enable serialization of network connections under multi-worker processes, whether to allow multiple network connections to be received at the same time, and which event-driven model to choose to process connection requests. The maximum number of connections that the worker process can support at the same time, etc.
events {
worker_connections 1024; #每个worker进程支持的最大连接数
}
http
The most frequent part of the Nginx server configuration, most functions such as proxy, cache and log definitions, and the configuration of third-party modules are here. Both the server layer and the location layer are included.
The server is also called the virtual host part. It describes a group of resources that are logically divided according to different server_name instructions. These virtual servers respond to http requests. The most common configurations are the monitoring configuration of the virtual machine host and the name or IP configuration of the virtual host. A server block can configure multiple location blocks. Configure matching rules in location.
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; #日志格式
access_log /var/log/nginx/access.log main; #访问日志
sendfile on; #优化静态资源
tcp_nopush on;
keepalive_timeout 65; #给客户端分配连接超时时间,服务器会在这个时间过后关闭连接。
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf; #指定在当前文件中包含另一个文件的指令
server {
listen 80; #监听端口,默认80
listen [::]:80;
server_name _; #提供服务的域名或主机名
root /usr/share/nginx/html; #存放网站的路径
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}