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
|
worker_processes 1;
events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server { listen 80; server_name localhost;
location / { root html; index index.html index.htm; }
error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
}
}
|
2.1 全局块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| user [user] [group]
user nobody nobody;
worker_processes number | auto;
worker_processes 4;
pid logs/nginx.pid;
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
| accept_mutex on | off;
multi_accept on | off;
use epoll
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;
default_type application/octet-stream;
access_log off;
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 | off;
sendfile_max_chunk size;
sendfile_max_chunk 128k;
keepalive_timeout timeout [header_timeout]
keepalive_timeout 120s 100s
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; }
|
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
| error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
|