基于docker+jenkins+git构建ci/cd(2)之nginx镜像和uwisg的设置

WSGI

WSGI的全称是Web Server Gateway Interface(Web服务器网关接口),它不是服务器、python模块、框架、API或者任何软件,只是一种描述web服务器(如nginx,uWSGI等服务器)如何与web应用程序(如用Django、Flask框架写的程序)通信的规范。server和application的规范在PEP3333中有具体描述,要实现WSGI协议,必须同时实现web server和web application,当前运行在WSGI协议之上的web框架有Bottle, Flask, Django。

uWSGI

uWSGI是一个全功能的HTTP服务器,实现了WSGI协议、uwsgi协议、http协议等。它要做的就是把HTTP协议转化成语言支持的网络协议。比如把HTTP协议转化成WSGI协议,让Python可以直接使用。

uwsgi

与WSGI一样,是uWSGI服务器的独占通信协议,用于定义传输信息的类型(type of information)。每一个uwsgi packet前4byte为传输信息类型的描述,与WSGI协议是两种东西,据说该协议是fcgi协议的10倍快。

Nginx

Nginx是一个Web服务器其中的HTTP服务器功能和uWSGI功能很类似,但是Nginx还可以用作更多用途,比如最常用的反向代理功能

正向代理器VS反向代理器

正向代理器

正向代理器是一个位于使用者端以及目标服务器之间的代理服务器(代理服务器),

透过代理伺服器收发Request / Respond资料,

如上图,X无法直接和Z请求资料,但是Y可以和Z请求资料(于是X透过Y向Z请求资料),

Y得到资料后,再将资料回传​​给X,这样X就能顺利得到Z的资料了。

通常正向代理器必须额外做一些设定才可以使用。

正向代理器,Client和Proxy可视为同一个LAN,对服务器是隐藏的。

温馨小提醒 ❤️

  • LAN(局域网),又称区域网路

  • WAN(广域网),又称广域网路

  • WLAN(无线局域网),又称无线区域网路

正向代理器是代理使用者端(Client端),替使用者端(Client端)收发请求/响应,

让真正的使用者对伺服器端(Server端)隐藏。

可能你自己也有使用过(或正在使用 ?)正向代理器,只是你不知道而以,像最常见的就是翻墙(VPN) ?

今天主角是阿鬼,阿鬼无法浏览FB,但可以浏览服务器A,而阿鬼发现服务器A可以访问FB,

于是阿鬼就透过Server A去浏览FB,整个流程是,阿鬼透过服务器A去浏览FB,服务器A收到

来自阿鬼的请求时,去浏览FB,FB再把响应回传给服务器A,服务器A再把收到的

回复回传给阿鬼,这样阿鬼就能顺利得到FB的资料,这就是透过Proxy(正向代理器)。

反向代理器

反向代理器则相反过来,对于使用者端来说,反向代理器就好像是目标Server,

使用者端也不需要做额外的设定,

如上图,虽然整个流程还是X-> Y-> Z,但因为不用特别设定,所以使用者端会感觉好像直接是X-> Z,

反向代理器,Proxy和Server属于一个LAN,对客户端隐藏

反向代理器是代理伺服器端(Server端),替伺服器端(Server端)收发请求/响应,

让真正的伺服器(Server端)对使用者隐藏。

访问过程

通过docker 构建nginx镜像 

FROM nginx:latest

COPY nginx.conf /etc/nginx/nginx.conf
COPY my_nginx.conf /etc/nginx/sites-available/

RUN mkdir -p /etc/nginx/sites-enabled/\
    && ln -s /etc/nginx/sites-available/my_nginx.conf /etc/nginx/sites-enabled/

# RUN mkdir -p /etc/nginx/sites-enabled/\
#     && ln -s /etc/nginx/sites-available/my_nginx.conf /etc/nginx/sites-enabled/\
#     && rm /etc/nginx/conf.d/default.conf

CMD ["nginx", "-g", "daemon off;"]

自己的nginx的配置文件my_nginx.conf内容:

upstream uwisg {
     #server 127.0.0.1:8001; # use TCP
    server unix:/docker_api/bbs.sock; # for a file socket
}

# configuration of the server
server {
    # the port your site will be served on
    listen    8000;
    # index  index.html;
    # the domain name it will serve for
    # substitute your machine's IP address or FQDN
    # server_name  .example.com;
    charset     utf-8;

    client_max_body_size 75M;   # adjust to taste

    # Django media
    # location /media  {
    #     alias /docker_api/media;  # your Django project's media files - amend as required
    # }

    location /static {
        alias /docker_api/static; # your Django project's static files - amend as required
    }
    
    location / {
        uwsgi_pass  uwisg;
        uwsgi_read_timeout 600;
        uwsgi_send_timeout 600;
        uwsgi_connect_timeout 600;
        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
    }

}

以及nginx的一些基础设置配置。最重要的是告诉nginx去哪里找我们的配置文件:

user  root;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    # include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-available/*;

关于uwsgi.ini的设计:

[uwsgi]
socket = bbs.sock
http = :9000
master=true
# maximum number of worker processes
processes=4
threads=2
# Django's wsgi file
module=bbs.wsgi:application
max-requests=5000
# chmod-socket=664
# uid=www-data
# gid=www-data
# clear environment on exit

关于uwsgi和nginx的相关配置可以查看https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html这个教学文档,其中有nginx和uwsgi的相关配置

关于django的Dockerfile设计:

FROM python:3.6.2
ENV PYTHONUNBUFFERED 1
RUN mkdir /docker_api
WORKDIR /docker_api
COPY . /docker_api
#RUN pip install -r requirements.txt
RUN pip install  -i  https://pypi.python.org/simple/  -r requirement.txt

还有一篇关于这两个容器的docker-compose.yaml的内容:

version: '3'
services:
    nginx:
        container_name: nginx-container        
        build: ./nginx
        restart: always
        ports:
        - "8000:8000"
        volumes:        
        - api_data:/docker_api
        - ./log:/var/log/nginx
        depends_on:
        - django
    django:
        container_name: django-container
        build: ./       
        restart: always
        # command: uwsgi  --emperor uwsgi.ini
        command: uwsgi --ini uwsgi.ini
        ports:
        - "8001:8001"
        - "9000:9000"
        volumes:
        - api_data:/docker_api

volumes:
    api_data

在jenkins构建中执行相应的shell脚本或者命令通过编译最新的代码添加至django镜像中,并将镜像推到自己私有仓库或者阿里云镜像仓库或者自己搭建一个harbor

#私有仓库地址
REG_URL=xxx

#$WORKSPACE分配给构建作为工作空间的目录的绝对路径。
#根据时间生成版本号

TAG=$REG_URL/$JOB_NAME

cd $WORKSPACE/

#使用我们刚才写好的 放在项目下面的Dockerfile 文件打包 
#
sudo docker login --username=xxxx -p=xxxx registry.cn-hangzhou.aliyuncs.com
sudo docker build -t  $JOB_NAME  $WORKSPACE/.
sudo docker tag     $JOB_NAME   $TAG
sudo docker push   $TAG
sudo docker rmi $TAG

 

转载于:https://www.cnblogs.com/Dxd-python/p/10642132.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值