Nginx简介
Nginx是一个高性能的Http和反向代理Web服务器,同时也提供了IMAP/POP3/SMTP服务。
- 占用内存少,并发能力强;
- 官方给出数据,最高支持5W个并发连接数的相应。
Nginx作用
- 反向代理:代理服务器;正向代理:代理客服端
- 负载均衡:内置策略和扩展策略
- 内置策略:
- 轮训:将请求依次发到指定服务器,每个服务机会均等
- 加权轮训(权重):权重越高,接收请求越多,
- Ip hash:对客户端请求的ip进行hash操作,根据hash结果,将同一个客户端ip的请求分发给同一台服务器进行处理,解决Session不共享问题,但是服务器挂掉,数据将丢失。(建议使用Redis做Session共享)
- 扩展策略:略
- 内置策略:
- 动静分离:
Nginx安装
Windows安装
- 下载解压即可使用
Linux安装
如果未安装gcc 和gcc-c++可能需要先安装
yum -y install gcc
yum -y install gcc-c++
开始安装具体步骤如下:
whereis nginx #检查是否安装Nginx
tar -zxvf 安装包 #解压安装包
./configure #自动配置
执行自动配置报错,具体错误信息去下:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
需要安装PCRE,具体命令如下:
yum -y install pcre-devel openssl openssl-devel
安装成功后,继续操作=
./configure #自动配置
make #执行make命令
make install #执行make install
whereis nginx #查看安装目录/usr/local/nginx
cd /usr/local/nginx/sbin
./nginx #启动nginx 访问ip:80/
如果出现Linux可以访问失败,则查看端口是否开放
firewall-cmd --list-ports
#8080/tcp
firewall-cmd --zone=public --add-port=80/tcp --parmanent #开启80端口
systemctl restart firewalled.service #重启防火墙
firewall-cmd --list-ports #查看端口
Nginx常用命令
./nginx #启动
./nginx -s stop #停止
./nginx -s quit #安全退出
./nginx -s reload #重新加载配置文件
ps -ef|grep nginx #查看Nginx进程
Nginx使用
后期补充
刚开始学习Nginx,如果不对之处,请各位大佬指正!
此文章根据视频自己总结,视频链接:狂神说