客户端永远不会直接访问服务器。 客户端、中间件、服务器都能做缓存。 我们一般用中间件做缓存。
加了代理之后最大的问题是什么? 会丢失客户端的ip地址,做控制的时候不能丢ip 客户端访问会有日志,
![[Pasted image 20261027092559.png]] Host:如果没有了,会报404,因为这条传的是域名, X-Real-IP:记录真实的客户IP X-Forwarded-For"记录的是完整的代理链ip,走了多少层都记录下来
Nginx 负载均衡状态配置
![[Pasted image 20261027095648.png]]
upstream lb {
server 172.16.1.2:8001 down;
server 172.16.1.2:8003 fail_timeout=5s max_conns=3;
server 172.16.1.2:8002 backup;
}
server {
listen 80;
server_name www.shida-lab.com;
location / {
proxy_pass http://lb;
include proxy_params;
}
}
Nginx 负载均衡调度策略
![[Pasted image 20261027101342.png]]
用hash的话一定要加一个一致性,不加会导致很多访问不了,加了也有可能一部分访问不了
![[e7944fd7cbccd3b02330b0645a6afa47.png]]![[e903243b05be9aa30643ee5190a1f58a.png]]
root@localhost:/soft/code2# cp ../code1/url?* .
讲四层的负载
![[Pasted image 20261027110224.png]] 这段被称为main,是对于全局生效的,对于网站和端口转发没有意义。 ![[Pasted image 20261027110348.png]] 管理链接的 ![[Pasted image 20261027110421.png]] 管理七层负载均衡和网页的业务 ![[Pasted image 20261027110610.png|327]] ![[525f794f540597dbc6dec6a4a9cd2671.png]] stream是管理四层的,不能写在conf.d目录里。有redis服务 做四层转发的时候不要把proxy-params中的Host和X-Real-IP写进去
实验:
![[Pasted image 20261027112253.png]] ![[Pasted image 20261027112406.png]]
解决方法:
root@www:/etc/nginx# dnf install -y nginx-all-modules
root@www:/etc/nginx# dnf install -y nginx-mod-stream
root@www:/etc/nginx# nginx -V #查看编译安装的参数
![[Pasted image 20261027113046.png]] 这里写的是动态就是动态的加载。
vim /etc/nginx/nginx.conf
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
include /usr/lib64/nginx/modules/*.conf; #加入这条
root@www:~# nginx -T | grep -E ’load_module|configuration file’ ssh就可 ![[Pasted image 20261027114051.png]]
如果在浏览器上输入https://172.24.126.140:9090用root进不去的话
去下面的命令去找。
root@localhost:~# vim /etc/cockpit/disallowed-users
root@localhost:~# systemctl restart cockpit
动态资源
不能直接生成的,我给后台的程序,帮我计算,结果给我。 nginx为什么自己处理不了动态资源? 本身没有代码解析的能力
server{
listen 80;
root /soft/code;
location ~ .*\.(jpg|jpeg|gif|png)$ {
root /soft/code/images;
gzip on;
}
}
https://dlcdn.apache.org/ 找到tomcat ![[6103b17bf82f8e7685bc12c08cef9bc1.png]] 然后下载到机器里
root@localhost:/soft/app# wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.122/bin/apache-tomcat-9.0.122.tar.gz
root@localhost:/soft/app# tar xzvf apache-tomcat-9.0.122.tar.gz
root@localhost:/soft/app# vim /soft/app/apache-tomcat-9.0.122/webapps/ROOT/java_test.jsp
--------------------------------------------------------------
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
<HEAD>
<TITLE>JSP Test Page</TITLE>
</HEAD>
<BODY>
<%
Random rand = new Random();
out.println("<h1>Random number:</h1>");
out.println(rand.nextInt(99)+100);
%>
</BODY>
</HTML>
--------------------------------------------------------------
root@localhost:/soft/app# dnf install java -y
#脚本的位置
root@localhost:/soft/app/apache-tomcat-9.0.122/bin# sh startup.sh
http://172.24.126.143:8080/java_test.jsp
动静分离
upstream static {
server 172.24.126.143:80;
}
upstream java {
server 172.24.126.143:8080;
}
server {
listen 80;
server_name www.shida-lab.com;
location / {
root /soft/code;
index index.html;
}
location ~ .*\.(jpg|jpeg|gif|png)$ { #静态
proxy_pass http://static;
include proxy_params;
}
location ~ .*\.jsp$ { #动态
proxy_pass http://java;
include proxy_params;
}
}
http://www.shida-lab.com/image-test.jpg 如果能访问,是代理帮助访问的,代理到了静态资源。 http://www.shida-lab.com/java_test.jsp 如果能访问,代理帮助访问的,代理到了动态资源 注意:动态代理的时候,在浏览器中,没有输入端口号8080 为什么:因为文件内写的,代理到8080
mkdir /soft/{android,iphone,firefox,sougou,default}
vim /soft/android/index.html
<h1>android</h1>
其他四个
root@localhost:~# vim /etc/nginx/conf.d/node.conf
--------------------------------------------------------------
server {
listen 8081;
root /soft/android;
index index.html;
}
server {
listen 8082;
root /soft/iphone;
index index.html;
}
server {
listen 8083;
root /soft/firefox;
index index.html;
}
server {
listen 8084;
root /soft/sougou;
index index.html;
}
server {
listen 8085;
root /soft/default;
index index.html;
}
root@www:/etc/nginx/conf.d# vim test.conf
upstream android {
server 172.24.126.143:8081;
}
upstream iphone {
server 172.24.126.143:8082;
}
upstream firefox {
server 172.24.126.143:8083;
}
upstream sougou {
server 172.24.126.143:8084;
}
upstream default {
server 172.24.126.143:8085;
}
server {
listen 80;
server_name www.shida-lab.com;
location / {
if ($http_user_agent ~* "android"){
proxy_pass http://android;
}
if ($http_user_agent ~* "iphone"){
proxy_pass http://iphone;
}
if ($http_user_agent ~* "Firefox"){
proxy_pass http://firefox;
}
if ($http_user_agent ~* "safari"){
proxy_pass http://sougou;
}
proxy_pass http://default;
include proxy_params;
}
}
nginx 缓存服务
客户端,中间件,服务端都可以做缓存。
![[f7131941d0168d1e87e0e01319f8107d.png]] ![[6a0e94bb1f511ae580d05276e89a354f.png]]
proxy_cache_path /soft/cache levels=1:2 keys_zone=code_cache:10m inactive=60m max_size=10g use_temp_path=off;
upstream cache {
server 172.24.126.143:8081;
server 172.24.126.143:8082;
server 172.24.126.143:8083;
}
server {
listen 80;
server_name www.shida-lab.com;
location / {
proxy_pass http://cache;
proxy_cache code_cache;
proxy_cache_valid 200 304 12h;
add_header Nginx-Cache "$upstream_cache_status";
proxy_next_upstream error timeout invalid_header http_500 http_502;
include proxy_params;
}
}
命令行检查:
root@www:/soft/cache# curl -s -I http://172.24.126.140/url2.html | grep "Nginx-Cache"
Nginx-Cache: MISS #这里第一次要看到是MISS
root@www:/soft/cache# curl -s -I http://172.24.126.140/url2.html | grep "Nginx-Cache"
Nginx-Cache: HIT
如果第一次看到不是MISS可以去看 root@www:/soft/cache# cd /soft/cache/b 的缓存,删了之后再检查 第二次检查会看到HIT命中。
![[1310c152a72af9d89a5a4b67a5515c8a.png]] 这个模块是开启缓存就能用。 ![[Pasted image 20261027163048.png]] 这个模块里的key和上一个模块的key的值必须一致,不一致永远出不了现象。
日志的统计
![[90386719c18f932d8dc4d8d1609dfc1b.png]] vim /etc/nginx/conf.d/test.conf ![[Pasted image 20261027164234.png]]
系统运维工程师、Linux运维工程师、技术支持、实施工程师、网络工程师
DevOps工程师、云原生运维工程师、SRE可靠性站点工程师、云计算工程师
MLOps、AI运维工程师、AI应用