Nginx之安装与配置
1. nginx是什么?
nginx是一个开源的,支持高性能高并发的www服务和代理服务软件。由一个俄罗斯人开发的。除了www服务和反向代理,还有负载均衡和缓存服务。
2. nginx应用环境
nginx可以运行在UNIX,Linux,BSD,Mac OS X,Solaris以及Microsoft Windows等操作系统上。
当前流行的Nginx Web组合被称为LNMP或者LEMP。
nginx的官网地址:http://nginx.org
3. nginx软件的使用排名
https://w3techs.com/technologies/overview/web_server/all

4. Nginx的特性
1. 可针对静态资源高速高并发访问及缓存。
2. 可使用反向代理加速,并且可进行数据缓存。
3. 具有简单负载均衡,节点健康检查和容错功能。
4. 支持远程FastCGI的缓存加速。
5. 支持SSL,TLS。
6. 具有模块化的架构。
5. 面试时可能面对的问题
1. 支持高并发:能支持几万并发连接(特别是静态小文件业务环境)
2. 资源消耗少:在3万并发连接,开启10个nginx线程消耗的内存不足200MB。
3. 支持负载均衡功能,类似于Haproxy或LVS的功能。
4. 具有squid的缓存功能。
5. 支持异步网络I/O事件模型epoll(Linux2.6+)
6. Nginx软件的主要企业功能
1. 作为web服务软件
支持更多的并发连接,同时消耗内存还小。
2. 反向代理和负载均衡
3. 前端业务数据缓存功能
通过自身的proxy_cache模块实现类squid等专业缓存软件的功能。
7. Nginx作为web服务软件的应用场景
1. 使用nginx运行html,js,css,小图片等静态数据
2. nginx结合fastcgi运行php动态程序(fastcgi_pass方式)
3. nginx结合tomcat/resin支持java动态程序(proxy_pass方式)
8. Nginx与其他web软件的对比
1. apache基于传统的select模型,并发性不高;nginx采用epoll或者kqueue模型,并发性高。
2. apache加入其他插件功能,不需要重新编译;而nginx需要。
3. apache进程消耗资源较高;而nginx很小。
4. Lighttpd扩展比nginx灵活;但是国内使用率不高。
9. 为什么nginx总体性能比apache高?
因为nginx采用的是epoll模型,而apache采用的是传统的select模型。
epoll与select模型之间的区别:
| 指标 | select | epoll |
| 性能 | 随着连接数的增加,性能急剧下降。 | 随着连接数的增加,性能基本没有下降。 |
| 连接数 | 连接数有限制 | 连接数无限制 |
| 内在处理机制 | 线性轮询 | 回调callback |
| 开发复杂性 | 低 | 中 |
10. 编译安装nginx
1. 安装nginx所需的pcre库。
安装pcre是为了nginx具备URL重写的rewrite模块。
yum install pcre pcre-devel -y
2. 安装openssl-devel
安装openssl是为了使用https的模块。
yum install openssl openssl-devel -y
3. 编译安装nginx的整个流程,如下:
1. 下载nginx的稳定版。
wget http://nginx.org/download/nginx-1.12.2.tar.gz
2. 查看系统中是否已经安装pcre和openssl。
rpm -qa pcre openssl pcre-devel openssl-devel
没有,则安装以上程序,使用yum install安装就可以了,没必要使用编译安装。
3. 还需要注意一下,是否安装gcc和gcc-c++。
rpm -qa gcc gcc-c++
没有,则安装,yum install gcc gcc-c++ -y
4. 创建nginx的连接账户
useradd nginxuser -s /sbin/nologin -M
5. 将下载好的文件解压缩。
tar -zxvf nginx-1.12.2.tar.gz
6. 进入解压后的目录,执行编译。
./configure --user=nginxuser --group=nginxuser --prefix=/application/nginx-1.12.2 --with-http_stub_status_module --with-http_ssl_module
出现下面的结果,表示编译环境没有问题。

7. 接着继续执行make&&make install,完成安装。
输入一个命令:echo $?
结果为0,表示安装过程中没有出错。
8. ln -s /application/nginx-1.12.2 /application/nginx
这个操作的目的是:为了方便知道nginx的版本,同时也是为了升级方便。
9. 安装完成后,启动nginx。
进入到安装目录下/application/nginx/sbin下,执行: ./nginx
这样就启动了nginx服务。
查看一下启动端口:lsof -i:80

