首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试NGINX配置

测试NGINX配置
EN

Stack Overflow用户
提问于 2022-02-15 15:02:07
回答 1查看 750关注 0票数 1

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

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

我一直在考虑建立一个带有docker的环境,使用curl/wget来测试我想要测试的curl列表。我不知道的是如何模拟某些域并跟踪转发的请求。

是否有这样的工具,或者我应该手动编写服务器?

EN

回答 1

Stack Overflow用户

发布于 2022-02-17 15:37:34

在做了一些实验之后,我成功地创建了这个解决方案。

使用码头组成,威瑞克和纽曼。这样做的目的是将NGINX代理请求设置到Wiremock (在这里,您可以控制请求是否与正确的结构匹配),然后使用Newman,您可以运行一个Postman集合,该集合会自动检查短消息的响应是否正确。

示例

在文件夹中创建所有这些文件,通过运行

代码语言:javascript
复制
docker-compose up -d nginx wiremock

然后,运行测试套件

代码语言:javascript
复制
docker-compose run --rm newman

它应该打印集合的结果。

文件

docker-compose.yml

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
{
  "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

代码语言:javascript
复制
{
  "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

代码语言:javascript
复制
{
    "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": []
        }
    ]
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71128693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档