Nginx是web服务软件。 Nginx是一个开源且高性能、可靠的http web服务、代理服务的软件 1、开源 2、Nginx web服务器 3、Nginx是俄罗斯一个程序员 4、Nginx还是代理
Nginx特点 1)高性能,高并发nginx支持很高的并发,nginx在处理大量并发的情况下比其他web服务要快 2)轻量且高扩展性#轻量 功能模块少,只保留核心模块,其他代码模块化 (易读,便于二次开发,对于开发人员非常友好) #高扩展性 需要什么模块再安装模块,不需要全部安装,并且还支持第三方模块 3) 高可靠性只要不过分几乎不会出现问题 其他的web服务需要每隔一段时间进行重启,nginx不需要 nginx的宕机时间,是99999级别 4) 支持热部署nginx可以再运行期间,更新迭代,代码部署 5)大多数公司都在用nginx 1.Nginx技术成熟,具备的功能是企业最常使用而且最需要的 2.适合当前主流架构趋势, 微服务、云架构、中间层 3.统一技术栈, 降低维护成本, 降低技术更新成本 6) Nginx使用的是Epool网络模型 Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。 Epoll: 当用户发起一次请求,epoll模型会直接进行处理,效率高效,并无连接限制。 7)nginx应用场景Nginx和Apache之间对比
网络模型: select 性能最低 poll 性能稍好 epoll 性能最强
Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。
Epoll: 当用户发起一次请求,epoll模型会直接进行处理,效率高效,并无连接限制。
Apache : select Nginx : epoll
其他的web服务
1.apache:
httpd,最早期使用的web服务,性能不高,操作难
2.nginx
tengine:Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性
openresty-nginx:OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。
3.IIS:windows下的web服务
4.lighttpd:是一个德国人领导的开源 Web 服务器软件,其根本的目的是提供一个专门针对高性能网站,安全、快速、兼容性好并且灵活的 Web Server 环境。具有非常低的内存开销,CPU 占用率低,效能好,以及丰富的模块等特点。
5.GWS:google web server
6.BWS:baidu web server
Tomcat
Resin
weblogic
Jboss
安装Nginx
官网:https://nginx.org/
1、官方源 # (重要:所有的配置必须顶格写) [root@web01 ~]# vim /etc/yum.repos.d/nginx.repo [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [root@web01 ~]# yum clean all [root@web01 ~]# yum makecache
[root@web01 ~]# yum install nginx -y /etc/nginx/nginx.conf
注意:如果安装官方源,epel必须注释掉。 2、epel源 https://developer.aliyun.com/mirror/epel?spm=a2c6h.13651102.0.0.3e221b11yU721Z wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo 清空缓存 [root@web02 ~]# yum clean all 重新生成 [root@web02 ~]# yum makecache [root@web02 ~]# yum install nginx
优化配置:
[root@web01 ~]# scp /etc/nginx/nginx.conf 192.168.15.8:/etc/nginx/
源代码编译安装(公司对nginx有自定义时安装) https://nginx.org/download/nginx-1.20.1.tar.gz 1、下载源代码包 [root@web03 ~]# wget https://nginx.org/download/nginx-1.20.1.tar.gz
2、解压 [root@web03 ~]# tar -xf nginx-1.20.1.tar.gz
3、进入nginx目录并且设置系统配置参数 [root@web03 nginx-1.20.1]# ./configure --help [root@web03 nginx-1.20.1]# ./configure --with-http_ssl_module --with-http_v2_module --with-stream --with-http_ssl_module # 配置HTTPS时使用 --with-http_v2_module # 配置GOLANG语言时使用 --with-stream # 启用TCP/UDP代理服务
4、开始编译 [root@web03 nginx-1.20.1]# make
5、安装 [root@web03 nginx-1.20.1]# make install
6、 加入环境变量 [root@web03 nginx]# vi /etc/profile export PATH=$PATH:/usr/local/nginx/sbin
重载 [root@web03 nginx]# source /etc/profile
重载后测试 [root@web03 ~]# nginx -v nginx version: nginx/1.20.1
7、加入system系统管理(重要:一定要顶格写) [root@web03 sbin]# vi /usr/lib/systemd/system/nginx.service
[Unit] Description=nginx - high performance web server documentation=http://nginx.org/en/docs/ 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 8、重载system服务并启动 [root@web03 sbin]# systemctl daemon-reload [root@web03 sbin]# systemctl start nginx
过滤查看进程数:
ps -ef | grep nginx
错误: 1、./configure: error: the HTTP rewrite module requires the PCRE library. yum install pcre pcre-devel -y # pcre 原本的包 # pcre-devel 扩展的包 2、./configure: error: SSL modules require the OpenSSL library. yum install openssl openssl-devel -yNginx常用命令详讲
nginx -v : 展示nginx的版本号 nginx -V :展示nginx版本号和配置参数 nginx -t : 测试配置文件 nginx -T : 测试配置文件并打印 nginx -q : 静默输出错误信息 nginx -s : 向nginx发送信号 nginx -p : 指定运行的家目录 nginx -e : 设置错误日志的路径 nginx -c : 设置配置文件 nginx -g : 临时设置配置项Nginx的配置文件
Nginx的进程模型 角色: master : 负责监控worker进程 worker :负责处理HTTP请求案例:搭建小游戏
1、服务器准备
2、nginx安装参考上面安装
3、上传代码
上传到这个路径下: /usr/share/nginx/
4、Nginx配置文件参数详解
# 工作进程启动用户
user nginx;
# 启动的worker进程数
worker_processes auto;
# 指定错误日志的路径
error_log /var/log/nginx/error.log notice;
# 指定nginx进程的PID
pid /var/run/nginx.pid;
# 配置事件
events {
# worker进程中最大的连接数
worker_connections 1024;
}
# http配置模块
http {
# include 是将其他文件导入进来
include /etc/nginx/mime.types;
# default_type 指定nginx处理文件的默认类型
default_type application/octet-stream;
# 定义日志的格式
log_format main '$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.log main;
# 高效读取文件
sendfile on;
#tcp_nopush on;
# 长连接的超时时间
keepalive_timeout 65;
# 设置GZIP压缩
#gzip on;
# 加载其他的配置文件
include /etc/nginx/conf.d/*.conf;
}
5、增加网站的配置
1、vim /etc/nginx/conf.d/game.conf
server {
server_name game.test.com;
listen 80;
location / {
root /usr/share/nginx/html5-mario;
index index.html;
}
}
# 游戏配置文件详解
game.conf
# 每一个server都代表一个网站(nginx是支持多个网站)
server {
# 指定当前网站的域名
server_name www.baidu.com;
# 指定当前网站的端口
listen 80;
# 指定一个访问路径
location / {
# 指定站点目录
root /opt/html;
# 指定默认访问的文件
index index.html;
}
}
2、测试nginx配置文件是否正确 [root@web02 conf.d]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful 3、启动nginx [root@web02 conf.d]# systemctl restart nginx 4、编写Windows的hosts文件 C:WindowsSystem32driversetc
如果无法修改hosts文件,请修改权限
6、测试
浏览器输入
http://game.test.com/
查看文件类型:
cat /etc/nginx/mime.types
查看配置端口:
netstat -nutlp
持久化:
service iptables save



