Nginx里有一个stream模块,用来实现四层协议的转发、 代理、负载均衡等。stream模块的用法跟http的用法类似,允许我们配 置一组TCP或者UDP等协议的监听,然后通过proxy_pass来转发我们的 请求,通过upstream添加多个后端服务,实现负载均衡。
四层协议负载均衡的实现,一般都会用到LVS、HAProxy、F5等,要么 很贵要么配置很麻烦,而Nginx的配置相对来说更简单,更能快速完成 工作。
./configure --with-stream --without-http_rewrite_module
sudo make
sudo make install
该指令和http的upstream指令是类似的
语法 |
stream { ... } |
默认值 |
— |
位置 |
main |
stream {
upstream redis {
server 127.0.0.1:6379;
server 127.0.0.1:6378;
}
upstream tomcat {
server 127.0.0.1:8080;
}
upstream mysql{
server 127.0.0.1:3306;
}
server {
listen 81;
proxy_pass redis;
}
server {
listen 82;
proxy_pass tomcat;
}
server {
listen 83;
proxy_pass mysql;
}
}
yangyanping@ZBMac-WP2HJYDWY src % ./redis-cli -h 127.0.0.1 -p 81
127.0.0.1:81> set name yyp
OK
127.0.0.1:81> get name
"yyp"
127.0.0.1:81>
访问地址:http://127.0.0.1:82/
yangyanping@ZBMac-WP2HJYDWY bin % ./mysql -uroot -pyangyanping -h127.0.0.1 -p83
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
yangyanping@ZBMac-WP2HJYDWY bin % ./mysql -uroot -pyangyanping -h127.0.0.1 -P83
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.29 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Nginx是基于Proxy Store来实现 的,其原理是把URL及相关组合当做Key,在使用MD5算法对Key进行哈 希,得到硬盘上对应的哈希目录路径,从而将缓存内容保存在该目录 中。它可以支持任意URL连接,同时也支持404/301/302这样的非200状 态码。Nginx即可以支持对指定URL或者状态码设置过期时间,也可以使 用purge命令来手动清除指定URL的缓存。
该指定用于设置缓存文件的存放路径
语法 |
proxy_cache_path path [levels=number] keys_zone=zone_name:zone_size [inactive=time] [max_size=size]; |
默认 值 |
— |
位置 |
http |
path:缓存路径地址,如:
proxy_cache_path /usr/local/proxy_cache levels=2:1 keys_zone=yyp:100m inactive=1d max_size=10g;
该指令用来开启或关闭代理缓存,如果是开启则自定使用哪个缓存区来 进行缓存。
语法 |
proxy_cache zone_name|off; |
默认值 |
proxy_cache off; |
位置 |
http、server、location |
zone_name:指定使用缓存区的名称。配置参考:
proxy_cache yyp;
该指令用来设置web缓存的key值,Nginx会根据key值MD5哈希存缓存。
语法 |
proxy_cache_key key; |
默认值 |
proxy_cache_key $scheme$proxy_host$request_uri; |
位置 |
http、server、location |
该指令用来对不同返回状态码的URL设置不同的缓存时间。
语法 |
proxy_cache_valid [code ...] time; |
默认值 |
— |
位置 |
http、server、location |
配置参考,为200的响应URL设置5天缓存
proxy_cache_valid 200 5d;
该指令用来设置资源被访问多少次后被缓存
语法 |
proxy_cache_min_uses number; |
默认值 |
proxy_cache_min_uses 1; |
位置 |
http、server、location |
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
proxy_cache_path /usr/local/proxy_cache levels=2:1 keys_zone=yyp:100m inactive=1d max_size=10g;
upstream backend {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name localhost;
location /tomcat {
proxy_cache yyp;
proxy_cache_key yangyanping;
proxy_cache_min_uses 5;
proxy_cache_valid 200 5d;
add_header nginx-cache "$upstream_cache_status";
proxy_pass http://backend/;
}
}
在浏览器中访问地址:http://127.0.0.1/tomcat/examples/index.html
使用命令查看缓存内容 sudo cat proxy_cache/88/9/424fcd3a9c622afbc0aecb18b9eec988
yangyanping@ZBMac-WP2HJYDWY local % sudo cat proxy_cache/88/9/424fcd3a9c622afbc0aecb18b9eec988
Password:
SΒb?&?^?6?b?#?b
Apache Tomcat Examples
Apache Tomcat Examples
yangyanping@ZBMac-WP2HJYDWY local %
rm -rf /usr/local/proxy_cache