一、Nginx概念简介
``Nginx
是一个轻量级的高性能HTTP
反向代理服务器,同时它也是一个通用类型的代理服务器,支持绝大部分协议,如TCP、UDP、SMTP、HTTPS
等。

Nginx
与redis相同,都是基于多路复用模型构建出的产物,因此它与Redis
同样具备资源占用少、并发支持高 的特点,在理论上单节点的Nginx
同时支持5W
并发连接,而实际生产环境中,硬件基础到位再结合简单调优后确实能达到该数值。
客户端请求处理流程的对比:

原本客户端是直接请求目标服务器,由目标服务器直接完成请求处理工作,但加入Nginx
后,所有的请求会先经过Nginx
,再由其进行分发到具体的服务器处理,处理完成后再返回Nginx
,最后由Nginx
将最终的响应结果返回给客户端。
二、Nginx环境搭建
官网地址:http://nginx.org/
第一步:首先创建Nginx
的目录并进入
[root@localhost]# mkdir /soft && mkdir /soft/nginx/
[root@localhost]# cd /soft/nginx/
第二步;下载Nginx
的安装包
可以通过FTP
工具上传离线环境包,也可通过wget
命令在线获取安装包
[root@localhost]# wget https://nginx.org/download/nginx-1.21.6.tar.gz

没有wget
命令的可通过yum
命令安装:
[root@localhost]# yum -y install wget
第三步:解压Nginx
的压缩包:
[root@localhost]# tar -xvzf nginx-1.21.6.tar.gz

第四步:下载并安装Nginx
所需的依赖库和包:
[root@localhost]# yum install --downloadonly --downloaddir=/soft/nginx/ gcc-c++

[root@localhost]# yum install --downloadonly --downloaddir=/soft/nginx/ pcre pcre-devel4

[root@localhost]# yum install --downloadonly --downloaddir=/soft/nginx/ zlib zlib-devel

[root@localhost]# yum install --downloadonly --downloaddir=/soft/nginx/ openssl openssl-devel

也可以通过yum
命令一键下载(推荐上面哪种方式):
[root@localhost]# yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
执行完成后,然后ls
查看目录文件,会看一大堆依赖:

紧接着通过rpm
命令依次将依赖包一个个构建,或者通过如下指令一键安装所有依赖包:
[root@localhost]# rpm -ivh --nodeps *.rpm

第五步:Nginx
的配置脚本
进入解压后的nginx
目录,然后执行Nginx
的配置脚本,为后续的安装提前配置好环境,默认位于/usr/local/nginx/
目录下(可自定义目录):
[root@localhost]# cd nginx-1.21.6
[root@localhost]# ./configure --prefix=/soft/nginx/

第六步:编译并安装Nginx
[root@localhost]# make && make install

第七步:最后回到前面的/soft/nginx/
目录,输入ls
即可看见安装nginx
完成后生成的文件。

第八步:修改安装后生成的conf
目录下的nginx.conf
配置文件
[root@localhost]# vi conf/nginx.conf
修改端口号:listen 80;
修改IP地址:server_name 你当前机器的本地IP(线上配置域名);
第九步:制定配置文件并启动Nginx
[root@localhost]# sbin/nginx -c conf/nginx.conf
[root@localhost]# ps aux | grep nginx
Nginx
其他操作命令:
sbin/nginx -t -c conf/nginx.conf # 检测配置文件是否正常
sbin/nginx -s reload -c conf/nginx.conf # 修改配置后平滑重启
sbin/nginx -s quit # 优雅关闭Nginx,会在执行完当前的任务后再退出
sbin/nginx -s stop # 强制终止Nginx,不管当前是否有任务在执行
第十步:开放80
端口,并更新防火墙
[root@localhost]# firewall-cmd --zone=public --add-port=80/tcp --permanent

[root@localhost]# firewall-cmd --reload

[root@localhost]# firewall-cmd --zone=public --list-ports

第十一步:在Windows/Mac
的浏览器中,直接输入刚刚配置的IP
地址访问Nginx
