首页 > 代码库 > nginx入门

nginx入门

公司的使用到了nginx,于是周末初步接触了一下nginx,立即被其简洁,优雅,高效的特性给迷住了。ngnix是在是个好东西,配置极其简单,容易理解,极其高效,稍微一调优,ab测试10k并发,很轻松。比起apache来强太多了...

1. 下载

[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.解压

[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. 安装

[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的方法

[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 -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1131/sshd
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      1004/cupsd
tcp        0      0 0.0.0.0:40035               0.0.0.0:*                   LISTEN      928/rpc.statd
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      908/rpcbind
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      3740/nginx
tcp        0     64 192.168.137.9:22            192.168.137.1:51321         ESTABLISHED 1267/sshd
tcp        0      0 192.168.137.9:22            192.168.137.1:51322         ESTABLISHED 1270/sshd
tcp        0      0 192.168.137.9:22            192.168.137.1:51336         ESTABLISHED 1331/sshd
tcp        0      0 :::22                       :::*                        LISTEN      1131/sshd
tcp        0      0 ::1:631                     :::*                        LISTEN      1004/cupsd
tcp        0      0 :::38082                    :::*                        LISTEN      928/rpc.statd
tcp        0      0 :::111                      :::*                        LISTEN      908/rpcbind

 上面所示,成功启动了ngnix,在80端口运行。

ngnix的进程分为了master 进程和worker进程,前者做为管理进程,管理后者,后者是处理页面请求的进程,worker可以有多个。一般根据CPU核数和负载进行配置多个worker. 我们访问试试:

技术分享

nginx的启动,关闭等等操作命令如下:

[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 可以用来修改配置文件之后,测试配置文件是否有语法错误

 

 

其实质是通过信号量来对nginx进行控制的,所以也可以通过下面的方式来控制nginx:

kill -INT `cat /usr/local/nginx/logs/nginx.pid`

[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 等等

先写到这里,后面继续学习nginx的配置。

nginx入门