参考 :
Nginx12 openresty使用lua-resty-http模块 - 金天黑日 - 博客园 (cnblogs.com)
windows openresty 死磕:安装和启动脚本 - 疯狂创客圈 - 博客园 (cnblogs.com)
https://blog.csdn.net/wwppp987/article/details/122803662
1.下载软件包
1.1.openresty
Windows
All the releases are signed by the public PGP key A0E98066
of Yichun Zhang.
- For 32-bit Windows: openresty-1.25.3.2-win32.zip 16MB PGP - 19 Jul 2024
- For 64-bit Windows: openresty-1.25.3.2-win64.zip 16MB PGP - 19 Jul 2024
根据计算机位数下载相应的即可, 然后放到D盘解压, 文件列表如下:
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2024/8/22 13:56 conf
d----- 2024/8/22 13:56 html
d----- 2024/8/22 13:56 include
d----- 2024/7/18 23:25 logs
d----- 2024/8/22 13:56 lua
d----- 2024/8/22 13:56 lualib
d----- 2024/8/22 13:57 pod
-a---- 2024/8/22 13:56 22924 COPYRIGHT
-a---- 2024/8/22 13:56 4271109 lua51.dll
-a---- 2024/8/22 13:56 389951 luajit.exe
-a---- 2024/8/22 13:56 38689676 nginx.exe
-a---- 2024/8/22 13:56 8974 README.txt
-a---- 2024/8/22 13:56 38253 resty
-a---- 2024/8/22 13:56 38671 resty.bat
-a---- 2024/8/22 13:56 14957 restydoc
-a---- 2024/8/22 13:56 8873 restydoc-index
-a---- 2024/8/22 13:56 9669 restydoc-index.bat
-a---- 2024/8/22 13:56 16003 restydoc.bat
1.2.lua-resty-http
GitHub - ledgetech/lua-resty-http: Lua HTTP client cosocket driver for OpenResty / ngx_lua.
下载解压, 文件列表如下:
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2024/8/22 15:26 .github
d----- 2024/8/22 15:26 lib
d----- 2024/8/22 15:26 t
------ 2024/2/29 11:43 26 .gitattributes
------ 2024/2/29 11:43 101 .gitignore
------ 2024/2/29 11:43 34 .luacheckrc
------ 2024/2/29 11:43 157 .luacov
------ 2024/2/29 11:43 240 dist.ini
------ 2024/2/29 11:43 1293 LICENSE
------ 2024/2/29 11:43 639 lua-resty-http-0.17.2-0.rockspec
------ 2024/2/29 11:43 757 Makefile
------ 2024/2/29 11:43 20903 README.md
将"lua-resty-http-master\lua-resty-http-master\lib\resty"里面的三个文件放到"D:\openresty-1.25.3.2-win64\lualib\resty"文件夹下面, 三个文件信息如下:
Mode LastWriteTime Length Name
---- ------------- ------ ----
------ 2024/2/29 11:43 33331 http.lua
------ 2024/2/29 11:43 12496 http_connect.lua
------ 2024/2/29 11:43 1152 http_headers.lua
2.修改"D:\openresty-1.25.3.2-win64\conf\nginx.conf"文件, 替换为如下内容
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
lua_package_path "D:\openresty-1.25.3.2-win64\lualib\resty\?.lua;;"
include mime.types;
default_type application/octet-stream;
#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;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /my_test {
set $proxy_address '';
access_by_lua_block {
local http = require "resty.http"
local httpc = http.new()
local res, err = httpc:request_uri("http://127.0.0.1:8000/get_proxy_address")
if not res then
ngx.log(ngx.ERR, "failed to request: ", err)
return ngx.exit(500)
end
ngx.var.proxy_address = res.body
}
proxy_pass http://proxy_address;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
}
}
需要修改的值:
- lua_package_path "D:\openresty-1.25.3.2-win64\lualib\resty\?.lua;;"
- location /my_test {
- http://127.0.0.1:8000/get_proxy_address
1=>改成自己电脑的路径
2=>改成代理的URI路径
3=>改成获取代理地址http接口的地址
3.创建获取代理地址的http服务
http接口返回字符串即可
本次案例采用python实现, 代码如下
# -*- coding: utf-8 -*-
# ====> nginx负载均衡算法服务 <==== #
import time
from flask import Flask
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
app_name = 'nginx负载均衡算法服务'
def check_port(l_port):
result = subprocess.run(('netstat', '-ano'), capture_output=True, text=True,
creationflags=subprocess.CREATE_NO_WINDOW)
output = result.stdout
lines = output.split('\n')
for line in lines:
if str(l_port) in line and 'LISTENING' in line: # 注意,这里要确保状态是 LISTENING 才表示端口被占用
return True
return False
# 获取最佳的ws服务端地址
@app.route('/get_proxy_address', methods=['GET'])
def get_best_ws():
print('请求进入')
# 定制负载均衡算法...
return '127.0.0.1:8001'
if __name__ == '__main__':
f_port = 8000
f_debug = False
print(f'开始启动{app_name},端口为:{f_port}, 是否开启debug:{f_debug}')
if check_port(f_port):
lg.warn(f'端口{f_port}已被占用, 退出运行 !')
time.sleep(2)
else:
# 启动web服务
app.run(host='0.0.0.0', port=f_port , debug=f_debug)
4.启动nginx
powerShell进入"D:\openresty-1.25.3.2-win64"目录,执行一下命令
cd D:\openresty-1.25.3.2-win64
start nginx.exe
其他命令:
# 查看nginx进程
tasklist.exe /fi "imagename eq nginx.exe"
# 停止nginx
.\nginx.exe -s quit
# 重新加载nginx.conf配置
nginx.exe -s reload
5.说明
nginx启动的端口是80, 获取代理地址的http服务的端口是8000, 代理的目标端口是8001
访问"127.0.0.1/my_test"时会从8000端口的http服务获取代理目标地址为8001, 就会访问8001地址
自定义的负载均衡算法在获取代理地址的http服务里面实现即可