
目录
一、Nginx介绍(官网地址:http://nginx.org)
1、nginx是什么?
nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
nginx是一款轻量级的web服务器/反向代理服务器以及电子邮件代理服务器。其特点是占用内存少,并发能力强,实际中nginx的并发能力确实实在同类型的网页服务器中表现较好。
nginx相较于Apache具有占用内存少,稳定性高,并且依靠并发能力强,丰富的模块库以及友好灵活的配置而闻名。在Linux系统下,nginx使用"epoll"事件模型,得益于此,nginx在Linux系统下效率相当高。
2、常见用法:
1.web服务器软件httpd http协议
同类的web服务器软件:apache nginx iis lighttpd
2.代理服务器 反向代理
3.邮箱代理服务器 IMAP POP3 SMTP
4.负载均衡 LB loadblance
3、nginx架构的特点:
1.高可靠:稳定性 master进程 管理调度请求分发到哪一个worker >> worker进行响应请求 一master 多worker
2.热部署:①平滑升级 ②可以快速重载配置
3.高并发:可以同时响应更多的请求,事件epoll模型
4.响应快:尤其在处理静态文件上,响应速度很快 sendfile
5.低消耗:cpu、内存 1w请求,占用内存2-3MB
6.分布式支持:反向代理,七层负载均衡
4、Nginx与Apache之间对比
网络模型: select 性能最低 poll 性能稍好 epoll 性能最强 Apache:select Nginx:epoll5、nginx安装方法
1.yum方式安装
Yum安装nginx,先到nginx官网,下载官方源。 第一步: cd /etc/yum.repos.d vi nginx.repo [nginx-stable] name=nginx stable repo baseurl= gpgcheck=1 enabled=1 gpgkey= module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl= gpgcheck=1 enabled=0 gpgkey= module_hotfixes=true 第二步: yum clean all 第三步: yum makecache 第四步: cd /etc/yum.repos.d mv epel.repo epel.repo.bak 第五步: yum install nginx -y注:安装官方源,必须先注释epel源,在进行yum安装nginx
2.epel源安装
yum install -y3.编译安装
官方稳定版:http://nginx.org/download/nginx-1.20.1.tar.gz 第一步:下载官方包,上传到Linux服务器 解压Nginx压缩包 tar xvf nginx-1.20.1.tar.gz 第二步:进入nginx目录 cd nginx-1.20.1 设置系统配置参数,执行下面 ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_v2_module --with-stream 编译 make make install 或 make && make install参数说明:
参数
作用
--prefix
编译安装到的软件目录
--user
worker进程运行用户
--group
worker进程运行的用户组
--with-http_ssl_module
支持https 需要安装 pcel-devel 依赖
--with-http_stub_status_module
基本状态信息显示,查看请求数、连接数
--with-http_realip_module
定义客户端地址和端口为header头信息,常用于反向代理后的真是IP获取
--with-http_v2_module
配置GOLANG语言时使用
--with-stream
启用TCP/UDP代理服务
目录介绍: /usr/local/nginx
目录
作用
conf
配置文件
html
网站默认目录
logs
日志
sbin
可执行文件[软件启动、停止、重启等操作]
4.nginx目录操作参数
注:编译安装的nginx,默认命令不在系统环境变量中,无法直接使用sytemctl。需要加入到环境变量中。
systemctl status nginx Unit nginx.service could not be found.nginx加入环境变量:
vi /etc/profile export PATH=$PATH:/usr/local/nginx/sbin 执行: source /etc/profilenginx加入system系统管理(切记:文件中内容前面不能有空格)
#进入配置文件进行添加内容 vi /usr/lib/systemd/system/nginx.service [Unit] Description=nginx - high performance web server Documentation= After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop [Install] WantedBy=multi-user.target #重载system服务并启动 systemctl daemon-reload systemctl start nginx cd /usr/local/nginx ./nginx -h命令执行后显示:
[root@web03 sbin]# ./nginx -h nginx version: nginx/1.20.1 Usage: nginx [-?hvVtTq] [-s signal] [-p prefix] [-e filename] [-c filename] [-g directives] Options: #查看帮助 -?,-h : this help #查看版本并推出 -v: show version and exit #查看版本和配置选项并推出 -V: show version and configure options then exit #检测配置文件语法并推出 -t: test configuration and exit #检测配置文件语法打印并推出 -T: test configuration, dump it and exit #在配置测试期间禁止显示非错误信息 -q: suppress non-error messages during configuration testing #发送信号给主进程 stop强制推出 quit优雅推出 reopen重开日志 reload重载配置 -s signal : send signal to a master process: stop, quit, reopen, reload #设置nginx目录 $prefix路径 -p prefix : set prefix path (default: /usr/local/nginx/) #设置错误日志的路径 -e filename : set error log file (default: logs/error.log) #指定启动使用的配置文件 -c filename : set configuration file (default: conf/nginx.conf) #在配置文件之外设置全局指令 -g directives : set global directives out of configuration file注:常用
-s参数控制管理nginx服务
-V参数查看nginx开启的模块和编译参数
-t参数检测配置文件是否错误
编译安装nginx时,会遇到以下几个问题点:
解决依赖:
yum install prce-devel zlib-devel openssl-devel注:安装openssl依赖时,默认把zlib-devel 已安装上。
依赖安装成功:
安装步骤及脚本:
#!/bin/bash #编译安装Nginx nginx_install(){ #创建软件运行用户 `id www` &>>/dev/null if [ $? -ne 0 ];then useradd -s/sbin/nologin -M www fi #安装依赖 yum -y install pcre-devel zlib-devel openssl-devel #编译安装 cd /root/soft tar xvf nginx-1.20.1.tar.gz cd nginx-1.20.1.tar.gz ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_v2_module --with-stream && make && make install } #脚本开始时间 start_time=`date +%s` #执行的脚本代码 nginx_install #脚本结束时间 end_time=`date +%s` #脚本执行花费时间 const_time=$((end_time-start_time)) echo Take time is: $const_times6、Nginx配置文件说明
路径:/etc/nginx/nginx.conf
# 工作进程启动用户 usernginx; # 启动的worker进程数 worker_processesauto; # 指定错误日志的路径 error_log/var/log/nginx/error.log notice; # 指定nginx进程的PID pid/var/run/nginx.pid; # 配置事件 events { # worker进程中最大的连接数 worker_connections1024; } # http配置模块 http { # include 是讲其他文件导入进来 include /etc/nginx/mime.types; # default_type 指定nginx处理文件的默认类型 default_typeapplication/octet-stream; # 定义日志的格式 log_formatmain$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"; # 定义日志路径 access_log/var/log/nginx/access.logmain; # 高效读取文件 sendfileon; #tcp_nopush on; # 长连接的超时时间 keepalive_timeout65; # 设置GZIP压缩 #gzipon; # 加载其他的配置文件 include /etc/nginx/conf.d/*.conf; } 说明:include /etc/nginx/conf.d/*.conf; 加载其他配置的好处在于,方便管理,不同网站使用单独一个配置文件,修改起来也比较方便 /etc/nginx/conf.d/ 配置文件都存放于这个目录下 添加完新的配置文件后执行命令:nginx -t 查看配置文件是否正确 [root@web01 nginx]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful7、实战演练,网页版小蝌蚪聊天室
源码下载地址:
链接:
提取码:xm5m
第一步: 上传源码到nginx目录 /usr/share/nginx/ 第二步: 添加nginx配置文件 /etc/nginx/conf.d vim xkd.conf server{ #域名,此域名非正式域名,需要到windows电脑下更改hosts文件 server_name test.xkd.com; #监听端口 listen 80; location / { #指定源码路径 root /usr/share/nginx/xkd; #指定网站方式默认首页 index index.html; } } 第三步: 执行命令:nginx-t 检测配置文件是否正确 第四步: 重启nginx服务 systemctl restart nginx 第五步: 更改本地hosts文件 C:\Windows\System32\drivers\etc\hosts 本机IP test.xkd.com注:nginx日志地址:/var/log/nginx/