整理了一些方便nginx 正則測試的工具
使用官方提供的一個基於php 的環境
nginx-regex-tester
使用pcretest
對於alpine docker 鏡像可以通過安裝pcre-tools 解決
參考集成環境
nginx-regex-tester我已經構建過一個docker鏡像,以下是pcretest的
- dockerfile
FROM nginx:alpine
LABEL author="dalongrong"
RUN set -x \
&& /bin/sed -i 's,http://dl-cdn.alpinelinux.org,https://mirrors.aliyun.com,g' /etc/apk/repositories \
&& apk update && apk add --no-cache pcre-tools
COPY --from=yaronpr/gotty /usr/local/bin/gotty /usr/local/bin/gotty
EXPOSE 8080
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT [ "/entrypoint.sh" ]
entrypoint.sh
#!/bin/sh
nohup gotty -w sh &
nginx -g "daemon off;"
pcretest 簡單說明
第一行寫 /中間加上正則表達式/
第二行寫 要匹配的字符
結果說明:
0表示匹配到的所有內容
1最后表示每個位置匹配到的內容
集成環境docker-compose
- docker-compose 文件
version: "3"
services:
nginx-test:
build: ./
image: dalongrong/nginx-pcretest
ports:
- "80:80"
nginx-regex-test:
image: dalongrong/nginx-regex-tester_regextester:latest
ports:
- "8080:80"
- 使用
nginx-regex-tester http://localhost:8080/regextester.php
pcretest http://localhost:9090 (webconsole 操作)
golang supervisord 版本的pcretest dockerfile
FROM nginx:alpine
LABEL author="dalongrong"
RUN set -x \
&& /bin/sed -i 's,http://dl-cdn.alpinelinux.org,https://mirrors.aliyun.com,g' /etc/apk/repositories \
&& apk update && apk add --no-cache pcre-tools
COPY --from=yaronpr/gotty /usr/local/bin/gotty /usr/local/bin/gotty
COPY --from=ochinchina/supervisord:latest /usr/local/bin/supervisord /usr/local/bin/supervisord
EXPOSE 8080 80
COPY supervisor.conf /etc/supervisor.conf
CMD ["/usr/local/bin/supervisord","-c","/etc/supervisor.conf"]
supervisor.conf
[program:website]
command = nginx -g "daemon off;"
[inet_http_server]
port = :9001
[program:gotty]
command = gotty -w sh
說明
以上運行集成了gotty一個很不錯的webconsole 工具,對於開發環境還是很不錯的選擇,可以方便測試,同時我也已經制作了
一個集成好的docker鏡像dalongrong/nginx-pcretest
可以方便使用
參考資料
https://www.cnblogs.com/rongfengliang/p/13150534.html
https://github.com/nginxinc/NGINX-Demos/tree/master/nginx-regex-tester
https://github.com/yudai/gotty
https://github.com/rongfengliang/nginx-pcretest-docker