浏览 3.2k
現在使用的Nginx使用了根據Cookie裏面的一個字段來RewiriteURL。 往 Nginx Ingress Controller遷移後,有沒有類似簡單的寫法。
現有的Nginx定義
map $cookie_target $subpath {
1 subpath1;
2 subpath2;
3 subpath3;
・・・
default subpathdefault;
}
server {
location / {
rewrite ^(.{5})(.*)$ http://www.example.com/$1/$subpath/$2 break;
}}
現在找到的對應案, 大約有70個Cookie的種類 只能老老實實寫70個conditions麽?
apiVersion: k8s.nginx.org/v1
kind: VirtualServer
metadata:
name: cafe
spec:
host: cafe.example.com
upstreams:
- name: coffee
service: coffee-svc
port: 80
- name: tea
service: tea-svc
port: 80
routes:
- path: /(.{5})(.*)
matches:
- conditions:
- cookie: target
value: 1
action:
proxy:
upstream: coffee
rewritePath: http://www.example.com/$1/subpath1/$2
- conditions:
- cookie: target
value: 2
action:
proxy:
upstream: coffee
rewritePath: http://www.example.com/$1/subpath2/$2
・・・・・
action:
proxy:
upstream: coffee
rewritePath: http://www.example.com/$1/subpathdefault/$2

按点赞数排序
按时间排序
介绍在nginx中自定义请求头字段,并在日志中展示相关字段的值:
第一种方法:
1 在代理nginx服务器配置中增加两个proxy_set_header字段,一个iden,一个age,分别赋值为"student","21",我们将来在日志中看到这两个常量;
server {
server_name 127.0.0.1;
listen 80;
location /header {
default_type "text/plain";
access_log /data/nginx/log/proxy_header.log api;
proxy_redirect off;
proxy_set_header iden "student";
proxy_set_header age "21";
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:88;
}
}
此时代理nginx将请求头重写,传入后端服务器,所以在此nginx服务器日志上看不到相关字段信息,为了演示方便,将此服务转发到同机器88端口的服务上;
2 配置后端服务相关配置:端口号88,返回200;
server {
server_name 127.0.0.1;
listen 88;
location / {
access_log /data/nginx/log/proxy_header.log api;
proxy_redirect off;
proxy_set_header Host $host;
default_type "text/plain";
return 200 "OK";
}
}
3 在 api的log_format中,增加两个字段;
"$http_iden" "$http_age"
4 curl 测试接口;
curl http://127.0.0.1/header
5 查看日志;

发现第一条日志记录有相关字段信息,是后端nginx服务的日志信息;第二条日志无字段相关信息,是代理nginx服务器的日志;
结论:使用proxy_set_header自定义字段时,后端服务器日志能获取到自定义的相关信息;
第二种方法:
1 在代理nginx服务器中配置set 关键字,定义两个字段,同样转发到统计器的88端口;
server {
server_name 127.0.0.1;
listen 80;
location /header {
set $iden "student";
set $age "21";
default_type "text/plain";
access_log /data/nginx/log/proxy_header.log api;
proxy_redirect off;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:88;
}
}
2 配置后端服务相关配置:端口号88,返回200,与第一种方法保持一致;
server {
server_name 127.0.0.1;
listen 88;
location / {
access_log /data/nginx/log/proxy_header.log api;
proxy_redirect off;
proxy_set_header Host $host;
default_type "text/plain";
return 200 "OK";
}
}
3 修改api的log_format中的两个字段;
"$iden" "$age"
4 curl 测试接口;
curl http://127.0.0.1/header
5 查看日志;

发现第一条日志记录无相关字段信息,是后端nginx服务的日志信息;第二条日志有字段相关信息,是代理nginx服务器的日志;
低版本,编译会打印md5和sha1。我测试1.10版本,是打印md5和sha1的,1.11以上不打印了,可以查看
https://github.com/nginx/nginx/tree/master源代码代码仓库auto/summary文件,里面有描述。下面为我本地Ubuntu18.04编译指令:
在代码根目录运行,./auto/configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream --with-stream_ssl_module --add-module=/home/lq/work/tongmingzhiyun/openresty/lua-nginx-module-0.10.21 --add-module=/home/lq/work/tongmingzhiyun/openresty/ngx_devel_kit-0.3.1
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ md5: using OpenSSL library
+ sha1: using OpenSSL library
+ using system zlib library
nginx path prefix: "/opt/nginx"
nginx binary file: "/opt/nginx/sbin/nginx"
nginx modules path: "/opt/nginx/modules"
nginx configuration prefix: "/opt/nginx/conf"
nginx configuration file: "/opt/nginx/conf/nginx.conf"
nginx pid file: "/opt/nginx/logs/nginx.pid"
nginx error log file: "/opt/nginx/logs/error.log"
nginx http access log file: "/opt/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
微信公众号
加入微信群