天天看点

容器化nginx代理实践搭建postwoman在线接口测试工具必知必会实践过程

容器化nginx代理实践搭建postwoman在线接口测试工具

  • 必知必会
    • nginx基础知识
    • docker基础知识
    • 接口报文测试基础知识
  • 实践过程
    • 搭建postwoman容器
    • 搭建nginx容器

必知必会

nginx基础知识

请确保对于以下问题,你已知道应该如何操作:

  • nginx正向代理?
  • nginx反向代理?
  • 如何测试nginx配置文件、使修改后的nginx配置生效?

    若上述问题中,有一些你尚无法作答,请先阅读相关的材料并掌握对应的实践方法。

docker基础知识

请确保对于以下问题,你已知道应该如何操作:

  • 如何拉取(pull)、打包(save)、载入(load)、标记(tag)、推送(push)镜像?
  • 如何启动一个容器,并为其指定挂载目录(与宿主机共享文件夹)、指定端口挂载?
  • 如何进入一个容器?
  • 如何修改容器中的文件?

    若上述问题中,有一些你尚无法作答,请先阅读相关的材料并掌握对应的实践方法。

接口报文测试基础知识

请确保对于以下问题,你已知道应该如何操作:

  • 如何在 postman 或 linux curl 等工具中对一个接口发送一段报文?

    若上述问题中,有一些你尚无法作答,请先阅读相关的材料并掌握对应的实践方法。

实践过程

搭建postwoman容器

docker pull liyasthomas/postwoman
docker run --name postwoman -d liyasthomas/postwoman
           

注意要加上-d使postwoman容器转入后台运行,从而不会占据你的命令输入。

搭建nginx容器

docker pull nginx
docker run --name nginx -p 80:80 --link postwoman:postwoman -d nginx:latest
           

修改nginx容器配置文件并生效方法:

docker cp nginx:/etc/nginx/nginx.conf .../nginx.conf
vim .../nginx.conf
# 编辑方法请参考下面一段
docker cp .../nginx.conf nginx:/etc/nginx/nginx.conf
docker exec -it nginx /bin/bash
/usr/sbin/nginx -t
/usr/sbin/nginx -s reload
exit
           

其中,vim编辑nginx.conf内容如下:(找到http段,在http段内的底部,添加server段)

http {
    ...
    server {
        listen 80;
        server_name abc.com;
        location = / {
    		proxy_pass http://postwoman:3000;
		}
		location /cn {
    		proxy_pass http://postwoman:3000;
		}
		location /_nuxt {
    		proxy_pass http://postwoman:3000;
		}
        location ~ ^/ServerbInterface {
            proxy_pass http://IPb;
        }
        location ~ ^/ServercInterface {
            proxy_pass http://IPc;
        }
    }
}
           

IPb、IPc是提供不同的/Server…Interface接口的服务所在IP地址。

如果/Server…Interface都已统一到一个服务IP下,则可以简写作:

location = / {
    proxy_pass http://postwoman:3000;
}
location /cn {
    proxy_pass http://postwoman:3000;
}
location /_nuxt {
    proxy_pass http://postwoman:3000;
}
location / {
    proxy_pass http://IP;
}
           

实时查看nginx日志文件方法:

docker logs nginx
           

注:nginx容器中,/var/log/nginx/access.log链接指向/dev/stdout,error.log链接指向/dev/stderr,所以要在外面查看。容器里面这些文件是看不到内容的。

至于其他的添加add_header的方法,Google Chrome浏览器仍然是不允许跨域的,即使添加了Access-Control-Allow-Origin之类,也不能通过Google Chrome浏览器自己实现的同源策略跨域检查。

浏览器访问前,本地配置hosts:

IPa abc.com
           

IPa是运行docker和postwoman的服务器地址。

访问abc.com即可使用postwoman。

继续阅读