Nginx 简介
nginx 是一个高性能的Web和反向代理服务器,它具有非常优越的特性
nginx 能支持高达50000个并发连接数的响应,用于高连接并发情况 使用epol and kqueue作为开发模型
nginx 作为负载均衡服务器:nginx 即可在内部直接支持和PHP程序对外进行服务,也可支持作为HTTP代理服务器对外进行服务
nginx采用C进行编写,不论系统资源开销还是CPU使用效率都比Perlbal要好
Nginx 的优点
高并发连接:官方测试能够支撑5万并发连接,在实际的生产环境中跑到2~3万的并发连接数
内存消耗少:在三万并发连接下,开启的10个nginx进程才消耗150M的内存
配置文件简单、成本低廉、支持Rewrite重写规则(能够根据域名、URL的不同 ,将HTTP请求分到不同的后端服务器群组
Nginx的基本功能
静态资源的Web服务器,能缓存打开的文件描述符
http、smtp、pop3协议的反向代理服务器
缓存加速、负载均衡 支持FastCGI (fpm,LNMP),uWSGI (Python)等 模块化
(非DSO机制),过滤器zip、SSI及图像的大小调整 支持SSL
Nginx 的扩展功能
基于名称和IP的虚拟主机
支持keepalive
支持平滑升级
定制访问日志,支持使用日志缓冲区提高日志存储性能
支持URL重写
支持路径别名
支持基于IP及用户的访问控制
支持速率限制,支持并发数限制
Nginx 的应用类别
使用nginx结合FastCGI运行的PHP、 JSP 、Perl等程序
使用nginx作反代理、负载均衡、过滤规则
使用nginx运行静态HTML网页、图片
nginx与其他新技术的结合应用
nginx的模块分类
nginx的模块从结构上分为核心模块、基础模块和第三方模块
HTTP模块、EVENT模块和MAIL模块等属于核心模块
HTTP Access模块、HTTP FastCGI模块、HTTP Proxy模块和HTTP Rewrite模块属于基本模块
HTTP Upstream模块、Request Hash模块、Notice模块和HTTP Access Key模块属于第三方模块
Nginx的安装
关闭防火墙和selinux
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
创建系统用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
安装依赖环境
#需用网络源安装依赖包。
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@localhost ~]# yum -y groups mark install 'Development Tools' #安装开发包
创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx
下载nginx,编译安装
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz
[root@localhost src]# tar xf nginx-1.12.0.tar.gz #解压
[root@localhost src]# cd nginx-1.12.0
[root@localhost nginx-1.12.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@localhost nginx-1.12.0]# make -j $(grep 'processor' /proc/cpuinfo | wc
-l) && make install
启动nginx
#配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/pr
ofile.d/nginx.sh
[root@localhost ~]# . /etc/profile.d/nginx.sh
[root@localhost ~]# nginx
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 127.0.0.1:6011 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 ::1:6011 :::*
访问到nginx 默认页