天天看點

容器化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。

繼續閱讀