根据客户端的IP地址确定请求的服务器,保证同一个IP总是访问同一个服务器,解决了动态网页存在的session共享问题
vim /usr/local/nginx/conf/nginx.conf
upstream tomcat {
ip_hash;
server 192.168.45.11:8080 weight=1 max_fails=1 fail_timeout=10s;
server 192.168.45.12:8080 weight=2 max_fails=1 fail_timeout=10s;
}
nginx_upstream_hash
软件包最短时间(仅NGINX Plus):请求发送给响应时间最短且活跃连接数最少的服务器。
主机 | IP | 服务 | 版本 |
---|---|---|---|
YY | 192.168.45.10 | nginx | 1.22.0 |
YY1 | 192.168.45.11 | tomcat | 8.5.82 |
YY2 | 192.168.45.12 | tomcat | 8.5.82 |
完成服务器服务搭建,这里不做讲述,如有需要后续进行超链接,单独出一篇部署的详细说明。
vim /usr/local/nginx/conf/nginx.conf
Down
:表示当前的 server 暂时不参与负载;Weight
:默认为 1,weight 越大,负载的权重就越大;Max_fails
:允许请求失败的次数默认为 1,当超过最大次数 proxy_next_upstream
模块定义的错误;Fail_timeout:max_fails
:请求失败后,暂停的时间;Backup
:其它所有的非 backup
机器 down
或者忙的时候,才会请求 backup
机器,所以这台机器压力会最轻。192.168.45.10:9090
和 192.168.45.13:7070
示范使用 upstream tomcat {
server 192.168.45.10:9090 down;
server 192.168.45.11:8080 weight=1 max_fails=1 fail_timeout=10s;
server 192.168.45.12:8080 weight=1 max_fails=1 fail_timeout=10s;
server 192.168.45.13:7070 backup;
}
proxy_pass http://tomcat;
tomcat
为之前定义的集群名称 location / {
# root html;
# index index.html index.htm;
proxy_pass http://tomcat;
}
nginx -s reload
curl 192.168.45.10
cat /usr/local/tomcat/logs/localhost_access_log.2023-03-04.txt
192.168.45.10
vim /usr/local/nginx/conf/nginx.conf
proxy_set_header X-Forwarded-For $remote_addr;
location / {
# root html;
# index index.html index.htm;
proxy_pass http://tomcat;
proxy_set_header X-Forwarded-For $remote_addr;
}
nginx -s reload
vim /usr/local/tomcat/conf/server.xml
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%{X-FORWARDED-FOR}i %a %l %u %t %r %s %b %D %q %{User-Agent}i" resolveHosts="false" />
pattern="%{X-FORWARDED-FOR}i %a %l %u %t %r %s %b %D %q %{User-Agent}i" resolveHosts="false" />
pattern
参数说明nginx.conf
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 logs/access.log main;
'"$http_user_agent" "$http_x_forwarded_for"';
http.conf
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b" common
systemctl restart httpd
ngx_cache_purge
模块在缓存过期时间未到前,手动清理缓存。proxy_pass
和 proxy_cache
。Proxy_cache
指令负责反向代理缓存后端服务器的静态内容。Fastcgi_cache
主要用来处理 FastCGI 动态进程缓存。ngx_cache_purge
部署链接在2023年3月3日 08:41:17 可用。
wget https://github.com/FRiCKLE/ngx_cache_purge/archive/refs/tags/2.3.tar.gz -O ngx_cache_purge-2.3.tar.gz
tar zxvf ngx_cache_purge-2.3.tar.gz -C /usr/local/src/
cd /usr/local/src/nginx-1.22.0/
./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre --with-http_ssl_module --with-http_gzip_static_module --add-module=/usr/local/ngx_cache_purge-2.3/
make -j 4
ngninx -s stop
nginx
命令cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
ngx_cache_purge
模块的 nginx
cd /usr/local/src/nginx-1.22.0/
cp objs/nginx /usr/local/nginx/sbin/nginx
nginx.conf
配置文件vim /usr/local/nginx/conf/nginx.conf
# 缓存模块
proxy_connect_timeout 75;
proxy_send_timeout 75;
proxy_read_timeout 75;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_buffering on;
proxy_temp_path /usr/local/nginx/proxy_temp;
proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=my-cache:100m max_size=1000m inactive=600m max_size=2g;
#ngx_cache_purge实现缓存清除
location ~ /purge(/.*) {
allow 127.0.0.1;
allow 192.168.45.0/24;
deny all;
proxy_cache_purge my-cache $host$1$is_args$args; #开启缓存清理及清>理权限
}
location ~ .*\.(gif|jpg|png|html|htm|css|js|ico|swf|pdf)(.*) {
proxy_pass http://tomcat; #使用负载均衡
proxy_redirect off; #在location里定义带参数
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_ignore_headers Set-Cookie;
proxy_hide_header Set-Cookie;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_cache my-cache; #在location里是用my-cache缓存
add_header Nginx-Cache $upstream_cache_status;
proxy_cache_valid 200 304 301 302 8h;
proxy_cache_valid 404 1m;
proxy_cache_valid any 1d;
proxy_cache_key $host$uri$is_args$args;
expires 30d;
}
nginx -s reload
Proxy_buffering on;
Proxy_temp_path;
temp
和 cache
两个目录在不同文件系统上(分区)。
Proxy_cache_path;
cache_key
的 MD5值。levels=1:2 keys_zone=my-cache:100m
my-cache
,内存缓存空间大小为 100 MB。/usr/local/nginx/proxy_cache/c/29/b7f54b2df7773722d382f4809d65029c
。Inactive=600 max_size=2g
Set-Cookie
的对象。
proxy_ignore_headers
设置忽略它们,设置方法如下:
Proxy_ignore_headers Set-Cookie;
Proxy_hide_header Set-Cookie;
Proxy_cache
Proxy_cache_key
Proxy_cache_valid
Add_header
response header
add_header name value;
$upstream_cache_status
^3fce02
$upstream_cache_status
包含以下几种状态:
expires
Cache-Control: max-age
,返回给客户端的浏览器缓存失效时间。location
块的内容是一个变量,没有办法进行缓存。F12
开发者调整模式Nginx Cache
内容,即 [[8-Nginx+Tomcat 负载均衡#^3fce02|缓存状态]]。 nginx.conf
sever
块内
nginx.conf
# 定义nginx静态服务器集群
upstream nginx {
server 192.168.45.10:808 weight=1 max_fails=2 fail_timeout=10s;
}
# 定义tomcat动态服务器集群
upstream tomcat {
server 192.168.45.11:8080 weight=1 max_fails=1 fail_timeout=10s;
server 192.168.45.12:8080 weight=2 max_fails=1 fail_timeout=10s;
}
# 808端口服务器
server{
listen 808;
server_name www.808.com;
location / {
root html;
index index.html index.htm;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|rar|zip|txt|flv|mid|doc|ppt|xls|mp3|wma|html|htm|css|js|ico|swf|pdf)$ {
# 读取静态资源的存放位置
root html ;
expires 30d;
# 资源是否进行缓存及缓存时间
}
}
# 80端口服务器
server {
listen 80;
server_name www.test.com;
# 动态内容交由tomcat处理
location / {
# root html;
# index index.html index.htm;
proxy_pass http://tomcat;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_ignore_headers Set-Cookie;
proxy_hide_header Set-Cookie;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
}
# 静态内容转发至nginx集群处理
location ~ .*\.(gif|jpg|jpeg|png|bmp|rar|zip|txt|flv|mid|doc|ppt|xls|mp3|wma|html|htm|css|js|ico|swf|pdf)$ {
proxy_pass http://nginx;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_cache my-cache;
add_header Nginx-Cache $upstream_cache_status;
proxy_cache_valid 200 304 301 302 8h;
proxy_cache_valid 404 1m;
proxy_cache_valid any 1d;
proxy_cache_key $host$uri$is_args$args;
expires 30d;
}
# 报错指向网页
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
index.jsp
index.html