您现在的位置是:首页 > 伤感句子

Nginx三大功能详解

作者:亙句时间:2024-05-07 08:05:28分类:伤感句子

简介  文章浏览阅读2k次,点赞67次,收藏21次。html>JSP, 计算器<%int i = 20;int j = 70;<\/div>

点击全文阅读

文章目录

Nginx(动静分离)1.基本介绍2.需求分析3.思路分析4.先使用传统的方式1.配置win的tomcat1.webapps\search\cal.jsp2.webapps\search\image\cal.jpg3.启动tomcat,访问 http://192.168.200.1:8080/search/cal.jsp 2.配置linux的tomcat1.将image和cal.jsp移动到linux的search目录下2.启动tomcat,访问 http://192.168.200.128:8080/search/cal.jsp3.访问 http://192.168.200.1:8080/search/cal.jsp 3.配置nginx.conf(之前配过)http全局块server块 4.启动nginx,访问 http://look.sunxiansheng.cn:7777/search/cal.jsp 5.动静分离优化1.分析静态资源请求路径资源路径为/search/image/cal.jpg 2.在nginx的安装目录下创建/search/image/来存放静态资源3.修改nginx.conf 配置动静分离4.重载使配置生效 `./sbin/nginx -s reload`5.浏览器访问 http://look.sunxiansheng.cn:7777/search/cal.jsp6.Nginx配置核心 Nginx(反向代理)1.反向代理快速入门1.需求分析2.思路分析3.配置前提1.安装JDK82.安装Tomcat83.测试是否安装成功测试java:输入java -version测试tomcat:进入安装的bin目录 `cd /opt/tomcat/apache-tomcat-8.5.59/bin` 执行 `./startup.sh` 4.测试win下访问tomcat1.查看8080端口是否开放 `firewall-cmd --list-all`2.查看虚拟机ip,`ifconfig`3.win浏览器访问 http://192.168.200.128:8080/4.反向代理不需要开放8080端口,所以关闭 `firewall-cmd --permanent --remove-port=8080/tcp` 然后重载 `firewall-cmd --reload` 4.配置域名解析到虚拟机的ip5.修改安装目录\nginx.conf1.检测配置文件是否正确,进入nginx安装目录 `cd /usr/local/nginx/` 输入 `./sbin/nginx -t`则会检测安装目录下的nginx.conf文件的语法2.上面显示46行有问题,发现少加了一个分号3.重载nginx 使配置文件生效 `./sbin/nginx -s reload` 6.浏览器访问 http://tomcat.sunxiansheng.cn/ 反向代理成功 2.反向代理配置-Location实例1.需求分析2.思路分析3.location语法规则1.语法2.优先级3.匹配的规则 4.实际常用规则(重点)1.第一个必选规则-精确匹配网站根目录(匹配到即返回)2.第二个必选规则-处理静态文件请求(匹配到即返回)有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用 3.第三个规则就是通用规则,用来转发动态请求到后端应用服务器(匹配符合要求的最长location)4.常用规则小结 5.应用实例1.在linux的tomcat下编写 webapps\product\hi.html1.这样的资源完整路径就是http://192.168.200.128:8080/product/hi.html2.进入webapps目录 `cd /opt/tomcat/apache-tomcat-8.5.59/webapps`3.创建product文件夹 `mkdir product`4.编辑hi.html文件 `vim product/hi.html`5.本机测试访问 http://192.168.200.128:8080/product/hi.html 2.在win的tomcat下编写 webapps\member\hello.html1.查看win的ip为 192.168.200.12.这样资源的完整路径就是http://192.168.200.1:8080/member/hello.html3.webapps下创建member文件夹并编写hello.html文件4.启动win的tomcat5.本机测试访问 http://192.168.200.1:8080/member/hello.html 3.保证两端网络连通1.虚拟机ping 192.168.200.1 不通2.关闭win防火墙,再次ping,确定网络互通3.虚拟机直接访问 http://192.168.200.1:8080/member/hello.html 4.配置nginx.conf1.打开nginx.conf `cd /usr/local/nginx/` `vim nginx.conf`2.配置nginx.conf3.重载nginx 使配置文件生效 `./sbin/nginx -s reload`4.查看10000端口是否在监听 `netstat -anp | grep 10000` 5.开启10000端口的防火墙6.windows测试访问http://192.168.200.128:10000/member/hello.htmlhttp://192.168.200.128:10000/product/hi.htmlhttp://tomcat.sunxiansheng.cn:10000/product/hi.html Nginx(负载均衡)1.需求分析2.思路分析3.负载均衡配置规则1.轮询(默认)2.weight3.ip_hash4.fair 4.具体实现1.部署两个服务1.在linux的tomcat下的webapps中创建/search/look.html2.在win的tomcat下的webapps中创建/search/look.html3.重启两个tomcat,并测试nginx服务是否可以访问其他服务1.重启win的tomcat后测试本机访问 http://192.168.200.1:8080/search/look.html2.重启linux的tomcat后测试本机访问 http://192.168.200.128:8080/search/look.html3.测试linux访问 http://192.168.200.1:8080/search/look.html 2.配置域名解析到linux服务器3.配置nginx.conf1.在http全局块配置2.在server块配置3.重载nginx 使配置文件生效 `./sbin/nginx -s reload` 4.开启7777端口的防火墙5.测试win访问 http://look.sunxiansheng.cn:7777/search/look.html 6.示意图7.注意事项1.nginx.conf的upstream不能带下划线2.浏览器不能是无痕的3.tomcat没有监听端口问题 8.负载均衡配置

