nginx说解
1.nginx基本概念
nginx是什么,能做什么事情
1. nginx是一个高性能的http和反向代理web服务器
2. 同时也提供IMAP/POP3/SMTP服务
3. 专门为性能优化而开发,占用内存少,并发能力强。nginx能支持高达5W个并发连接数.
反向代理
反向代理是相对正向代理来说的
正向代理:局域网通过正向代理访问internet , nginx可以做正向代理
反向代理:架设在服务器前,对外暴露的是nginx服务器,隐藏真实服务器的IP
负载均衡
将请求分发到多个服务器上,将负载分发到不同的服务器,横向扩容,解决单个服务器无法承载高并发请求问题
动静分离
为加快网站访问速度,可以将动态资源(页面),静态资源(页面)交由不同的服务器来解析,加快网站解析速度,降低单个服务器的压力。
静态资源服务器(jss、css、html、images图片服务器等)
高可用
高可用基于keepalived, 脚本
2.nginx安装、常用命令 配置文件
centos中安装nginx
一. nginx依赖包
1. pcre
说明:解析正则表达式包
网址:https://ftp.pcre.org/pub/pcre/pcre-8.45.tar.bz2
下载:wget https://ftp.pcre.org/pub/pcre/pcre-8.45.tar.bz2
解压 tar -xvf pcre-8.45.tar.bz2
./configure
make && make install
非root账号无权限时:su root 输入密码
或者使用yum install安装
yum install pcre
2. openssl
说明:ssl加密,用于https
网址:https://www.openssl.org/
下载:wget https://www.openssl.org/source/openssl-1.1.1k.tar.gz
解压: tar -zxvf openssl-1.1.1k.tar.gz
安装: ./config && make && make install
3. zlib
说明:用于压缩
网址:http://www.zlib.net/
下载:http://www.zlib.net/zlib-1.2.11.tar.gz
解压:tar -zxvf zlib-1.2.11.tar.gz
安装: ./configure && make && make install
4. nginx
网址:http://nginx.org/
下载:wget http://nginx.org/download/nginx-1.20.1.tar.gz
解压:tar -zxvf nginx-1.20.1.tar.gz
安装:./configure && make && make install
注:也可以使用yum简化安装过程
yum install nginx
防火墙
firewall-cmd --list-all
nginx常用命令
cd /usr/local/nginx/sbin
1. 查看nginx版本号
./nginx -v
2. 启动nginx
./nginx #启动nginx
3. 停止nginx
./nginx -s stop
./nginx -s quit
#区别
1) nginx -s quit : 优雅的退出,nginx在退出前完成已经接收的请求处理。
2) nginx -s stop : 快速关闭,不管有没有正在处理的请求
4. 重加载nginx, 修改nginx配置文件后使用
./nginx -s reload
5. 查看nginx work信息
ps -ef | grep nginx
nginx配置文件
1. nginx配置文件位置
一般位于安装位置: /usr/local/nginx/conf 下的 nginx.conf
2. nginx配置文件 nginx.conf组成
由3部分组成:
1)全局块
从配置文件开始,到events块之间的内容,主要会设置一些影响nginx服务器整体运行的配置命令,主要包括:配置运行nginx服务器的用户(用户组), work process数、进程pid存放路径、日志存放路径和类型、以及配置文件的引入
比如:work_processes 2; # 表示工作进程 work process 数量为2 ,一般为cpu核数
2)events块
events块涉及的指令主要影响 nginx服务器与用户的网络连接,
如:work_connections 1024; # 表示 每个work process支持的最大连接数
3)http块
http块是nginx配置最频繁的部分,代理、缓存和日志定义,以及第三方模块的配置都在这里
注:http块又包括:http全局块 和 server块
3.1) http全局块:
http全局块配置的指令包括文件引入、MIME-TYPE定义、日志自定义、连接超时时间、单连接请求数上限等
3.2) server块:
server块和虚拟主机有密切关系
3.2.1) 全局server块
最常见的配置是本虚拟主机的监听配置、本机名称或IP配置
3.2.2) location块
一个Server块可以包含多个location块
防火墙设置
systemctl start firewalld #启动
systemctl stop firewalld #停止
systemctl disable firewalld #禁用
systemctl status firewalld #状态
systemctl enable firewalld.service #在开机时启用服务
systemctl disable firewalld.service #在开机时禁用一个服务
systemctl is-enabled firewalld.service #查看服务是否开机启动
systemctl list-unit-files | grep enabled #查看已启动的服务
systemctl --failed #查看启动失败的服务列表
firewall-cmd --add-port=8080/tcp --permanent
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --remove-port=8080/tcp --permanent
firewall-cmd --reload # 重新加载防火墙规则
3.nginx配置实例-反向代理
反向代理初探
1. 实现效果
1) 打开浏览器,在浏览器中输入地址 www.mysite.com,跳转到linux系统tomcat主页面中
2. 准备工作
1) 在linux系统中安装tomcat,使用默认端口8080
# tomcat安装文件放到centos中,解压
# 进入tomcat的bin目录中,./startup.sh 启动tomcat
2) 对外开放访问的端口
firewall-cmd --add-port=8080/tcp --permanent
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload # 重新加载防火墙规则
firewall-cmd --list-all #查看防火墙
3) 访问过程
输入www.123.com 通过Host解析将该域名解析至nginx,nginx监听请求并将请求转发至tomcat
整个过程简化为 www.123.com -> nginx -> tomcat
4) 环境:
本机为windows, 虚拟机为centos, IP地址为:http://192.168.30.132/, tomcat端口为8080
5) 具体步骤:
1)本机windows配置hosts文件 (位置:C:\Windows\System32\drivers\etc\hosts)
192.168.30.132 www.mysite.com
2) centos配置nginx.conf
server {
listen 80; #监听80端口
server_name 192.168.30.132;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://192.168.30.132:8080/; #将请求转发至 192.168.30.132的tomcat8080端口,此处也可以是另一台IP
root html;
index index.html index.htm;
}
}
根据路径代理不同服务节点
通过不同路径访问不同步的服务器
访问http://www.mysite.com/teacher 跳转至192.168.30.132:8080端口
访问http://www.mysite.com/student 跳转至192.168.30.132:8081端口
准备工作:
1) 两个tomcat服务器,一个8080端口,一个8081端口
2) 准备两个页面,分别放至tomcat/webapps/xxx目录下
如:student.html 放在tomcat/webapps/student目录下 (一个tomcat目录下)
cd tomcat/webapps
mkdir student
cd student
echo '<h1> student info !!! </1>' > student.html
如:teacher.html 放在tomcat/webapps/teacher目录下(另一个tomcat目录下)
cd tomcat/webapps
mkdir teacher
cd teacher
echo '<h1> teacher info !!! </h1>' > teacher.html
location /student/ {
proxy_pass http://192.168.30.132:8080/student/student.html;
root html;
index index.html index.htm;
}
location /teacher/ {
proxy_pass http://192.168.30.132:8081/teacher/teacher.html;
root html;
index index.html index.htm;
}
路径匹配优先级
= :精确匹配
~ :正则匹配,区分大小写
~*:正则匹配,不区分大小写
^~: 前缀正则匹配,区分大小写
^~*: 前缀正则匹配,不区分大小写
/xx/yyy :路径匹配,最长优先匹配
在nginx的location和配置中location的顺序没有太大关系。与location表达式的类型有关。相同类型的表达式,字符串长的会优先匹配。
以下是按优先级排列说明:
第一优先级:等号类型(=)的优先级最高。一旦匹配成功,则不再查找其他匹配项。
第二优先级:^~类型表达式。一旦匹配成功,则不再查找其他匹配项。
第三优先级:正则表达式类型(~ ~*)的优先级次之。如果有多个location的正则能匹配的话,则使用正则表达式最长的那个。
第四优先级:常规字符串匹配类型。按前缀匹配
location = / {
# 精确匹配 / ,主机名后面不能带任何字符串
[ configuration A ]
}
location / {
# 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求
# 但是正则和最长字符串会优先匹配
[ configuration B ]
}
location /documents/ {
# 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
# 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
[ configuration C ]
}
location ~ /documents/Abc {
# 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
# 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
[ configuration CC ]
}
location ^~ /images/ {
# 匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
# 匹配所有以 gif,jpg或jpeg 结尾的请求
# 然而,所有请求 /images/ 下的图片会被 config D 处理,因为 ^~ 到达不了这一条正则
[ configuration E ]
}
location /images/ {
# 字符匹配到 /images/,继续往下,会发现 ^~ 存在
[ configuration F ]
}
location /images/abc {
# 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在
# F与G的放置顺序是没有关系的
[ configuration G ]
}
location ~ /images/abc/ {
# 只有去掉 config D 才有效:先最长匹配 config G 开头的地址,继续往下搜索,匹配到这一条正则,采用
[ configuration H ]
}
4.nginx配置实例-负载均衡
5.nginx配置实例-动静分离
6.nginx配置高可用集群
乒乓狂魔