也可以使用:netstat -lnt | grep 80

查看一下进程:
ps -ef | grep nginx

10. 客户端测试:
1. windows系统:
直接访问站点即可。
2. linux系统:
使用curl命令或者wget命令。
11. 安装过程中错误调试
1. ./configure: error: SSL modules require the OpenSSL library.
这个错误是因为缺少openssl,yum install openssl openssl-devel -y
2. ./configure: error: the HTTP XSLT module requires the libxml2/libxslt
libraries. You can either do not enable the module or install the libraries.
解决:yum -y install libxslt*
3. ./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.
解决:yum -y install gd-devel
12. 启动的错误
1. nginx: [emery] getpwnam("nginx") failed
useradd nginx -s /sbin/nologin -M
2. 缺少gcc
yum install gcc-c++ -y
13. Nginx服务的启停控制
1. Nginx服务的信号控制
| 信号 | 作用 |
| TERM或INT | 快速停止nginx服务 |
| QUIT | 平缓停止nginx服务 |
| HUP | 使用新的配置文件启动进程,之后平缓停止原有进程,平滑重启 |
| USR1 | 重新打开日志文件,常用于日志切割 |
| USR2 | 使用新版本的nginx文件启动服务,之后平缓停止原有nginx进程,平滑升级 |
| WINCH | 平缓停止worker process,用于nginx服务器平滑升级 |
kill SIGNAL nginx的主进程PID
2. Nginx服务的命令行
./nginx
./nginx -s stop
14. 设置nginx服务的开启启动
1. CentOS6
在/etc/rc.d/rc.local中,添加:
/application/nginx/sbin/nginx
或者在/etc/init.d目录下,创建一个文件nginx,内容如下:
#! /bin/sh
# chkconfig: 2345 55 25
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
case "$1" in
start)
echo -n "Starting $NAME... "
if netstat -tnpl | grep -q nginx;then
echo "$NAME (pid `pidof $NAME`) already running."
exit 1
fi
$NGINX_BIN -c $CONFIGFILE
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
stop)
echo -n "Stoping $NAME... "
if ! netstat -tnpl | grep -q nginx; then
echo "$NAME is not running."
exit 1
fi
$NGINX_BIN -s stop
if [ "$?" != 0 ] ; then
echo " failed. Use force-quit"
exit 1
else
echo " done"
fi
;;
status)
if netstat -tnpl | grep -q nginx; then
PID=`pidof nginx`
echo "$NAME (pid $PID) is running..."
else
echo "$NAME is stopped"
exit 0
fi
;;
force-quit)
echo -n "Terminating $NAME... "
if ! netstat -tnpl | grep -q nginx; then
echo "$NAME is not running."
exit 1
fi
kill `pidof $NAME`
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
reload)
echo -n "Reload service $NAME... "
if netstat -tnpl | grep -q nginx; then
$NGINX_BIN -s reload
echo " done"
else
echo "$NAME is not running, can't reload."
exit 1
fi
;;
configtest)
echo -n "Test $NAME configure files... "
$NGINX_BIN -t
;;
*)
echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}"
exit 1
;;
esac
chkconfig --add nginx
chkconfig --level 2345 nginx on
2. CentOS7
1. 在系统服务目录里创建nginx.service文件
touch /lib/systemd/system/nginx.service
2. 文件中添加如下内容
[Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target
3. 设置开机启动
systemctl enable nginx.service
4. 其它命令
systemctl start nginx.service
systemctl stop nginx.service
systemctl disable nginx.service
systemctl status nginx.service
15. Nginx的目录结构
1. conf
nginx.conf 主配置文件
fastcgi.conf fastcgi的配置文件
mime.types 支持的mime.types资源类型
2. html
默认站点所在的路径
index.html 默认首页文件
50x.html 错误页面文件
3. logs
默认日志文件所在的目录
access.log 默认访问日志文件
error.log 默认错误日志文件
nginx.pid nginx的pid文件
4. sbin
默认启动nginx的二进制文件
16. nginx的主配置文件详解
worker_processes 1; #worker进程的数量--一般与cpu核数一致
events {
worker_connections 1024; #单个worker进程支持的最大TCP连接数
}
http {
include mime.types; #定义文件类型
default_type application/octet-stream; #如果在上面的文件中找不到对应的类型,那么以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; http状态码,使用50x.html回应客户端
location = /50x.html {
root html;
}
}
}

浙公网安备 33010602011771号