一、Nginx是什么
Nginx(engine X)是一个开源的轻量级的HTTP服务器,能够提供高性能的HTTP和反向代理服务。与传统的Apache服务器相比,在性能上Nginx占用系统资源更小、支持高并发,访问效率更高;在功能上,Nginx不仅作为Web服务软件,还适用于反向代理、负载均衡等场景;在安装配置上,Nginx更为简单、灵活。Nginx因为并发性能和资源占用上的优势,已经广泛用于大中型互联网企业。
1. Nginx 功能介绍
- 静态的web资源服务器html,图片,js,css,txt等静态资源
- http/https协议的反向代理
- 结合FastCGI/uWSGI/SCGI等协议反向代理动态资源请求
- tcp/udp协议的请求转发(反向代理)
- imap4/pop3协议的反向代理
2. Nginx 特点
-
支持高并发:Nginx是专门为性能优化而开发的,采用内核Poll模型,单机能够支持几万以上的并发连接
-
低资源消耗:Nginx采取了分阶段资源分配技术,使得CPU与内存的占用率非常低。一般1万个非活跃的HTTP Keep-Alive连接在Nginx中仅消耗几MB内存
-
高拓展性:设计极具扩展性,由多个不同功能、不同层次、不同类型且耦合度极低的模块组成
-
高可用性:Nginx支持热部署,其中的master管理进程与worker工作进程的分离设计;启动速度特别迅速,因此可以在不间断服务的情况下,对软件版本或者配置进行升级,即使运行数月也无需重新启动,几乎可以做到7x24小时不间断地运行
-
丰富的使用场景:可以作为Web服务端、HTTP反向代理、负载均衡和前端缓存服务等场景使用
-
开源协议:使用BSD许可协议,免费使用,且可修改源码
3. Nginx框架模型及进程通信
3.1 框架模型流程:
3.2进程组件角色:
-
master进程
-
监视工作进程的状态
-
当工作进程死掉后重启一个新的
-
处理信号和通知工作进程
-
-
worker进程
-
处理客户端请求
-
从主进程处获得信号做相应的事情
-
-
cache loader进程
-
加载缓存索引文件信息,然后退出
-
-
cache manager进程
-
管理磁盘的缓存大小,超过预定值大小后最少使用数据将被删除
-
3.4 Nginx进程结构
采用多进程模型好处:首先,对于每个worker进程来说,独立的进程,不需要加锁,所以省掉了锁带来的开销,同时在编程以及问题查找时,也会方便很多。其次,采用独立的进程,可以让互相之间不会影响,一个进程退出后,其它进程还在工作,服务不会中断,master进程则很快启动新的worker进程。当然,worker进程的异常退出,肯定是程序有bug了,异常退出,会导致当前worker上的所有请求失败,不过不会影响到所有请求,所以降低了风险。
- 对外接口:接收外部的操作(信号)
- 对内转发:根据外部的操作的不同,通过信号管理 Worker
- 监控:监控 worker 进程的运行状态,worker 进程异常终止后,自动重启 worker 进程
- 读取Nginx 配置文件并验证其有效性和正确性
- 建立、绑定和关闭socket连接
- 按照配置生成、管理和结束工作进程
- 接受外界指令,比如重启、升级及退出服务器等指令
- 不中断服务,实现平滑升级,重启服务并应用新的配置
- 开启日志文件,获取文件描述符
- 不中断服务,实现平滑升级,升级失败进行回滚处理
- 编译和处理perl脚本
- 所有 Worker 进程都是平等的
- 实际处理:网络请求,由 Worker 进程处理
- Worker进程数量:一般设置为核心数,充分利用CPU资源,同时避免进程数量过多,导致进程竞争CPU资源,增加上下文切换的损耗
- 接受处理客户的请求
- 将请求依次送入各个功能模块进行处理
- I/O调用,获取响应数据
- 与后端服务器通信,接收后端服务器的处理结果
- 缓存数据,访问缓存索引,查询和调用缓存数据
- 发送请求结果,响应客户的请求
- 接收主程序指令,比如重启、升级和退出等
3.5 Nginx 进程间通信
3.6 Nginx 启动和 HTTP 连接建立
3.7 HTTP 处理过程
4. Nginx 模块介绍
nginx高度模块化:
- 核心模块:是 Nginx 服务器正常运行必不可少的模块,提供错误日志记录 、配置文件解析 、事件
- 驱动机制 、进程管理等核心功能
- 标准HTTP模块:提供 HTTP 协议解析相关的功能,比如: 端口配置 、 网页编码设置 、 HTTP响应头设置 等等
- 可选HTTP模块:主要用于扩展标准的 HTTP 功能,让 Nginx 能处理一些特殊的服务,比如:Flash
- 多媒体传输 、解析 GeoIP 请求、 网络传输压缩 、 安全协议 SSL 支持等
- 邮件服务模块:主要用于支持 Nginx 的 邮件服务 ,包括对 POP3 协议、 IMAP 协议和 SMTP协议的支持
- Stream服务模块: 实现反向代理功能,包括TCP协议代理
- 第三方模块:是为了扩展 Nginx 服务器应用,完成开发者自定义功能,比如: Json 支持、 Lua 支持等
#模块分类
核心模块:core module
标准模块:
HTTP 模块: ngx_http_*
HTTP Core modules #默认功能
HTTP Optional modules #需编译时指定
Mail 模块: ngx_mail_*
Stream 模块: ngx_stream_*
第三方模块
5. Nginx 安装
5.1 Nginx编译安装
源码安装需要提前准备标准的编译器, GCC 的全称是( GNU Compiler collection ),其有 GNU 开发,并以 GPL即 LGPL 许可,是自由的类 UNIX 即苹果电脑 Mac OS X 操作系统的标准编译器,因为 GCC 原本只能处理 C 语 言,所以原名为GNU C 语言编译器,后来得到快速发展,可以处理 C++,Fortran , pascal , objective C , java以及 Ada 等其他语言,此外还需要 Automake 工具,以完成自动创建 Makefile 的工作, Nginx 的一些模块 需要依赖第三方库,比如: pcre (支持 rewrite ), zlib (支持 gzip 模块)和 openssl (支持 ssl 模块)等
https://nginx.org/en/download.html
[root@nginx ~]# yum install gcc pcre-devel zlib-devel openssl-devel -y
#先安装源码编译器
#将下载好的文件添加到root/下
[root@nginx ~]# ls
公共 视频 文档 音乐 anaconda-ks.cfg nginx-1.24.0 #安装稳定版
模板 图片 下载 桌面
[root@nginx ~]# cd nginx-1.24.0/
[root@nginx nginx-1.24.0]# ls
auto CHANGES.ru configure html src CHANGES
conf contrib LICENSE man README
#编译环境
[root@nginx nginx-1.24.0]# ./configure --prefix=/usr/local/nginx/ \
> --user=nginx \ # 指定nginx运行用户
> --group=nginx \ # 指定nginx运行组
> --with-http_ssl_module \ # 支持https://
> --with-http_v2_module \ # 支持http版本2
> --with-http_realip_module \ # 支持ip透传
> --with-http_gzip_static_module \ # 支持压缩
> --with-http_stub_status_module \ # 支持状态页面
> --with-pcre \ # 支持正则
> --with-stream \ # 支持tcp反向代理
> --with-stream_ssl_module \ # 支持tcp的ssl加密
> --with-stream_realip_module # 支持tcp的透传ip
执行命令后check检查除了error日志
( nginx error log file: "/usr/local/nginx//logs/error.log"),没有其他error出现就ok;出现问题大多数是少依赖,源码编译的时候装的一系列开发包
[root@nginx nginx-1.24.0]# ls #软件编译环境后会生成 Makefile 文件用 make 命令来编译Nginx
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
#以下操作了解不需要敲
[root@nginx nginx-1.24.0]# make clean #重复环境编译时,可以还原,还原后再执行前面的操作
rm -rf Makefile objs
[root@nginx nginx-1.24.0]# ls #查看就没Makefile objs(编译过程中真正执行make命令的软件,生成的所有中间文件和最终的可执行文件都会被放置在objs目录下)
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
#执行编译任务
[root@nginx nginx-1.24.0]# make -j2 #make并行执行编译任务,-j选项后面跟的数字:同时运行两个cpu核(数字根据自己的写),编译快慢取决于cpu速度和内存,最后显示
make[1]: 离开目录“/root/nginx-1.24.0” 就是编译成功
[root@nginx nginx-1.24.0]#
[root@nginx nginx-1.24.0]# cd objs/
[root@nginx objs]# ls
autoconf.err nginx ngx_auto_config.h ngx_modules.c src
Makefile nginx.8 ngx_auto_headers.h ngx_modules.o
[root@nginx objs]# cd src/ #Nginx的功能模块
[root@nginx src]# ls
core event http mail misc os stream
#cp objs文件到usr/local/nginx/
[root@nginx ~]# cd nginx-1.24.0/
[root@nginx nginx-1.24.0]# make install
make -f objs/Makefile install
make[1]: 进入目录“/root/nginx-1.24.0”
test -d '/usr/local/nginx/' || mkdir -p '/usr/local/nginx/'
test -d '/usr/local/nginx//sbin' \
|| mkdir -p '/usr/local/nginx//sbin'
test ! -f '/usr/local/nginx//sbin/nginx' \
|| mv '/usr/local/nginx//sbin/nginx' \
'/usr/local/nginx//sbin/nginx.old'
cp objs/nginx '/usr/local/nginx//sbin/nginx'
test -d '/usr/local/nginx//conf' \
|| mkdir -p '/usr/local/nginx//conf'
cp conf/koi-win '/usr/local/nginx//conf'
cp conf/koi-utf '/usr/local/nginx//conf'
cp conf/win-utf '/usr/local/nginx//conf'
test -f '/usr/local/nginx//conf/mime.types' \
|| cp conf/mime.types '/usr/local/nginx//conf'
cp conf/mime.types '/usr/local/nginx//conf/mime.types.default'
test -f '/usr/local/nginx//conf/fastcgi_params' \
|| cp conf/fastcgi_params '/usr/local/nginx//conf'
cp conf/fastcgi_params \
'/usr/local/nginx//conf/fastcgi_params.default'
test -f '/usr/local/nginx//conf/fastcgi.conf' \
|| cp conf/fastcgi.conf '/usr/local/nginx//conf'
cp conf/fastcgi.conf '/usr/local/nginx//conf/fastcgi.conf.default'
test -f '/usr/local/nginx//conf/uwsgi_params' \
|| cp conf/uwsgi_params '/usr/local/nginx//conf'
cp conf/uwsgi_params \
'/usr/local/nginx//conf/uwsgi_params.default'
test -f '/usr/local/nginx//conf/scgi_params' \
|| cp conf/scgi_params '/usr/local/nginx//conf'
cp conf/scgi_params \
'/usr/local/nginx//conf/scgi_params.default'
test -f '/usr/local/nginx//conf/nginx.conf' \
|| cp conf/nginx.conf '/usr/local/nginx//conf/nginx.conf'
cp conf/nginx.conf '/usr/local/nginx//conf/nginx.conf.default'
test -d '/usr/local/nginx//logs' \
|| mkdir -p '/usr/local/nginx//logs'
test -d '/usr/local/nginx//logs' \
|| mkdir -p '/usr/local/nginx//logs'
test -d '/usr/local/nginx//html' \
|| cp -R html '/usr/local/nginx/'
test -d '/usr/local/nginx//logs' \
|| mkdir -p '/usr/local/nginx//logs'
make[1]: 离开目录“/root/nginx-1.24.0”
[root@nginx ~]# cd /usr/local/nginx/sbin/
[root@nginx sbin]# ls
nginx
[root@nginx sbin]# useradd -s /sbin/nologin -M nginx
[root@nginx sbin]# id nginx
用户id=1001(nginx) 组id=1001(nginx) 组=1001(nginx)
[root@nginx sbin]# ./nginx
[root@nginx sbin]# ps aux | grep nginx
[root@nginx sbin]# netstat -antlupe | grep nginx
测试:
[root@nginx sbin]# du -sh nginx
5.4M nginx #编译文件相对比较大
#关闭Debug功能来减小编译文件
[root@nginx ~]# cd nginx-1.24.0/
[root@nginx nginx-1.24.0]# vim auto/cc/gcc
#先清除再重新编译
[root@nginx nginx-1.24.0]# make clean
rm -rf Makefile objs
[root@nginx nginx-1.24.0]# ./configure --prefix=/usr/local/nginx/ > --user=nginx > --group=nginx > --with-http_ssl_module > --with-http_v2_module > --with-http_realip_module > --with-http_gzip_static_module > --with-http_stub_status_module > --with-pcre > --with-stream > --with-stream_ssl_module > --with-stream_realip_module
[root@nginx ~]# vim ~/.bash_profile
[root@nginx ~]# cat ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
export PATH=$PATH:/usr/local/nginx/sbin #把nginx软件的命令执行路径添加到环境变量中
[root@nginx nginx-1.24.0]# cd
[root@nginx ~]# source ~/.bash_profile #重新加载执行当前~/.bash_profile 文件中的命令和设置
[root@nginx ~]# du -sh /usr/local/nginx/sbin/nginx
1.2M /usr/local/nginx/sbin/nginx
[root@nginx ~]# nginx #将nginx重启
测试:
[root@Nginx nginx-1.24.0]# ls /usr/local/nginx/conf html logs sbin
- conf:保存nginx所有的配置文件,其中nginx.conf是nginx服务器的最核心最主要的配置文件,其他的.conf则是用来配置nginx相关的功能的,例如fastcgi功能使用的是fastcgi.conf和fastcgi_params 两个文件,配置文件一般都有一个样板配置文件,是以.default为后缀,使用时可将其复制并将default后缀 去掉即可。
- html目录中保存了nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的web 文件是默认的错误页面提示页面。
- logs:用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比如/var/logs/nginx里面。
- sbin:保存nginx二进制启动脚本,可以接受不同的参数以实现不同的功能
5.2 平滑升级
1)关于版本和备份:下载新版本nginx了解原旧版nginx编译参数将旧nginx二进制文件进行备份,然后替换成新的nginx二进制文件;
2)向旧的Nginx的master进程发送USR2信号:将旧的master进程的pid文件添加后缀.oldbin,master进程会用新nginx二进制文件启动新的master进程;
3)向旧的master进程发送winch信号,旧的worker子进程退出
[root@nginx ~]# wget https://nginx.org/download/nginx-1.26.2.tar.gz
--2024-08-20 16:10:03-- https://nginx.org/download/nginx-1.26.2.tar.gz
正在解析主机 nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:5c0:2600::6, ...
正在连接 nginx.org (nginx.org)|3.125.197.172|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1244789 (1.2M) [application/octet-stream]
正在保存至: “nginx-1.26.2.tar.gz”
nginx-1.26.2.tar.gz 100%[=============================================>] 1.19M 888KB/s 用时 1.4s
2024-08-20 16:10:07 (888 KB/s) - 已保存 “nginx-1.26.2.tar.gz” [1244789/1244789])
[root@nginx ~]# ls
公共 视频 文档 音乐 anaconda-ks.cfg nginx-1.24.0
模板 图片 下载 桌面 echo-nginx-module-0.63 nginx-1.26.2.tar.gz
[root@nginx ~]# tar zxf nginx-1.26.2.tar.gz
[root@nginx ~]# ls
公共 视频 文档 音乐 anaconda-ks.cfg nginx-1.24.0 nginx-1.26.2.tar.gz
模板 图片 下载 桌面 echo-nginx-module-0.63 nginx-1.26.2
#编译新版本
[root@nginx ~]# cd nginx-1.26.2/
[root@nginx nginx-1.26.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --add-module=/root/echo-nginx-module-0.63 --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
#编译安装
[root@nginx nginx-1.26.2]# curl -I 172.25.254.100
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Tue, 20 Aug 2024 08:19:01 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 20 Aug 2024 07:25:22 GMT
Connection: keep-alive
ETag: "66c444e2-267"
Accept-Ranges: bytes
#可更改版本信息避免版本裸露
[root@nginx ~]# cd nginx-1.26.2/src/core/
[root@nginx core]# ls
nginx.c ngx_crypt.h ngx_open_file_cache.c ngx_resolver.c
nginx.h ngx_cycle.c ngx_open_file_cache.h ngx_resolver.h
ngx_array.c ngx_cycle.h ngx_output_chain.c ngx_rwlock.c
ngx_array.h ngx_file.c ngx_palloc.c ngx_rwlock.h
ngx_bpf.c ngx_file.h ngx_palloc.h ngx_sha1.c
ngx_bpf.h ngx_hash.c ngx_parse.c ngx_sha1.h
ngx_buf.c ngx_hash.h ngx_parse.h ngx_shmtx.c
ngx_buf.h ngx_inet.c ngx_parse_time.c ngx_shmtx.h
ngx_conf_file.c ngx_inet.h ngx_parse_time.h ngx_slab.c
ngx_conf_file.h ngx_list.c ngx_proxy_protocol.c ngx_slab.h
ngx_config.h ngx_list.h ngx_proxy_protocol.h ngx_spinlock.c
ngx_connection.c ngx_log.c ngx_queue.c ngx_string.c
ngx_connection.h ngx_log.h ngx_queue.h ngx_string.h
ngx_core.h ngx_md5.c ngx_radix_tree.c ngx_syslog.c
ngx_cpuinfo.c ngx_md5.h ngx_radix_tree.h ngx_syslog.h
ngx_crc32.c ngx_module.c ngx_rbtree.c ngx_thread_pool.c
ngx_crc32.h ngx_module.h ngx_rbtree.h ngx_thread_pool.h
ngx_crc.h ngx_murmurhash.c ngx_regex.c ngx_times.c
ngx_crypt.c ngx_murmurhash.h ngx_regex.h ngx_times.h
[root@nginx core]# vim nginx.h
[root@nginx core]#
[root@nginx nginx-1.26.2]# make #只要 make 无需 make install
#备份旧的Nginx二进制
[root@nginx nginx-1.26.2]# cd objs/
[root@nginx objs]# ls
addon Makefile nginx.8 ngx_auto_headers.h ngx_modules.o
autoconf.err nginx ngx_auto_config.h ngx_modules.c src
[root@nginx objs]# cd /usr/local/nginx/sbin/
[root@nginx sbin]# ls
nginx
[root@nginx sbin]# cp nginx nginx.bak
[root@nginx sbin]# ls
nginx nginx.bak
[root@nginx sbin]# \cp -f /root/nginx-1.26.2/objs/nginx /usr/local/nginx/sbin/nginx
[root@nginx sbin]# ps ax | grep nginx
885 ? Ss 0:00 avahi-daemon: running [nginx.local]
30117 ? Ss 0:00 nginx: master process sbin/nginx
30118 ? S 0:00 nginx: worker process
36435 pts/0 S+ 0:00 grep --color=auto nginx
[root@nginx sbin]# ls
nginx nginx.bak
[root@nginx sbin]#
#向旧master发送winch信号
[root@nginx sbin]# kill -WINCH 39583
[root@nginx sbin]# ps aux | grep nginx
avahi 885 0.0 0.1 15532 6224 ? Ss 10:11 0:00 avahi-daemon: running [nginx.l]
root 39582 0.0 0.0 10052 948 ? Ss 18:36 0:00 nginx: master process /usr/sbiinx
nginx 39584 0.0 0.1 13940 4900 ? S 18:36 0:00 nginx: worker process
nginx 39647 0.0 0.1 13940 4900 ? S 18:41 0:00 nginx: worker process
root 39649 0.0 0.0 221812 2360 pts/0 S+ 18:41 0:00 grep --color=auto nginx
5.3 平滑回滚
思路:替换nginx二进制文件,向旧的master发送USR2信号,向旧的master发送WINCH,向旧的master发送QUIT
[root@nginx sbin]# cp nginx nginx.26
[root@nginx sbin]# ls
nginx nginx.26 nginx.bak
[root@nginx sbin]# mv nginx.bak nginx
mv:是否覆盖'nginx'? y
[root@nginx sbin]# kill -HUP 39647
[root@nginx sbin]# ps aux | grep nginx
avahi 885 0.0 0.1 15532 6224 ? Ss 10:11 0:00 avahi-daemon: running [nginx.local]
root 39582 0.0 0.2 10052 948 ? S 18:36 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 36549 0.0 0.3 9876 6528 ? S 18:41 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 39584 0.0 0.1 13940 4900 ? S 18:36 0:00 nginx: worker process
root 39753 0.0 0.0 221812 2344 pts/0 S+ 19:49 0:00 grep --color=auto nginx
[root@nginx sbin]# ^C
二、Nginx不同场景配置
1. Nginx配置文件介绍
... #全局块
events { #events块
...
}
http #http块
{
... #http全局块
server #server块
{
... #server全局块
location [PATTERN] #location块
{
...
}
location [PATTERN]
{
...
}
}
server
{
...
}
... #http全局块
}
- 全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
-
events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
-
http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
-
server块:配置虚拟主机的相关参数,一个http中可以有多个server。
-
location块:配置请求的路由,以及各种页面的处理情况。
2. 正向代理
简单来说就是Nginx代理客户端来访问互联网。典型代表:翻墙。
最大特点是:客户端非常明确要访问的服务器地址;服务器只清楚请求来自哪个代理服务器,而不清楚来自哪个具体的客户端;正向代理模式隐藏了真实客户端信息。
[root@nginx-master ~]# vim /etc/nginx/nginx.conf #在配置文件中的http块中添加server块
server {
resolver 114.114.114.114; #指定DNS服务器IP地址
listen 80;
location / {
proxy_pass http://localhost:80; #设定代理服务器的协议和地址
proxy_set_header HOST $host;
proxy_buffers 256 4k;
proxy_max_temp_file_size 0k;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_next_upstream error timeout invalid_header http_502;
}
}
server {
resolver 114.114.114.114; #指定DNS服务器IP地址
listen 443;
location / {
proxy_pass https://localhost:443; #设定代理服务器的协议和地址
proxy_buffers 256 4k;
proxy_max_temp_file_size 0k;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_next_upstream error timeout invalid_header http_502;
}
}
Linux客户端访问测试:配置htp_proxy和https_proxy变量如下:IP为Nginx proxy server IP想要永久生效请配置到/etc/profile文件
3. 反向代理
Nginx作为反向代理服务器接收来自客户端的请求,并将请求转发给后端的真实服务器集群中的一台。典型代表:负载均衡;最大特点:主要用于服务器进群分布式部署的情况下,隐藏了服务器的真实信息!
反向代理配置:
配置关键: 1) server_name: 代表客户端向服务器发起请求时输入的域名
2)proxy_pass: 代表源服务器的访问地址, 即真正处理请求的服务器地址(服务器+端口号)
示例一:客户端访问 http://www.yuki.com:80, 实际访问的是http://www.yuki.com:8081,也就是http://192.168.93.101:8081,只是做了一个端口的转换。
server {
#监听端口
listen 80;
#服务器名称,也就是客户端访问的域名地址
server_name www.yuki.com;
#nginx日志输出文件
access_log logs/nginx.access.log main;
#nginx错误日志输出文件
error_log logs/nginx.error.log;
root html;
index index.html index.htm index.php;
location / {
#被代理服务器的地址
proxy_pass http://localhost:8081; #localhost IP 为192.168.93.101
#对发送给客户端的URL进行修改的操作
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
}
}
4. Nginx通过反向代理实现负载均衡
注意:upstream{}配置在http块中的全局块中
首先我们介绍下Nginx实现负载均衡的几种方式:
1)轮询
默认模式,每个请求按照顺序逐一分配到不同的后端服务器。
优点:配置简单,只需要将服务器列表加入到upstream模块中即可。
#负载中有三台服务器,当请求到达时,nginx按照时间顺序将请求分配给这三台服务器
upstream serverList {
server 1.2.3.4;
server 1.2.3.5;
server 1.2.3.6;
}
2)加权轮询
每个请求按照5:3:2的权重分配到后端服务器。
#当请求到达时,nginx按照时间顺序和权重把请求分配给三台服务器处理。例如10个请求,30%的4处理,30%的3处理,20%的2处理
upstream serverList {
server 1.2.3.4 weight=5;
server 1.2.3.5 weight=3;
server 1.2.3.6 weight=2;
}
3)ip_hash
每个请求按照IP的hash结果分配,同一个IP客户端访问一个固定的后端服务器。
优点:可以保证来自同一个IP的请求,被打到固定的机器上,可以解决session的问题。
#负载中有三台服务器,当请求到达时,nginx会优先按照ip_hash的结果进行分配,其他则按照时间顺序把请求分配给三台服务器处理。
upstream serverList {
ip_hash
server 1.2.3.4;
server 1.2.3.5;
server 1.2.3.6;
}
4)url_hash
按访问url的hash结果来分配请求,相同url固定转发到同一个后端服务器进行处理。
upstream serverList {
server 1.2.3.4;
server 1.2.3.5;
server 1.2.3.6;
hash $request_uri;
hash_method crc32;
}
5)fair
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream serverList {
server 1.2.3.4;
server 1.2.3.5;
server 1.2.3.6;
fair;
}
备注:
-
每种模式可携带的参数有:
-
down:当前服务器暂不参与负载;
-
weight:权重,值越大,服务器的负载量越大;
-
max_fails:允许请求失败的次数,默认为1;
-
fail_timeout:max_fails次失败后暂停的时间;
-
backup:备份机,只有其他所有的非backup机器down或者忙时才会请求backup机器。
-
如:
# 负载中三台服务器,服务器4的失败超时时间 60s,服务器5暂不参与负载,服务器6只是作为备份机。
# upstream:是负载的配置模块,serverList是名称,随便起
upstream serverList {
server 1.2.3.4 fail_timeout=60s;
server 1.2.3.5 down;
server 1.2.3.6 backup;
}
Nginx实现负载均衡的常用的几种方式我们先介绍到这里, 下面我们来看实现负载均衡的配置:
负载均衡示例(关键配置)
# upstream:是负载的配置模块,serverList是名称,随便起
upstream serverList {
server 192.168.93.110; #web server1,后端真实服务器
server 192.168.93.111; #web server2,后端真实服务器
}
server {
listen 80;
server_name www.xxx.com; # server_name:是客户端请求的域名地址
root html;
index index.html index.htm index.php;
location / {
proxy_pass http://serverList; # 指向负载的列表的模块,如serverList
proxy_redirect off;
proxy_set_header Host $host;
}
}
测试:
访问网页http://www.yuki.com:80,可以看到请求轮询定向到web server1 和web server2. 负载均衡功能验证成功。
5. nginx作为静态网页服务器
配置:
server {
listen 80;
server_name www.xxx.com; #静态网站访问的域名地址;
client_max_body_size 1024M;
location / {
root /var/www/static; #直接静态项目绝对路径的根目录;
index index.html;
}
}
网页配置:
测试:
可以访问到该静态网页,测试成功。
6. php相关配置
①安装php依赖
yum install -y bzip2 systemd-devel libxml2-devel sqlite-devel
libpng-devel libcurl-devel oniguruma-devel
②编译环境等
./configure \
--prefix=/usr/local/php \ #安装路径
--with-config-file-path=/usr/local/php/etc \ #指定配置路径
--enable-fpm \ #用cgi方式启动程序
--with-fpm-user=nginx \ #指定运行用户身份
--with-fpm-group=nginx \
--with-curl \ #打开curl浏览器支持
--with-iconv \ #启用iconv函数,转换字符编码
--with-mhash \ #mhash加密方式扩展库
--with-zlib \ #支持zlib库,用于压缩http压缩传输
--with-openssl \ #支持ssl加密
--enable-mysqlnd \ #mysql数据库
--with-mysqli \
--with-pdo-mysql \
--disable-debug \ #关闭debug功能
--enable-sockets \ #支持套接字访问
--enable-soap \ #支持soap扩展协议
--enable-xml \ #支持xml
--enable-ftp \ #支持ftp
--enable-gd \ #支持gd库
--enable-exif \ #支持图片元数据
--enable-mbstring \ #支持多字节字符串
--enable-bcmath \ #打开图片大小调整,用到zabbix监控的时候用到了这个模块
--with-fpm-systemd #支持systemctl 管理cgi
③配置及优化
server {
listen 80;
server_name www.yuki.org;
root /data/web/php;
index index.html;
location /leo {
root /data/web;
auth_basic "password";
auth_basic_user_file "usr/local/nginx/.htpasswd";
}
}
mkdir /data/web/php
touch /data/web/php/index.php
vim /data/web/php/index.php
<?php
phpinfo();
?>