Openresty 配置 / 分布式部署
域名配置,权重比较
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
#默认是str类型.会已下载方式访问
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
#默认80端口, 域名 localhost
listen 80;
server_name localhost;
#以html类型访问.
default_type 'text/html;charset=utf-8';
# 域名匹配 (4种)
location / {
echo '不做匹配.通配域名, 权重最低';
}
location = /a {
echo '匹配等于 /a 的域名, 权重最高';
}
location ^~ /a {
echo '匹配 /a 开头的域名, 权重次之 ';
}
location ^~ /a/b {
echo '匹配 /a/b 开头的,相同权重.根据匹配度程度来匹配';
}
# ~ 用于匹配正则 \w 表示正则表达式
location ~ /\w {
echo '匹配正则表达式的域名 \w 表示匹配 字母, 数字, 符号. 优先级 次次之';
}
location ~ /[a-z] {
echo '匹配程度一致.以前面的优先.';
}
}
}
url跳转写法
关键字: proxy_pass
注意: url必须为完整格式.
server {
#默认80端口, 域名 localhost
listen 80;
server_name localhost;
#以html类型访问.
default_type 'text/html;charset=utf-8';
# 跳转到网易
location = / {
proxy_pass https://www.163.com;
}
# 跳转到百度 访问: localhost/baidu/
# 注意: 此写法后面需要跟上 / ; 否则会跳转 https://www.baidu.com/baidu 导致错误
location ^~ /baidu/ {
proxy_pass https://www.baidu.com/;
}
# 跳转到cls520.cn 这个网页
location ^~ /private/ {
proxy_pass http://www.cls520.cn/;
}
}
分布式部署
关键字: upstream / server (名字不可有下划线)
轮询
# 分布式部署
upstream my-group {
server 192.168.43.212:7010;
server 192.168.43.212:7020;
}
server {
#默认80端口, 域名 localhost
listen 80;
server_name localhost;
#以html类型访问.
default_type 'text/html;charset=utf-8';
location /a/ {
proxy_pass http://my-group/;
}
}
权重: weight
# 分布式部署
upstream my-group {
server 192.168.43.212:7010 weight=10; #按照weight比例分配
server 192.168.43.212:7020 weight=2;
}
server {
#默认80端口, 域名 localhost
listen 80;
server_name localhost;
#以html类型访问.
default_type 'text/html;charset=utf-8';
location /a/ {
proxy_pass http://my-group/;
}
}
ip地址: ip_hash
# 分布式部署
upstream my-group {
ip_hash; # 根据访问者ip进行分配
server 192.168.43.212:7010 weight=10; #按照weight比例分配
server 192.168.43.212:7020 weight=2;
}
server {
#默认80端口, 域名 localhost
listen 80;
server_name localhost;
#以html类型访问.
default_type 'text/html;charset=utf-8';
location /a/ {
proxy_pass http://my-group/;
}
}
每次修改完后.需要重启nginx才能生效.