Nginx直接访问Redis方案
- HttpRedis:提供指令少,功能单一。
- HttpRedis2Module:比HttpRedis操作更灵活,功能更强大。
- Lua-resty-redis库:Openresty提供的操作Redis的接口库,需要通过–with-luajit编译安装。
redis2-nginx-module
redis2-nginx-module 是一个支持 Redis 2.0 协议的 Nginx upstream 模块,它可以让 Nginx 以非阻塞方式直接防问远方的 Redis 服务,同时支持 TCP 协议和 Unix Domain Socket 模式,并且可以启用强大的 Redis 连接池功能。详情见github redis2-nginx-module
安装nginx +redis2-nginx-module
1
2
|
yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install nginx-module-redis2
|
修改配置文件/etc/nginx/nginx.conf,启用nginx-module-redis2模块
1
|
load_module modules/ngx_http_redis2_module.so;
|
配置示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
location = /foo {
set $value 'first';
redis2_query set one $value;
redis2_pass 127.0.0.1:6379;
}
# GET /get?key=some_key
location = /get {
set_unescape_uri $key $arg_key; # this requires ngx_set_misc
redis2_query get $key;
redis2_pass foo.com:6379;
}
# GET /set?key=one&val=first%20value
location = /set {
set_unescape_uri $key $arg_key; # this requires ngx_set_misc
set_unescape_uri $val $arg_val; # this requires ngx_set_misc
redis2_query set $key $val;
redis2_pass foo.com:6379;
}
# multiple pipelined queries
location = /foo {
set $value 'first';
redis2_query set one $value;
redis2_query get one;
redis2_query set one two;
redis2_query get one;
redis2_pass 127.0.0.1:6379;
}
location = /bar {
# $ is not special here...
redis2_literal_raw_query '*1\r\n$4\r\nping\r\n';
redis2_pass 127.0.0.1:6379;
}
location = /bar {
# variables can be used below and $ is special
redis2_raw_query 'get one\r\n';
redis2_pass 127.0.0.1:6379;
}
# GET /baz?get%20foo%0d%0a
location = /baz {
set_unescape_uri $query $query_string; # this requires the ngx_set_misc module
redis2_raw_query $query;
redis2_pass 127.0.0.1:6379;
}
location = /init {
redis2_query del key1;
redis2_query lpush key1 C;
redis2_query lpush key1 B;
redis2_query lpush key1 A;
redis2_pass 127.0.0.1:6379;
}
location = /get {
redis2_query lrange key1 0 -1;
redis2_pass 127.0.0.1:6379;
}
|
连接池配置
1
2
3
4
5
6
7
8
9
10
11
12
13
|
upstream redisPool{
server ${KVSTORE_HOST}:${KVSTORE_PORT};
keepalive 1024;
}
server {
location = /v1/qrcode/loginInfo {
default_type application/json;
redis2_query auth ${KVSTORE_AUTH};
redis2_query select ${KVSTORE_DB};
redis2_query get $arg_uuid;
redis2_pass redisPool;
}
}
|
docker中运行
1
|
docker run -d -v ./nginx.conf:/etc/nginx/conf.d/default.conf:ro stepankuzmin/nginx-with-redis
|
nginx.conf配置
1
2
3
4
5
6
7
8
9
10
11
12
13
|
server {
location / {
set $redis_key $uri;
redis_pass name:6379;
default_type text/html;
error_page 404 = /fallback;
}
location = /fallback {
proxy_pass backend;
}
}
|
在redis设置键值,即可通过http访问redis中内容。
Dockerfile 运行Nginx+Redis模块
To create a Dockerfile for Nginx with the Redis module on Alpine Linux, you can follow these steps. Make sure you have Docker installed on your system.
- Create a new directory for your Docker build context, and navigate into it:
1
2
|
mkdir nginx-redis
cd nginx-redis
|
- Create a file named
Dockerfile
within this directory and open it in a text editor:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# Use the official Alpine Linux as the base image
FROM nginx:alpine
# Install build dependencies
RUN apk add --no-cache \
git \
build-base \
openssl-dev \
pcre-dev \
zlib-dev
# Clone the Nginx source code and the ngx_http_redis module
RUN git clone https://github.com/openresty/redis2-nginx-module.git /tmp/redis2-nginx-module
# Download and extract the Nginx source code
RUN wget http://nginx.org/download/nginx-1.21.3.tar.gz -O /tmp/nginx.tar.gz && \
tar -zxC /tmp -f /tmp/nginx.tar.gz
# Build Nginx with the Redis module
RUN cd /tmp/nginx-* && \
./configure --with-compat --add-dynamic-module=/tmp/redis2-nginx-module && \
make modules
# Move the compiled module to the appropriate location
RUN cp /tmp/nginx-*/objs/ngx_http_redis_module.so /etc/nginx/modules/
# Remove build dependencies and temporary files
RUN apk del build-base git openssl-dev pcre-dev zlib-dev && \
rm -rf /tmp/*
# Copy the default Nginx configuration
COPY nginx.conf /etc/nginx/nginx.conf
# Expose port 80
EXPOSE 80
# Run Nginx
CMD ["nginx", "-g", "daemon off;"]
|
- Create a file named
nginx.conf
within the same directory and add your Nginx configuration. Here is a simple example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
worker_processes 1;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/modules/*.conf;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
|
This example uses the default Nginx configuration and listens on port 80.
- Build the Docker image:
1
|
docker build -t nginx-redis .
|
- Run the Docker container:
1
|
docker run -p 8080:80 nginx-redis
|
Now, your Nginx container with the Redis module should be running on port 8080. Customize the configuration files as needed for your specific use case.
参考