Nginx 简介
一、Nginx概述
1.1 概述
Nginx(“engine x”)是一个高性能的 HTTP /反向代理的服务器及电子邮件(IMAP/POP3)代理服务器。

官方测试nginx能够支撑5万并发,并且cpu,内存等资源消耗却非常低,运行非常稳定。最重要的是开源,免费,可商用的。
Nginx还支持热部署,几乎可以做到7 * 24 小时不间断运行,即时运行数个月也不需要重启,还能够在不间断服务的情况下对软件进行升级维护。
1.2 Nginx应用场景
1、单机环境下参考服务器配置。 并发连接数在7000+ -8000左右。 集群模式20000+。
2、作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使 Nginx尤其受到虚拟主机提供商的欢迎。能够支持高达 50,000 个并发连接数的响应。
3、作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP,也可以支持作为 HTTP代理服务器 对外进行服务。Nginx 用 C 编写, 不论是系统资源开销还是 CPU 使用效率都比 Perlbal 要好的多。
4、作为邮件代理服务器:Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last.fm 描述了成功并且美妙的使用经验。
5、Nginx 安装非常的简单,配置文件 非常简洁(还能够支持perl语法),Bug非常少的服务器。
二、Nginx安装
2.2 安装相关依赖
2.2.1 第一步
1、 避免:Nginx 与 httpd 发生端口冲突
卸载:可能已经安装的 httpd,从而避免发生<端口冲突>
1
| (rpm -qa | grep -P "^httpd-([0-9].)+") && rpm -e --nodeps httpd || echo "未安装"
|
2、 下载:Nginx 源码安装包,并解压
1 2 3
| cd ~ which wget || yum install -y wget wget http://nginx.org/download/nginx-1.19.1.tar.gz
|
3、安装其他依赖
1
| yum install -y gcc pcre-devel zlib-devel
|
4、创建运行账户nginx
1
| useradd -M -s /sbin/nologin nginx
|
2.3 安装nginx
解压nginx-xx.tar.gz包
1
| tar -axf nginx-1.19.1.tar.gz
|
进入解压目录,执行./configure 设置安装路径和运行账户
1 2
| cd ~/nginx-1.19.1 ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
|
make&&make install
配置网页
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
| cat >/usr/local/nginx/conf/nginx.conf <<EOF worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; charset utf-8; server { listen 80; server_name localhost; include conf.d/*.conf; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } EOF
[ -d /usr/local/nginx/conf/conf.d ] || mkdir -p /usr/local/nginx/conf/conf.d
|
2.3 设置环境变量,同时设置开机自启
1、设置变量
1 2 3
| cat > /etc/profile.d/nginx.sh<<EOF export PATH="/usr/local/nginx/sbin:\$PATH" EOF
|
2、刷新环境
3、启停:Nginx 服务进程
1 2 3
| echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.d/rc.local chmod +x /etc/rc.d/rc.local nginx 或 nginx -c /usr/local/nginx/conf/nginx.conf
|
2.4 访问
直接浏览器输入虚拟机ip地址测试
三、nginx常用命令和配置文件
3.1 常用命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| nginx -v
nginx -t
nginx
nginx -s stop
nginx -s reload
|
3.2 配置文件详细讲解
1 2
| 位置:/usr/local/nginx/conf/nginx.conf
|
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
| ★ 查看:<Nginx 主配置文件>的<默认配置>
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; charset utf-8; server { listen 80; server_name localhost; include conf.d/*.conf; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
|
3.3 理解:<Nginx 主配置文件>的<语法格式>及<默认配置>
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
| ┍ main 主配置段 ┤ worker_processes 1; │ │ └ ┍ events { evens 事件配置段 ┤ worker_connections 1024; └ } ┍ http { │ include mime.types; │ default_type application/octet-stream; │ sendfile on; │ keepalive_timeout 65; │ server { ─────────────────────────────┐ │ listen 80; │ http 网站配置段 ┤ server_name localhost; │ │ ┍ location / { │ │ │ root html; │ │ │ index index.html index.htm; ├定义:虚拟主机 │ └ } │ │ error_page 500 502 503 504 /50x.html; │ │ ┍ location = /50x.html { │ │ │ root html; │ │ └ } │ │ }─────────────────────────────────────┘ └ }
|
四、Nginx反向代理与负载均衡
4.1 反向代理
反向代理:正好相反。对于客户端来说,反向代理就好像目标服务器。并且客户端不需要进行任何设置。客户端向反向代理发送请求,接着反向代理判断请求走向何处,并将请求转交给客户端,使得这些内容就好像它自己的一样,一次客户端并会并会不感知到反向代理后面的服务,因此不需要客户端做任何设置,只需要把反向代理服务器当成真正的服务器就好了。
4.2 负载均衡
负载均衡建立在现有网络结构之上,它提供一种链家有效透明的方法扩展网络设备和服务器的宽带、增加吞吐量,加强网络数据处理能力,提高网络的灵活性和可用性。
Nginx proxy反向代理模块 (默认安装)
功能 1:可以作为<应用程序网关>
对外隐藏:<内网服务器>的<IP地址>
对外发布:<内网服务器>的<服务资源>
功能 2:可以实现<动静分离>
通过 location URI 地址匹配,实现:转发<动态网页的请求>。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| vim /usr/local/nginx/conf/nginx.conf location / { proxy_pass http://服务器池; } 或 proxy_pass http://后端服务器池的名字;
vim /usr/local/nginx/conf/nginx.conf .... location / { .... proxy_pass http://xm; } ....
|
Nginx upstream 上游模块(负载均衡)
#指令 和 功能
upstream 定义:一个命名的<后端服务器池>
server 定义:服务器池里的服务器
ip_hash 启用:基于<IP地址哈希值>的<负载均衡算法>
负载均衡的部署方式
1.轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除
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 35 36 37 38 39 40 41 42 43
| vim /usr/local/nginx/conf/nginx.conf .... http { upstream xm { server ip地址:80; server ip地址:80; } ... } 保存后出去重载文件 nginx -s reload
vim /usr/local/nginx/conf/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; charset utf-8; upstream test { server 192.168.106.147:80; server 192.168.106.148:80; } server { listen 80; server_name localhost; include conf.d/*.conf; location / { root html; index index.html index.htm; proxy_pass http://test; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
|
2.weight (权重)
权重越大,被分配到的任务越多,被访问的概率越高
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 35 36 37 38 39 40 41 42 43 44 45
| vim /usr/local/nginx/conf/nginx.conf .... http { upstream xm { server ip地址:80 weight=7; server ip地址:80 weight=3; } ... }
保存后出去重载文件 nginx -s reload
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; charset utf-8; upstream test { server 192.168.106.147:80 weight=7; server 192.168.106.148:80 weight=3; } server { listen 80; server_name localhost; include conf.d/*.conf; location / { root html; index index.html index.htm; proxy_pass http://test; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
|
3.ip_hash(哈希算法)
客户第一次访问某个服务器后短时间断开,再次访问自动定位到该服务器
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 35 36 37 38 39 40 41 42 43 44 45 46
| vim /usr/local/nginx/conf/nginx.conf .... http { upstream xm { ip_hash; server ip地址:80; server ip地址:80; } ... }
保存后出去重载文件 nginx -s reload
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; charset utf-8; upstream test { ip_hash; server 192.168.106.147:80; server 192.168.106.148:80; } server { listen 80; server_name localhost; include conf.d/*.conf; location / { root html; index index.html index.htm; proxy_pass http://test; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
|
五、Nginx+php动静分离
为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速度,降低原来单个服务器的压力。一般来说,都需要将动态资源和静态资源分开,由于Nginx的高并发和静态资源缓存等特性,经常将静态资源部署在Nginx上。如果请求的是静态资源,直接到静态资源目录获取资源,如果是童泰资源的请求,则利用反向代理的原理,把请求转发给对应后台应用去处理,从而实现动静分离。
1. 动态和静态分离主要是通过nginx + PHP FPM实现的,其中nginx处理图片、html等静态文件,PHP处理动态程序。
2. 动态静态分离是指在web服务器架构中,将静态页面与动态页面或静态内容接口与动态内容接口分离,从而提高整个服务的访问性能和可维护性的架构设计方法。
3. 简单地说,当用户请求时,如果他只是访问静态请求,比如图片和html, nginx会直接返回。如果他发送了一个动态请求,nginx会把这个请求发送给程序进行动态处理
1、配合php实现动态页面和静态页面分开处理
1 2 3 4 5 6 7 8
| 1、删除httpd rpm -e httpd --nodeps 2、安装php及其组件 yum install -y php php-devel php-mysql yum install -y php-fpm 3、启动php及其组件,同时将其加入开机自启 systemctl enable php-fpm systemctl start php-fpm
|
2、修改运行用户
1 2
| sed -i -r 's/^\s*user\s*=.*/user = nginx/' /etc/php-fpm.d/www.conf sed -i -r 's/^\s*group\s*=.*/group = nginx/' /etc/php-fpm.d/www.conf
|
重启服务
1
| systemctl restart php-fpm
|
3、修改配置文件
1 2 3 4 5 6 7 8 9 10 11 12
| cat > /usr/local/nginx/conf/conf.d/location_php.conf <<EOF location ~ \.php$ { root html; fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name; if (!-f \$document_root\$fastcgi_script_name) { return 404; } } EOF
|
重载nginx
4、编写php页面
1 2 3 4 5
| cat > /usr/local/nginx/html/index.php <<EOF <?php phpinfo(); ?> EOF
|