Nginx(动静分离)

1.基本介绍

image-20240407092401376

image-20240407092415222

2.需求分析

image-20240407092432696

3.思路分析

image-20240407093143923

4.先使用传统的方式

1.配置win的tomcat
1.webapps\search\cal.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>hello, jsp</title></head><body><img src="image/cal.jpg"/><h1>JSP, 计算器</h1><%int i = 20;int j = 70;int res = i + j;out.println(i + " + " + j + " = " + res);%></body></html>
2.webapps\search\image\cal.jpg

image-20240407094735483

3.启动tomcat,访问 http://192.168.200.1:8080/search/cal.jsp

image-20240407095507619

2.配置linux的tomcat
1.将image和cal.jsp移动到linux的search目录下

image-20240407095133005

2.启动tomcat,访问 http://192.168.200.128:8080/search/cal.jsp

image-20240407095611813

3.访问 http://192.168.200.1:8080/search/cal.jsp

image-20240407095902937

3.配置nginx.conf(之前配过)
http全局块

image-20240407100618521

server块

image-20240407100607951

4.启动nginx,访问 http://look.sunxiansheng.cn:7777/search/cal.jsp

image-20240407101529567

image-20240407101453080

5.动静分离优化

1.分析静态资源请求路径
资源路径为/search/image/cal.jpg

image-20240407103858559

2.在nginx的安装目录下创建/search/image/来存放静态资源

image-20240407104353746

3.修改nginx.conf 配置动静分离
这样配置就表示当匹配到jpg结尾的文件时,假如是/search/image/cal.jpg,会将/usr/local/nginx/与/search/image/cal.jpg拼接,来寻找资源

image-20240407104536384

4.重载使配置生效 ./sbin/nginx -s reload
5.浏览器访问 http://look.sunxiansheng.cn:7777/search/cal.jsp
6.Nginx配置核心
服务发现 + 匹配资源路径服务发现 + 拼接资源路径首先部署服务,然后写出使用浏览器怎么访问服务,最后配置Nginx

image-20240407114043512

Nginx(反向代理)

1.反向代理快速入门

1.需求分析

image-20240406111319371

2.思路分析

image-20240406112710731

3.配置前提
1.安装JDK8
2.安装Tomcat8
3.测试是否安装成功
测试java:输入java -version

