You got a dream,you gotta protect it.People can’t do something themselves,they wanna tell you you can’t do it.If you want something,go get it.</br> 不要别人告诉你该做什么,有梦想,就得保护。他人做不成什么事情,就跟你说你也做不成。如果你想要什么,就要去争取。——《当幸福来敲门》
基本概述
Nginx 是互联网主流的高性能 http 和 反响代理 Web 服务器,Nginx 不但可以作为 Web 服务器,它还提供了静态资源服务、缓存、负载均衡 等功能。
不论你是前端程序员还是后端程序员,对于Nginx的接触,应该不会陌生。对于前端和后端来说,它就像是沟通桥梁,类似粘合剂的作用。 尤其是在前后端分离的时代,Nginx出场的频率和一线偶像明星出镜的次数差不多。 在云原生[Cloud Native]时代,Envoy的出现,似乎增加了我们技术选型的可能性。当然Envoy和Nginx都采用了 多线程 + 非阻塞 + 异步IO(Libevent) 的架构模式。
实战搭建Nginx
基于Centos 7部署Nginx
- 安装Nginx相关依赖</br>
#安装使nginx支持rewrite yum -y install pcre* #安装使nginx支持gcc-c++ yum -y install gcc-c++ #安装使nginx支持zlib yum -y install zlib* #安装使nginx支持openssl yum -y install openssl
- 下载安装nginx-1.19.8.tar.gz安装包,上传到服务器目录: /usr/local</br>
[root@centos-pivtoal ~]# cd /usr/local [root@centos-pivtoal local]# ls aegis bin cloudmonitor etc games include lib lib64 libexec nginx-1.19.8.tar.gz sbin share src [root@centos-pivtoal local]# tar -xvf nginx-1.19.8.tar.gz [root@centos-pivtoal local]# cd nginx-1.19.8 [root@centos-pivtoal nginx-1.19.8]# ls auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src [root@centos-pivtoal nginx-1.19.8]#
- 配置安装检查: ./configure</br>
[root@centos-pivtoal nginx-1.19.8]# ./configure [root@centos-pivtoal nginx-1.19.8]#
- 编译并安装Nginx: make && make install
[root@centos-pivtoal nginx-1.19.8]# make && make install [root@centos-pivtoal nginx-1.19.8]#
- 配置开机自启动Nginx</br>
[1]. 在/lib/systemd/system/在目录创建nginx.service:
[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
[注意事项]:</br> [Unit]: 服务的说明 </br> Description:描述服务</br> After:描述服务类别</br> [Service]服务运行参数的设置</br> Type:forking是后台运行的形式</br> ExecStart为服务的具体运行命令</br> ExecReload为重启命令</br> ExecStop为停止命令</br> PrivateTmp:True表示给服务分配独立的临时空间</br> [Service]的启动、重启、停止命令全部要求使用绝对路径</br> [Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3</br>
[2]. 添加系统自启动</br>
#设置开机启动 systemctl enable nginx.service #启动nginx服务 systemctl start nginx.service #检查nginx服务状态 systemctl status nginx.service #重新启动服务 systemctl restart nginx.service #查看所有已启动的服务 systemctl list-units --type=service
基于Dokcer部署Nginx
- 拉取Nginx镜像</br>
[root@centos-pivtoal ~]# docker pull nginx Using default tag: latest latest: Pulling from library/nginx a076a628af6f: Pull complete 0732ab25fa22: Pull complete d7f36f6fe38f: Pull complete f72584a26f32: Pull complete 7125e4df9063: Pull complete Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest [root@centos-pivtoal ~]# docker tag docker.io/library/nginx:latest nginx:latest [root@centos-pivtoal ~]#
- 编写Docker部署脚本</br>
docker run -itd -p 80:80 -p 443:443 --name nginx-server --network-alias nginx-server --hostname nginx-server --restart always -v /docker/nginx/conf/conf.d/:/etc/nginx/conf.d/ -v /docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/application/html:/usr/share/nginx/html nginx:latest
- 执行脚本命令
docker run -itd -p 80:80 -p 443:443 --name nginx-server --network-alias nginx-server --hostname nginx-server --restart always -v /docker/nginx/conf/conf.d/:/etc/nginx/conf.d/ -v /docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/application/html:/usr/share/nginx/html nginx:latest
实战配置Nginx
- 设置Nginx进程数目[worker_processes]:默认可以设置为CPU的核数相等,并发比较大的时候,可以设置为cpu核数*2
#查看服务器cpu信息:cat /proc/cpuinfo | grep processor [root@iZm5efc4cs8k6t2agr7tceZ manager]# cat /proc/cpuinfo | grep processor processor : 0 processor : 1 processor : 2 processor : 3 [root@iZm5efc4cs8k6t2agr7tceZ manager]#
添加配置举例: [1] .4 CPU (4 Core) + 4 worker_processes(每个worker_processes 使用1个CPU) </br> worker_processes 4;</br> worker_cpu_affinity 0001 0010 0100 1000;</br> [2].8 CPU (8 Core) + 8 worker_processes (每个worker_processes 使用1个CPU)</br> worker_processes 8;</br> worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;</br> [3].16 CPU (16 Core) + 16 worker_processes (每个worker_processes 使用1个CPU)</br> worker_processes 16;</br> worker_cpu_affinity 0000000000000001 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 0000000010000000 0000000100000000 0000001000000000 0000010000000000 0000100000000000 0001000000000000 0010000000000000 0100000000000000 1000000000000000;
- 配置事件处理模型[events]:</br>
#配置Nginx worker进程最大打开文件数 worker_rlimit_nofile 65535; events { #使用高性能的 epoll 事件驱动,处理效率高 use epoll; accept_mutex on; #打开同时接受多个新网络连接请求的功能 multi_accept on; #单个进程允许的客户端最大连接数 worker_connections 65535; }
- 开启高效传输模式</br>
#开启高效文件传输模式 sendfile on; #需要在sendfile开启模式才有效,防止网路阻塞,积极的减少网络报文段的数量。将响应头和正文的开始部分一起发送,而不一个接一个的发送。 tcp_nopush on; tcp_nodelay on;
- 开启传输压缩 </br>
gzip on; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;