OpenResty编译安装
一、环境介绍
操作系统:龙蜥 os 8.9
软件:1.27.1.2
https://openresty.org/en/download.html
二、配置环境
dnf install epel-release -y
dnf install perl-Digest-MD5 -y
dnf install git pcre-devel openssl-devel gcc curl zlib-devel make GeoIP-devel
三、编译安装
./configure -j2 --with-threads --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_sub_module --with-stream --with-stream=dynamic --with-stream_ssl_module --with-stream_realip_module --with-stream_geoip_module --with-stream_geoip_module=dynamic --with-stream_ssl_preread_module --with-stream_realip_module
make -j2
make install
四、启动
/usr/local/openresty/nginx/sbin/nginx
五、配置使用基于cookie会话保持
1、nginx 主配置文件
# 加载模块写在最顶端
# 普通用户启动 (useradd nginx -s /sbin/nologin -M)
user nginx;
# 配置nginx worker进程个数
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
# 配置日志存放路径
#access_log logs/accesslog.log warn;
error_log logs/error.log error;
pid logs/nginx.pid;
# nginx事件处理模型优化
events {
worker_connections 51200; # 当个进程允许的客户端最大连接数
use epoll;
}
# 配置nginx worker进程最大打开文件数
worker_rlimit_nofile 65535;
http {
# 隐藏版本号
server_tokens off;
# 设置日志格式
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/accesslog.log main;
# 开启高效文件传输模式
include mime.types; # 媒体类型
default_type application/octet-stream; # 默认媒体类型
charset utf-8; # 默认字符集
sendfile on;
tcp_nopush on; # 只有在sendfile开启模式下有效
# 设置连接超时时间
keepalive_timeout 65; # 设置客户端连接保持会话的超时时间,超过则服务器会关闭该连接
tcp_nodelay on; # 打开tcp_nodelay,在包含了keepalive参数才有效果
client_header_timeout 15; # 设置客户端请求有超时时间,该时间内客户端未发送数据,nginx将返回‘Request time out(408)’错误
client_body_timeout 15; # 设置客户端请求体超时时间,同上
send_timeout 15; # 设置相应客户端的超时时间,超时nginx将会关闭连接
# 上传文件大小设置(动态引用)
client_max_body_size 10m;
# 数据包头部缓存大小
client_header_buffer_size 1k; #默认请求包头信息的缓存
large_client_header_buffers 4 4k; #大请求包头部信息的缓存个数与容量
# 压缩处理
gzip on; #开启压缩
gzip_min_length 1k; #小文件不压缩
gzip_comp_level 4; #压缩比率
gzip_buffers 4 16k; #压缩缓冲区大小,申请4个单位为16K的内存作为亚索结果流缓存
gzip_http_version 1.1; # 默认压缩版本
#对特定文件压缩,类型参考mime.types
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_vary on;
gzip_disable "MSIE[1-6]\.";
# 设置fastcgi
fastcgi_cache_path /usr/local/openresty/nginx/fastcgi_cache levels=1:2
keys_zone=TEST:10m
inactive=5m; # 为FastCGI缓存指定一个路径,目录结构等级,关键字区域存储时间和非活动删除时间
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_connect_timeout 300; # 指定连接到后端FastCGI的超时时间
fastcgi_send_timeout 300; # 向FastCGI传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI传送请求的超时时间
fastcgi_read_timeout 300; # 接收FastCGI应答的超时时间,这个值是指已经完成两次握手后接收FastCGI应答的超时时间
fastcgi_buffer_size 16k; # 缓冲区大小
fastcgi_buffers 16 16k;
fastcgi_busy_buffers_size 16k;
fastcgi_temp_file_write_size 16k; # 在写入fastcgi_temp_path时将用多大的数据块,默认值是fastcgi_buffers的两倍
fastcgi_cache TEST; # 开启FastCGI缓存并且为其制定一个名称
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m; # 为指定的应答代码指定缓存时间,上例中将200,302应答缓存一小时,301应答缓存1天,其他为1分钟
fastcgi_cache_min_uses 1; # 5分钟内某文件1次也没有被使用,那么这个文件将被移除
fastcgi_cache_use_stale error timeout invalid_header http_500;
# 内存缓存
open_file_cache max=2000 inactive=20s; #设置服务器最大缓存文件数量,关闭20秒内无请求的文件
open_file_cache_valid 60s; #文件句柄的有效时间是60秒,60秒后过期
open_file_cache_min_uses 5; #只有访问次数超过5次会被缓存
open_file_cache_errors off;
lua_package_path "/usr/local/openresty/lualib/resty/?.lua;;"; # 关键配置
# 引入子配置文件
include vhost/*.conf;
}
#配置tcp代理,需要额外加载stream相关模块
stream {
#
# upstream cloudsocket {
# hash $remote_addr consistent;
# # $binary_remote_addr;
# server 192.168.182.155:3306 weight=5 max_fails=3 fail_timeout=30s;
# }
# server {
# listen 3306;#数据库服务器监听端口
# proxy_connect_timeout 10s;
# proxy_timeout 300s;#设置客户端和代理服务之间的超时时间,如果5分钟内没操作将自动断开。
# proxy_pass cloudsocket;
# }
# 引入子配置文件
include stream/*.conf;
}
2、添加default.conf 默认页面
server {
listen 80 default_server;
server_name _; # 匹配所有域名
location / {
# 返回 403 错误,禁止 IP 地址访问
return 403;
}
}
server {
listen 443 ssl default_server;
server_name _;
ssl_certificate /usr/local/openresty/nginx/cert/xxx.pem;
ssl_certificate_key /usr/local/openresty/nginx/cert/xxx.key;
# 其他 SSL 配置
location / {
return 403;
}
}
3、lua脚本
local ck = require "resty.cookie"
local cookie, err = ck:new()
if not cookie then
ngx.log(ngx.ERR, "failed to instantiate cookie: ", err)
return ngx.exit(500)
end
-- 尝试获取名为 route_key 的 cookie
local route_key, err = cookie:get("route_key")
if not route_key then
-- 生成唯一值(优先用 ngx.var.request_id,否则用更安全的随机数)
local new_route
if ngx.var.request_id then
new_route = ngx.var.request_id
else
-- 更安全的随机数(非加密安全,但比 math.random 好)
new_route = ngx.time() .. ngx.md5(ngx.var.connection .. math.random())
end
-- 设置 cookie,客户端会收到这个 cookie
local ok, err = cookie:set({
key = "route_key",
value = new_route,
path = "/",
httponly = true,
max_age = 3600, -- 1小时(单位:秒)
})
if not ok then
ngx.log(ngx.ERR, "failed to set cookie 'route_key': ", err)
-- 可以选择继续执行(即使 Cookie 设置失败),或返回错误
-- return ngx.exit(500)
end
-- 把生成的新值赋给 nginx 变量供 hash 使用
ngx.var.route_key = new_route
else
-- 已有 cookie,赋值给 nginx 变量
ngx.var.route_key = route_key
end
4、安装cookie.lua
cd /usr/local/openresty/lualib/resty
mkdir -p /usr/local/openresty/lualib/resty
curl -o cookie.lua https://raw.githubusercontent.com/cloudflare/lua-resty-cookie/master/lib/resty/cookie.lua
5、样例配置
upstream first_backend {
hash $route_key consistent;
server 10.99.12.45:8999 weight=1 max_fails=2 fail_timeout=10;
}
server {
listen 80;
server_name www.gbsz.com;
location / {
set $route_key "";
# 执行 Lua 脚本,读取或设置 cookie,并赋值给 $route_key
access_by_lua_file /usr/local/openresty/nginx/conf/lua/sticky_cookie.lua;
proxy_pass http://first_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}