nginx基本使用

1 基本命令

1
2
3
4
#启动
./bin/nginx
#停止
./bin/nginx -s stop

2 配置详解

conf/nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#全局块
#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

#event块
events {
worker_connections 1024;
}

#http块
http {
include mime.types;
default_type application/octet-stream;

#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 logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}

2.1 全局块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 指定可以运行nginx服务的用户和用户组,只能在全局块配置
user [user] [group]
# 将user指令注释掉,或者配置成nobody的话所有用户都可以运行
user nobody nobody;
# user指令在Windows上不生效,如果你制定具体用户和用户组会报小面警告

# 指定工作线程数,可以制定具体的进程数,也可使用自动模式,这个指令只能在全局块配置
worker_processes number | auto;
# 列子:指定4个工作线程,这种情况下会生成一个master进程和4个worker进程
worker_processes 4;

# 指定pid文件存放的路径,这个指令只能在全局块配置
pid logs/nginx.pid;

# 指定错误日志的路径和日志级别,此指令可以在全局块、http块、server块以及location块中配置。
# 其中debug级别的日志需要编译时使用--with-debug开启debug开关
error_log [path] [debug | info | notice | warn | error | crit | alert | emerg]
error_log logs/error.log notice;
error_log logs/error.log info;

2.2 event块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 开启的时候,将会对多个Nginx进程接收连接进行序列化,防止多个进程对连接的争抢,默认是开启状态
accept_mutex on | off;

# 如果multi_accept被禁止了,nginx一个工作进程只能同时接受一个新的连接。否则,一个工作进程可以同时接受所有的新连接。
# 如果nginx使用kqueue连接方法,那么这条指令会被忽略,因为这个方法会报告在等待被接受的新连接的数量。
# 默认是off状态
multi_accept on | off;

# 指定使用哪种网络IO模型,method可选择的内容有:select、poll、kqueue、epoll、rtsig、/dev/poll以及eventport。
# use method
use epoll

# 设置允许每一个worker process同时开启的最大连接数,当每个工作进程接受的连接数超过这个值时将不再接收连接
# 当所有的工作进程都接收满时,连接进入logback,logback满后连接被拒绝
# 注意:这个值不能超过超过系统支持打开的最大文件数,也不能超过单个进程支持打开的最大文件数
worker_connections 1024;

2.3 http块(重点)

2.3.1 http全局块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# include指令,用于包含其他的配置文件,可以放在配置文件的任何地方,但是要注意你包含进来的配置文件一定符合配置规范
# 下面的指令将mime.types包含进来,mime.types和ngin.cfg同级目录,不同级的话需要指定具体路径
include mime.types;

# 配置默认类型,如果不加此指令,默认值为text/plain。
# 此指令还可以在http块、server块或者location块中进行配置。
default_type application/octet-stream;

# access_log配置,此指令可以在http块、server块或者location块中进行设置
# 在全局块中,介绍过errer_log指令,用于配置Nginx进程运行时的日志存放和级别,此处所指的日志与常规的不同,它是指记录Nginx服务器提供服务过程应答前端请求的日志
# access_log path [format [buffer=size]]
# 如果你要关闭access_log,你可以使用下面的命令
access_log off;

# log_format指令,用于定义日志格式,此指令只能在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 logs/access.log main;

# 开启关闭sendfile方式传输文件,可以在http块、server块或者location块中进行配置
sendfile on | off;

# 设置sendfile最大数据量,此指令可以在http块、server块或location块中配置
sendfile_max_chunk size;
# 其中,size值如果大于0,Nginx进程的每个worker process每次调用sendfile()传输的数据量最大不能超过这个值(这里是128k,所以每次不能超过128k);如果设置为0,则无限制。默认值为0。
sendfile_max_chunk 128k;

# 配置连接超时时间,此指令可以在http块、server块或location块中配置。
# 与用户建立会话连接后,Nginx服务器可以保持这些连接打开一段时间
# timeout,服务器端对连接的保持时间。默认值为75s;header_timeout,可选项,在应答报文头部的Keep-Alive域设置超时时间:“Keep-Alive:timeout= header_timeout”。报文中的这个指令可以被Mozilla或者Konqueror识别。
keepalive_timeout timeout [header_timeout]
# 下面配置的含义是,在服务器端保持连接的时间设置为120s,发给用户端的应答报文头部中Keep-Alive域的超时时间设置为100s。
keepalive_timeout 120s 100s

# 配置单连接请求数上限,此指令可以在http块、server块或location块中配置。
# Nginx服务器端和用户端建立会话连接后,用户端通过此连接发送请求。指令keepalive_requests用于限制用户通过某一连接向Nginx服务器发送请求的次数。默认是100
keepalive_requests number;

2.3.2 server块

每一个http块都可以包含多个server块,而每个server块就相当于一台虚拟主机。server块也可以包含自己的全局块,同时可以包含多个location块。

2.3.2.1 listen指令
1
2
3
4
5
listen 127.0.0.1:8000;     #只监听来自127.0.0.1这个IP,请求8000端口的请求
listen 127.0.0.1; #只监听来自127.0.0.1这个IP,请求80端口的请求(不指定端口,默认80)
listen 8000; #监听来自所有IP,请求8000端口的请求
listen *:8000; #和上面效果一样
listen localhost:8000; #和第一种效果一致
2.3.2.2 server_name指令

对于name 来说,可以只有一个名称,也可以由多个名称并列,之间用空格隔开。

1
2
server_name myserver.com www.myserver.com
server_name myserver.* *.myserver.com
2.3.2.3 location块
1
2
3
4
5
location [ = | ~ | ~* | ^~ ] uri { 
root html; #静态资源存放位置
index index.html index.htm; #文件名
deny all; #拒绝访问的ip限制,会返回403页面
}

uri变量是待匹配的请求字符串,可以是不含正则表达的字符串,如/myserver.php等;也可以是包含有正则表达的字符串,如 .php$(表示以.php结尾的URL)

其中方括号里的部分,是可选项,用来改变请求字符串与 uri 的匹配方式:

  • “=”,用于标准uri前,要求请求字符串与uri严格匹配。优先级最高。
  • “^~”,用于标准uri前,要求Nginx服务器找到标识uri和请求字符串匹配度最高的location后,立即使用此location处理请求。
  • “~”,用于表示uri包含正则表达式,并且区分大小写。
  • “~*”,用于表示uri包含正则表达式,并且不区分大小写。注意如果uri包含正则表达式,就必须要使用“~”或者“~*”标识
1
2
3
4
5
6
7
8
9
10
11
12
1 location = /
2 location = /index
3 location ^~ /article/
4 location ^~ /article/files/
5 location ~ \.(gif|png|js|css)$
6 location /

http://localhost/ -> 1
http://localhost/index -> 2
http://localhost/article/ -> 3
http://localhost/article/files/1.txt -> 4
http://localhost/1.txt -> 5
2.3.2.4 错误码页面
1
2
3
4
5
# 发生错误500 502 503 504都会重定向到html下的/50x.html页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

nginx基本使用
http://www.zivjie.cn/2023/05/20/中间件/nginx/nginx基本使用/
作者
Francis
发布于
2023年5月20日
许可协议