【问题标题】:Testing NGINX configuration测试 NGINX 配置
【发布时间】:2022-02-17 23:37:34
【问题描述】:

我有一个带有 NGINX 的反向代理服务器,我想自动测试它的配置。

我最终想要实现的是有一个可以运行的命令,它使用配置启动 NGINX,运行几个 http 请求,然后跟踪并收集是否调用了正确的代理服务器。

我一直在考虑使用 docker-compose 设置环境,并将 curl/wget 与我要测试的 url 列表一起使用。我不知道的是如何模拟某些域并跟踪转发的请求。

是否有工具可以做到这一点,还是我应该手动编写服务器?

【问题讨论】:

    标签: nginx automated-tests nginx-reverse-proxy


    【解决方案1】:

    经过一番试验,我设法创建了这个解决方案。

    使用 Docker Compose、Wiremock 和 Newman。想法是将 NGINX 代理请求设置到 Wiremock(您可以在其中控制请求是否匹配正确的结构),然后使用 Newman,您可以运行 Postman 集合,该集合会自动检查存根响应是否正确。

    示例

    在一个文件夹中创建所有这些文件,通过运行获得测试环境

    docker-compose up -d nginx wiremock
    

    然后,运行测试套件

    docker-compose run --rm newman
    

    它应该打印集合的结果。

    文件

    docker-compose.yml

    version: "3"
    
    services:
      nginx:
        image: nginx
        ports:
          - "80:80"
        volumes:
          - ./config:/etc/nginx
    
      wiremock:
        image: wiremock/wiremock:2.32.0
        command: [ "--port", "80", "--verbose" ]
        ports:
          - "8080:80"
        volumes:
          - ./wiremock:/home/wiremock
        networks:
          default:
            aliases:
              - backend-service-1
              - backend-service-2
    
      newman:
        image: postman/newman
        volumes:
          - ./newman:/etc/newman
        command: [ "run", "example.postman_collection.json" ]
    
    

    config/nginx.conf

    events {
      worker_connections  1024;
    }
    
    http {
        resolver 127.0.0.11; # docker internal resolver
    
        server {
            listen 80 default_server;
    
            location /some/path/ {
                proxy_set_header X-Forwarded-Host $host;
                proxy_pass http://backend-service-1/some/path;
            }
    
            location /other/path/ {
                proxy_set_header X-Forwarded-Host $host;
                proxy_pass http://backend-service-2/other/path;
            }
        }
    }
    

    wiremock/mappings/some-path.json

    {
      "request": {
        "method": "GET",
        "url": "/some/path",
        "headers": {
          "Host": {
            "equalTo": "backend-service-1",
            "caseInsensitive": true
          }
        }
      },
      "response": {
        "status": 200,
        "body": "{\"host\": \"from-1\"}",
        "headers": {
          "Content-Type": "application/json"
        }
      }
    }
    

    wiremock/mappings/other-path.json

    {
      "request": {
        "method": "GET",
        "url": "/other/path",
        "headers": {
          "Host": {
            "equalTo": "backend-service-2",
            "caseInsensitive": true
          }
        }
      },
      "response": {
        "status": 200,
        "body": "{\"host\": \"from-2\"}",
        "headers": {
          "Content-Type": "application/json"
        }
      }
    }
    

    newman/example.postman_collection.json

    {
        "info": {
            "name": "example",
            "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
        },
        "item": [
            {
                "name": "some path",
                "event": [
                    {
                        "listen": "test",
                        "script": {
                            "exec": [
                                "pm.test(\"request backend service 1\", function () {",
                                "    pm.response.to.have.status(200);",
                                "",
                                "    var jsonData = pm.response.json();",
                                "    pm.expect(jsonData.host).to.eql(\"from-1\");",
                                "});",
                                ""
                            ],
                            "type": "text/javascript"
                        }
                    }
                ],
                "request": {
                    "method": "GET",
                    "header": [],
                    "url": {
                        "raw": "http://nginx/some/path/",
                        "protocol": "http",
                        "host": [
                            "nginx"
                        ],
                        "path": [
                            "some",
                            "path",
                            ""
                        ]
                    }
                },
                "response": []
            },
            {
                "name": "other path",
                "event": [
                    {
                        "listen": "test",
                        "script": {
                            "exec": [
                                "pm.test(\"request backend service 2\", function () {",
                                "    pm.response.to.have.status(200);",
                                "",
                                "    var jsonData = pm.response.json();",
                                "    pm.expect(jsonData.host).to.eql(\"from-2\");",
                                "});",
                                ""
                            ],
                            "type": "text/javascript"
                        }
                    }
                ],
                "request": {
                    "method": "GET",
                    "header": [],
                    "url": {
                        "raw": "http://nginx/other/path/",
                        "protocol": "http",
                        "host": [
                            "nginx"
                        ],
                        "path": [
                            "other",
                            "path",
                            ""
                        ]
                    }
                },
                "response": []
            }
        ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-01
      • 2013-09-06
      • 1970-01-01
      • 2011-08-19
      • 2013-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多