image-20240406113253774

测试tomcat:进入安装的bin目录 cd /opt/tomcat/apache-tomcat-8.5.59/bin 执行 ./startup.sh

本机访问 http://localhost:8080/

image-20240406113956996

4.测试win下访问tomcat
1.查看8080端口是否开放 firewall-cmd --list-all

image-20240406114219270

2.查看虚拟机ip,ifconfig

image-20240406114258480

3.win浏览器访问 http://192.168.200.128:8080/

image-20240406114352577

4.反向代理不需要开放8080端口,所以关闭 firewall-cmd --permanent --remove-port=8080/tcp 然后重载 firewall-cmd --reload

image-20240406140006329

4.配置域名解析到虚拟机的ip

image-20240406134632187

5.修改安装目录\nginx.conf

image-20240406115959902

1.检测配置文件是否正确,进入nginx安装目录 cd /usr/local/nginx/ 输入 ./sbin/nginx -t则会检测安装目录下的nginx.conf文件的语法

image-20240406120620742

2.上面显示46行有问题,发现少加了一个分号

image-20240406120457538

3.重载nginx 使配置文件生效 ./sbin/nginx -s reload
6.浏览器访问 http://tomcat.sunxiansheng.cn/ 反向代理成功

image-20240406135032639

2.反向代理配置-Location实例

1.需求分析

image-20240406140246207

2.思路分析

image-20240406141323564

3.location语法规则
1.语法

image-20240406150606136

2.优先级
=:精确匹配^~:匹配以某个路径开头的~:正则匹配,区分大小写~*:正则匹配,不区分大小写普通匹配
3.匹配的规则
非普通匹配:只要按照优先级匹配到了,就直接返回普通匹配:会匹配符合要求的最长路径关于普通匹配 /api/ 就相当于/api/* 即只要资源路径的前缀是/api/即可只要是服务就记住两句话 服务发现 = ip + 端口 + 上下文路径(可选)查找资源 = 服务发现 + 资源路径
4.实际常用规则(重点)
1.第一个必选规则-精确匹配网站根目录(匹配到即返回)
服务发现 + / 可以匹配到这个locationproxy_pass 表示,当匹配到这个的时候,将请求转发到 http://tomcat:8080/index + /
    location = / {       proxy_pass http://tomcat:8080/index    }
2.第二个必选规则-处理静态文件请求(匹配到即返回)
有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
服务发现 + /static/* 可以匹配到这个locationroot表示,匹配到之后在本服务下找 /webroot/res/ + **/static/***中间的/会自动去掉
    location ^~ /static/ {       # 请求/static/a.txt 将被映射到实际目录文件:/webroot/res/static/a.txt       root /webroot/res/;    }
*服务发现 + .(gif|jpg|jpeg|png|css|js|html|ico) 可以匹配到这个locationroot表示,匹配到之后在本服务下找**/webroot/res/ + .(gif|jpg|jpeg|png|css|js|html|ico)* 中间的/会自动去掉
    location ~* \.(gif|jpg|jpeg|png|css|js|html|ico)$ {       root /webroot/res/;    }
