公司使用到了nginx,于是周末初步接触了一下nginx,立即被其简洁,优雅,高效的特性给迷住了。nginx是在是个好东西,配置极其简单,容易理解,极其高效,稍微一调优,ab测试10k并发,很轻松。比起apache来强太多了...
1. 下载
|
1
2
3
4
5
6
7
8
9
10
11
|
[root@localhost src]#
wget -c http://nginx.org/download/nginx-1.6.2.tar.gz--2015-01-11 16:04:13--
http://nginx.org/download/nginx-1.6.2.tar.gzResolving
nginx.org... 206.251.255.63Connecting
to nginx.org|206.251.255.63|:80...
connected.HTTP
request sent, awaiting response... 200 OKLength: 804164 (785K)
[application/octet-stream]Saving
to: “nginx-1.6.2.tar.gz”100%[=====================================================================>] 804,164 5.03K/s
in 3m 42s2015-01-11 16:07:57 (3.54 KB/s)
- “nginx-1.6.2.tar.gz”
saved [804164/804164] |
2.解压
|
1
2
3
4
5
6
7
8
|
[root@localhost src]#
tar xvf nginx-1.6.2.tar.gznginx-1.6.2/nginx-1.6.2/auto/nginx-1.6.2/conf/nginx-1.6.2/contrib/nginx-1.6.2/src/nginx-1.6.2/configure... |
3. 安装
|
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
|
[root@localhost nginx-1.6.2]#
./configure....creating
objs/MakefileConfiguration
summary +
using system PCRE library +
OpenSSL library is not used +
using builtin md5 code +
sha1 library is not found +
using system zlib library nginx
path prefix: "/usr/local/nginx" nginx
binary file: "/usr/local/nginx/sbin/nginx" nginx
configuration prefix: "/usr/local/nginx/conf" nginx
configuration file: "/usr/local/nginx/conf/nginx.conf" nginx
pid file: "/usr/local/nginx/logs/nginx.pid" nginx
error log file: "/usr/local/nginx/logs/error.log" nginx
http access log file: "/usr/local/nginx/logs/access.log" nginx
http client request body temporary files: "client_body_temp" nginx
http proxy temporary files: "proxy_temp" nginx
http fastcgi temporary files: "fastcgi_temp" nginx
http uwsgi temporary files: "uwsgi_temp" nginx
http scgi temporary files: "scgi_temp"[root@localhost nginx-1.6.2]#
make && make install[root@localhost nginx]#
pwd/usr/local/nginx[root@localhost nginx]#
lltotal 16drwxr-xr-x. 2 root
root 4096 Jan 11 16:12 confdrwxr-xr-x. 2 root
root 4096 Jan 11 16:12 htmldrwxr-xr-x. 2 root
root 4096 Jan 11 16:12 logsdrwxr-xr-x. 2 root
root 4096 Jan 11 16:12 sbin |
安装成功。其中conf是配置文件的目录,html是放web页面的目录,logs是放日志文件的目录,sbin目录是 nginx运行时二进制文件。安装时有可能报PCRE库缺失,可以使用命令安装即可:yum -y install pcre-devel;
4. 启动关闭nginx的方法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
[root@localhost sbin]#
/usr/local/nginx/sbin/nginx[root@localhost sbin]#
echo $?0[root@localhost sbin]#
ps -elf|grep nginx1 S
root 3740 1 0 80 0 - 887 - 16:16 ? 00:00:00 nginx:
master process /usr/local/nginx/sbin/nginx5 S
nobody 3741 3740 0 80 0 - 933 - 16:16 ? 00:00:00 nginx:
worker process0 S
root 3744 1306 0 80 0 - 1088 - 16:16 pts/1 00:00:00 grep
nginx[root@localhost sbin]#
netstat -antpActive
Internet connections (servers and established)Proto
Recv-Q Send-Q Local Address Foreign Address State PID/Program nametcp 0 0 0.0.0.0:22 0.0.0.0:*
LISTEN 1131/sshdtcp 0 0 127.0.0.1:631 0.0.0.0:*
LISTEN 1004/cupsdtcp 0 0 0.0.0.0:40035 0.0.0.0:*
LISTEN 928/rpc.statdtcp 0 0 0.0.0.0:111 0.0.0.0:*
LISTEN 908/rpcbindtcp 0 0 0.0.0.0:80 0.0.0.0:*
LISTEN 3740/nginxtcp 0 64 192.168.137.9:22 192.168.137.1:51321 ESTABLISHED 1267/sshdtcp 0 0 192.168.137.9:22 192.168.137.1:51322 ESTABLISHED 1270/sshdtcp 0 0 192.168.137.9:22 192.168.137.1:51336 ESTABLISHED 1331/sshdtcp 0 0 :::22 :::*
LISTEN 1131/sshdtcp 0 0 ::1:631 :::*
LISTEN 1004/cupsdtcp 0 0 :::38082 :::*
LISTEN 928/rpc.statdtcp 0 0 :::111 :::*
LISTEN 908/rpcbind |
上面所示,成功启动了ngnix,在80端口运行。
nginx的进程分为了master 进程和worker进程,前者做为管理进程,管理后者,后者是处理页面请求的进程,worker可以有多个。一般根据CPU核数和负载进行配置多个worker. 我们访问试试:

nginx的启动,关闭等等操作命令如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@localhost sbin]#
/usr/local/nginx/sbin/nginx -hnginx
version: nginx/1.6.2Usage:
nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]Options: -?,-h
: this help -v
: show version and exit -V
: show version and configure options then exit -t
: test configuration 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/local/nginx/) -c
filename : set configuration file (default:
conf/nginx.conf) -g
directives : set global directives out of configuration file |
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx -s quit
/usr/local/nginx/sbin/nginx -s reopen
/usr/local/nginx/sbin/nginx -s reload
分别表示 优雅的停止nginx;立即停止nginx;重新打开日志文件;平滑的重启nginx并重新加载nginx的配置文件;
/usr/local/nginx/sbin/nginx -t 可以用来修改配置文件之后,测试配置文件是否有语法错误
5. 通过信号量来控制nginx
其实质是通过信号量来对nginx进行控制的,所以也可以通过下面的方式来控制nginx:
kill -INT `cat /usr/local/nginx/logs/nginx.pid`
|
1
2
3
|
[root@localhost logs]#
kill -INT `cat /usr/local/nginx/logs/nginx.pid`[root@localhost logs]#
ps -elf|grep nginx0 S
root 3843 1306 0 80 0 - 1088 - 16:37 pts/1 00:00:00 grep
nginx |
看到nginx的两个进程被我们杀掉了。还有其他的信号量可以使用,分别对应到上面的命令。
kill -HUP pid, kill -USR1 pid, kill -USR2 pid 等等,总结如下:
1. TERM,INT : Quick shutdown,立即关闭进程,不管他有没有在处理请求;
2. QUIT : Graceful shutdown, 优雅的关闭进程,也就是等到该进程处理的请求都完成之后才关闭;
3. HUP : Configuration reload, start the new worker processes with a new configuration. Gracefully shutdown the old worker processes
4. USR1 : Reopen the log files, 重新打开日志文件,在备份日志按月/日分割日志时用;
5. USR2 : Upgrade Executable on the fly, 平滑的升级;
6. WINCH : Gracefully shutdown the worker processes, 优雅的关闭旧的进程(配合USR2来进行升级);
先写到这里,后面继续学习nginx的配置。
1085

被折叠的 条评论
为什么被折叠?



