nginx 简介
nginx因具有高并发(特别是静态资源)、占用系统资源少等,且功能丰富
在功能应用方面,nginx不但是一个优秀的web服务软件,还具有反向代理负载均衡和缓存服务功能,在反向代理、负载均衡方面,它类似于大名鼎鼎的LVS负载均衡及HAPROXY等专业代理软件,但是NGINX部署起来更方便,在缓存服务功能方面,它又类似于Squid等专业的缓存服务软件
总结一下nginx的几大功能:
- www web服务
- 负载均衡(也可以叫做反向代理,也可以叫做proxy)
- web 缓存(相当于squid,CDN主要使用squid)
nginx版本选择
nginx的软件有三种版本,稳定版、开发版、及历史稳定版
选择标准如下:
- 开发版更新快,新功能多,但bug多
- 稳定版的更新很慢,但是bug较少,可以做为企业生产环境的首选。
但是在实际工作中,尽量避免使用最新的稳定版,应该选择比已出版本来的最新版本晚6-10个月的版本比较好
官网的解释是这样的:
- Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版
- Stable version:最新稳定版,生产环境上建议使用的版本
- Legacy versions:遗留的老版本的稳定版
nginx 安装之RPM安装
关于使用rpm还是源码安装nginx的思考
rpm安装极为简单,下载一个yum源,然后yum install即可
源码最大的好处就是可以定制模块,并可以在安装过后重新编译增加新模块。
而rpm则不行,这也是一大劣势
我们这里还是选择rpm安装,因为rpm安装主要的包其实已经安装好了,也就基本够我们使用了
进入官网
右侧栏选择download,那么你就看到了最近的nginx版本
也看到了nginx 的几种安装方式,有源码安装,还有rpm安装,我们选择rpm安装
nginx之所以强大,是因为它具有众多的功能模块,rpm安装不能定制化安装你需要的功能模块,但是一般的都已经添加了

按照官网:
[root@bogon ~]# cat /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
gpgcheck=0
enabled=1
之后
yum install -y nginx
Installed:
nginx.x86_64 1:1.12.2-1.el7_4.ngx
至此nginx 安装完毕
rpm 安装之后 nginx 简单命令
-
nginx 命令查看
nginx -h
查看命令,或者man nginx -
nginx 启动
直接输入nginx -
nginx 检查配置文件有无错误
nginx -t
在启动或者重启nginx之前一定要nginx -t
检查配置文件
- nginx 关闭
nginx -s stop 关闭
nginx -s reload 重启
- nginx 默认启动 80端口
[root@bogon ~]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 11887 root 6u IPv4 1168066 0t0 TCP *:http (LISTEN)
nginx 11888 nginx 6u IPv4 1168066 0t0 TCP *:http (LISTEN)
- 使用curl 命令检查
[root@bogon ~]# curl 127.0.0.1
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
rpm 安装 nginx 主要配置文件
[root@bogon opt]# rpm -ql nginx
/etc/logrotate.d/nginx #Nginx 日志轮转,用于logrotate服务的日志切割
/etc/nginx
/etc/nginx/conf.d #主配置文件目录
/etc/nginx/conf.d/default.conf #主配置文件
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/modules #nginx 模块目录
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx
/usr/lib64/nginx/modules #nginx 模块目录
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx #nginx 启动进程
/usr/sbin/nginx-debug #nginx 启动进程
/usr/share/doc/nginx-1.12.2 #帮助手册等
/usr/share/doc/nginx-1.12.2/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx #nginx 缓存目录
/var/log/nginx #nginx 日志目录
rpm 安装的nginx的模块
nginx之所以强大,是因为它具有众多的功能模块
nginx -V 可以看出你rpm编译时使用了哪些参数,但是注意这是不全的,因为有一些模块是默认安装的,rpm安装nginx有一个不好的地方就是不能够再次添加新module了
[root@bogon ~]# nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx
--modules-path=/usr/lib64/nginx/modules
--conf-path=/etc/nginx/nginx.conf
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--pid-path=/var/run/nginx.pid
--lock-path=/var/run/nginx.lock
--http-client-body-temp-path=/var/cache/nginx/client_temp
--http-proxy-temp-path=/var/cache/nginx/proxy_temp
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
--http-scgi-temp-path=/var/cache/nginx/scgi_temp
--user=nginx --group=nginx --with-compat --with-file-aio
--with-threads --with-http_addition_module
--with-http_auth_request_module
--with-http_dav_module
--with-http_flv_module
--with-http_gunzip_module
--with-http_gzip_static_module
--with-http_mp4_module
--with-http_random_index_module
--with-http_realip_module
--with-http_secure_link_module
--with-http_slice_module
--with-http_ssl_module
--with-http_stub_status_module
--with-http_sub_module
--with-http_v2_module
--with-mail
--with-mail_ssl_module
--with-stream
--with-stream_realip_module
--with-stream_ssl_module
--with-stream_ssl_preread_module
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
nginx 配置文件详解
[root@bogon ~]# cat /etc/nginx/nginx.conf
user nginx; #nginx服务的系统使用用户
worker_processes 1; #worker进程的数量
error_log /var/log/nginx/error.log warn; #nginx的错误日志
pid /var/run/nginx.pid; #nginx服务启动时的pid文件
---以上叫做nginx 的核心区---
events {
worker_connections 1024; #每个worker进程支持的最大连接数
}
---以上叫做events区-------
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on; #开启高效传输模式
#tcp_nopush on;
keepalive_timeout 65; #连接超时
#gzip on;
include /etc/nginx/conf.d/*.conf; #这个意思就是我们可以在这个目录下面写很多nginx 的配置文件,nginx 会先读/etc/nginx/nginx.conf 主配置文件,再去读这个目录下的配置文件。
}
----以上是nginx http核心模块区域,里面可以嵌套server虚拟主机---
配置调优
work_process 8
一般来说拥有几个逻辑cpu,work_process 就设置为几
nginx doesn't benefit from more than one worker per CPU.
但是worker_processes最多开启8个,8个以上性能提升不会再提升了,而且稳定性变得更低,所以8个进程够用了。
配置完毕后,重启nginx ,执行/etc/init.d/nginx restart
我们的cpu核数是32个,所以我们就设置个最大值8就可以了
在服务器上执行top,然后按1,就可以看到CPU内核的工作情况。如果多个CPU内核的利用率都相差不多,证明nginx己经成功的利用了多核CPU。
worker_connections 10000;
这个一般在做企业调优时是必须要改的一个参数
这个一般调节到10000个左右就可以满足企业需求了,最大应该可以到65535
理论上每台nginx服务器的最大连接数为
worker_processes*worker_connections
网友评论