借助 ChatGPT 解决运维问题:Nginx Location 块匹配删除

人工智能
GPT理解了我们的意思,但是没有理解很准确,群友的需求是匹配到proxy_pass 后面的地址,然后删除整个location块的内容,这里线验证下基础localhost 路由的匹配删除,再来调试其他的。

背景

昨天(2024年9月5日),一个运维同事在群里发布了一个需求,聊天记录如下。

图片图片

解决过程

此时,我们直接将配置文件图片丢给GPT,并给它一个解决需求的提示词。这里就直接复制群里兄弟的聊天昨天需求提示词:

提示词如下:

比如我过滤出这个服务名,怎么删除包含这个服务名的location,整个location,因为不一定有多少行,没法按照行数删

图片图片

图片图片

GPT理解了我们的意思,但是没有理解很准确,群友的需求是匹配到proxy_pass 后面的地址,然后删除整个location块的内容,这里线验证下基础localhost 路由的匹配删除,再来调试其他的。

测试验证

$ cat nginx.conf 
location /dataCenterService {
    proxy_pass http://semantic-datacenter-service:31001/;
}

location /uploadDemo {
    client_max_body_size 20M;
    proxy_send_timeout 1200s;
    proxy_read_timeout 1200s;
    proxy_connect_timeout 1200s;
    send_timeout 1200s;
    proxy_pass http://intelligent-quality-inspection-service:31001/;
}

location /accountService {
    proxy_pass http://hc-account-analysis-service:31001/;
}

location /qa2020Service{
    proxy_pass http://10.0.0.22:9188/;
}

执行命令测试:

这里专门把 -i参数去掉了,这样防止意外修改文件。

$ sed  '/location \/accountService {/,/}/d' nginx.conf
location /dataCenterService {
    proxy_pass http://semantic-datacenter-service:31001/;
}

location /uploadDemo {
    client_max_body_size 20M;
    proxy_send_timeout 1200s;
    proxy_read_timeout 1200s;
    proxy_connect_timeout 1200s;
    send_timeout 1200s;
    proxy_pass http://intelligent-quality-inspection-service:31001/;
}


location /qa2020Service{
    proxy_pass http://10.0.0.22:9188/;
}

可以看到结果是符合预期了,可以将包含某个location路由的nginx块给去掉。接下来我继续通过提示词调试GPT的输出,满足匹配到proxy_pass 后面的地址,然后删除整个localhost 块的内容.

我们继续向chatgpt 发送提示词:

很棒,但我的需求是匹配到proxy_pass 后面的地址,然后删除整个localhost 块的内容.

图片图片

GPT给了一条命令我们来测试下:

命令执行结果如下:

$ sed  '/location \//,/}/ {/proxy_pass .*hc-account-analysis-service/!b};/location \//,/}/d' nginx.conf
location /dataCenterService {
    proxy_pass http://semantic-datacenter-service:31001/;
}

location /uploadDemo {
    client_max_body_size 20M;
    proxy_send_timeout 1200s;
    proxy_read_timeout 1200s;
    proxy_connect_timeout 1200s;
    send_timeout 1200s;
    proxy_pass http://intelligent-quality-inspection-service:31001/;
}

location /accountService {
    proxy_pass http://hc-account-analysis-service:31001/;
}

location /qa2020Service{
    proxy_pass http://10.0.0.22:9188/;
}

可以看到没有任何变化,于是我继续调整提示词:

  • 结果不太符合预期
  • 附上命令及输出结果

图片图片

GPT很快理解了我的意思,又给我生成了一条命令:

图片图片

$ sed  '/location \//{N;/: {/,/proxy_pass .*hc-account-analysis-service/!b};/}/d}' nginx.conf
sed: -e expression #1, char 73: unexpected `}'

接着我继续将命令输出结果发送给gpt:

图片图片

这次还是错的,经历了几轮调试之后,gpt终于给出了符合需求的命令:

图片图片

配置文件

$ cat nginx.conf 
location /dataCenterService {
    proxy_pass http://semantic-datacenter-service:31001/;
}

location /uploadDemo {
    client_max_body_size 20M;
    proxy_send_timeout 1200s;
    proxy_read_timeout 1200s;
    proxy_connect_timeout 1200s;
    send_timeout 1200s;
    proxy_pass http://intelligent-quality-inspection-service:31001/;
}

location /accountService {
    proxy_pass http://hc-account-analysis-service:31001/;
}

location /qa2020Service{
    proxy_pass http://10.0.0.22:9188/;
}

删除proxy_pass 包含hc-account-analysis-service的块:

$ sed -n '/location \//{:a;N;/}/!ba;/proxy_pass .*hc-account-analysis-service/{d};p}' nginx.conf
location /dataCenterService {
    proxy_pass http://semantic-datacenter-service:31001/;
}
location /uploadDemo {
    client_max_body_size 20M;
    proxy_send_timeout 1200s;
    proxy_read_timeout 1200s;
    proxy_connect_timeout 1200s;
    send_timeout 1200s;
    proxy_pass http://intelligent-quality-inspection-service:31001/;
}
location /qa2020Service{
    proxy_pass http://10.0.0.22:9188/;
}

删除proxy_pass 包含intelligent-quality-inspection-service的块

$ sed -n '/location \//{:a;N;/}/!ba;/proxy_pass .*intelligent-quality-inspection-service/{d};p}' nginx.conf
location /dataCenterService {
    proxy_pass http://semantic-datacenter-service:31001/;
}
location /accountService {
    proxy_pass http://hc-account-analysis-service:31001/;
}
location /qa2020Service{
    proxy_pass http://10.0.0.22:9188/;
}

大功告成!

责任编辑:武晓燕 来源: 云原生运维圈
相关推荐

2021-11-15 05:52:50

nginx服务器运维

2023-05-18 16:09:06

2023-06-13 08:00:57

ChatGPT语言模型

2023-05-05 19:16:22

Python数据清洗

2009-09-15 21:21:54

IT服务运维管理摩卡软件

2009-06-04 14:53:48

2020-10-28 10:40:35

运维安全技术

2016-12-13 13:15:49

运维

2019-03-19 08:41:38

Linux运维变更

2018-11-15 11:52:36

百度云运维AIOps

2020-12-08 12:50:17

向日葵远程运维

2012-05-15 09:57:39

运维安全\运维审计

2018-04-10 09:49:17

IT运维人员京东运维体系

2010-01-21 22:19:25

网络优化运维管理摩卡软件

2018-09-07 15:34:25

Linux运维故障

2021-04-15 11:22:36

运维架构技术

2024-11-29 07:42:47

2019-03-15 10:13:10

运维云计算运营

2015-12-23 10:44:02

点赞
收藏

51CTO技术栈公众号