3.第三个规则就是通用规则,用来转发动态请求到后端应用服务器(匹配符合要求的最长location)
服务发现 + /* 可以匹配到这个location如果匹配到则会将请求转发到 http://tomcat:8080/ + /*
    location / {       proxy_pass http://tomcat:8080/    }
4.常用规则小结
实际上使用的也就是一个精确匹配一个处理静态文件的匹配,还有一堆通用匹配前两个匹配是正则匹配,只要匹配到了就返回最后的一堆都是通用匹配,匹配到了不一定马上返回,直到匹配到符合要求的最长location才会返回
5.应用实例
1.在linux的tomcat下编写 webapps\product\hi.html
1.这样的资源完整路径就是http://192.168.200.128:8080/product/hi.html
2.进入webapps目录 cd /opt/tomcat/apache-tomcat-8.5.59/webapps
3.创建product文件夹 mkdir product
4.编辑hi.html文件 vim product/hi.html

image-20240406155439901

5.本机测试访问 http://192.168.200.128:8080/product/hi.html

image-20240406155739081

2.在win的tomcat下编写 webapps\member\hello.html
1.查看win的ip为 192.168.200.1

image-20240406160208523

2.这样资源的完整路径就是http://192.168.200.1:8080/member/hello.html
3.webapps下创建member文件夹并编写hello.html文件

image-20240406160353314

4.启动win的tomcat

image-20240406160824240

5.本机测试访问 http://192.168.200.1:8080/member/hello.html

image-20240406160850465

3.保证两端网络连通
1.虚拟机ping 192.168.200.1 不通

image-20240406161018559

2.关闭win防火墙,再次ping,确定网络互通

image-20240406161201804

3.虚拟机直接访问 http://192.168.200.1:8080/member/hello.html

image-20240406161406070

4.配置nginx.conf
1.打开nginx.conf cd /usr/local/nginx/ vim nginx.conf

image-20240406161648403

2.配置nginx.conf

image-20240406162738065

3.重载nginx 使配置文件生效 ./sbin/nginx -s reload
4.查看10000端口是否在监听 netstat -anp | grep 10000

image-20240406162822365

5.开启10000端口的防火墙
firewall-cmd --permanent --add-port=10000/tcpfirewall-cmd --reloadfirewall-cmd --query-port=10000/tcp

image-20240406163805381

6.windows测试访问
http://192.168.200.128:10000/member/hello.html

image-20240406163859095

http://192.168.200.128:10000/product/hi.html

image-20240406163919808

http://tomcat.sunxiansheng.cn:10000/product/hi.html

image-20240406164331366

Nginx(负载均衡)

1.需求分析

image-20240406170924517

2.思路分析

image-20240406171820259

3.负载均衡配置规则

1.轮询(默认)

image-20240406173005071

2.weight

image-20240406173018543

3.ip_hash

image-20240406173042177

4.fair

image-20240406173114532

4.具体实现

1.部署两个服务
1.在linux的tomcat下的webapps中创建/search/look.html

image-20240406191601067

image-20240406191738458

2.在win的tomcat下的webapps中创建/search/look.html

image-20240406192015755

image-20240406192002691

3.重启两个tomcat,并测试nginx服务是否可以访问其他服务
1.重启win的tomcat后测试本机访问 http://192.168.200.1:8080/search/look.html

image-20240406192326709

2.重启linux的tomcat后测试本机访问 http://192.168.200.128:8080/search/look.html

image-20240406192650933

3.测试linux访问 http://192.168.200.1:8080/search/look.html

image-20240406192852751

2.配置域名解析到linux服务器

image-20240406193902156

3.配置nginx.conf
1.在http全局块配置

image-20240406204525909

2.在server块配置

image-20240406204534891

3.重载nginx 使配置文件生效 ./sbin/nginx -s reload
4.开启7777端口的防火墙
firewall-cmd --permanent --add-port=7777/tcp && firewall-cmd --reload && firewall-cmd --query-port=7777/tcp

image-20240406200711812

5.测试
win访问 http://look.sunxiansheng.cn:7777/search/look.html

image-20240406204702712

image-20240406204710258

6.示意图
首先将所有需要Nginx管理的服务都列出来1.Nginx服务发现(server) + 2.匹配服务的资源路径(location)3.服务发现(proxy_pass)发送请求时,Nginx服务发现 + 服务的资源路径

image-20240406212630158

7.注意事项
1.nginx.conf的upstream不能带下划线

image-20240406211545171

2.浏览器不能是无痕的

image-20240406211716679

3.tomcat没有监听端口问题

image-20240406211731412

8.负载均衡配置

https://zhuanlan.zhihu.com/p/409693332

点击全文阅读

郑重声明:

本站所有活动均为互联网所得,如有侵权请联系本站删除处理

我来说两句