nginx访问日志GET后面用http://开头

今天检查nginx访问日志的时候发现奇怪的日志

[02/Nov/2017:13:35:46 +0800] "GET http://www.baidu.com/ HTTP/1.1" 200 8672 "http://www.baidu.com" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0" -

截图如下

未分类

这里取的只是百度的域名,还有好多其他的域名,这个应该是代理攻击

有没有发现GET后面用http://开头的,正常情况GET后面是用”/”开头,如下面的访问日志

[08/Nov/2017:21:54:15 +0800] "GET /2330.html HTTP/1.1" 200 8419 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" -

这个问题在百度gogole的相关文章比较少,所以现在不清楚目前的处理方法是否正确,如果你要遇到了这个问题,可以尝试一下

目前处理的方法

#nginx中添加下面的配置,主要作用就是没有指定域名的返回444

server {
    server_name _;
    location / { 
        return 444; 
    }
}
server
{

    listen 443 ssl http2;
    server_name www.phpsong.com;
    //...


    #添加代码,主要作用如果域名不是www.phpsong.com的就返回444
    if ($host !~ ^(phpsong.com|www.phpsong.com)$) {
              return 444;
    }

    //...
}

设置之后,发现这种日志不存在了,因为不清楚模拟这种攻击,所以不清楚是否已经解决了,apache的解决方法可以看一下这个文章https://wiki.apache.org/httpd/ProxyAbuse

如何在nginx下实现访问web网站密码认证保护的功能

在某些特定的环境下,我们希望nginx下的web站点在访问时需要用户输入账户密码才能访问。以便拒绝那些不速之客。

其实,配置起来也很简单,按照下面的步骤即可实现。

一、编辑虚拟主机配置文件。

server { 
     listen       80;  //监听端口为80 
     server_name  www.abc.com;  //虚拟主机网址 
     location / { 
            root   sites/www1;  //虚拟主机网站根目录 
            index  index.html index.htm;  //虚拟主机首页 
            auth_basic "secret";  //虚拟主机认证命名 
            auth_basic_user_file /usr/local/nginx/passwd.db; //虚拟主机用户名密码认证数据库 
      } 
      #省略其他配置
      ....
}

二、通过htpasswd命令生成用户名及对应密码数据库文件。

[root@localhost /]#mkdir /usr/local/nginx
[root@localhost /]#vi /usr/local/nginx/passwd.db
[root@localhost /]# htpasswd -c /usr/local/nginx/passwd.db admin  //创建认证信息,admin 为认证用户名 
New password: *******  //输入认证密码 
Re-type new password: ********  //再次输入认证密码 
Adding password for user admin 
[root@localhost /]# chmod 400 /usr/local/nginx/passwd.db      //修改网站认证数据库权限 
[root@localhost /]# chown nginx.  /usr/local/nginx/passwd.db  //修改网站认证数据库属主和属组
[root@localhost /]# cat /usr/local/nginx/passwd.db            //可以看到通过htpasswd生成的密码为加密格式
admin:x/J1o94NgIV2k

三、重新启动nginx即可生效。

service nginx reload

通过上面的三个步骤即可完成在nginx下实现访问web网站密码认证保护的功能。

nginx显示文件目录列表并提供文件下载服务

测试环境:

centos 7 64 位

安装并启动nginx

# yum -y install wget vim
# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# yum clean all
# yum -y install nginx
# iptables -F
# service nginx start

更改nginx配置文件

# cd /etc/nginx/
# cp nginx.conf nginx.conf.bk
# vim nginx.conf   <--找到server字段下的location,更改如下
.......
.......
server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        location / {
charset utf-8;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
if ($request_filename ~* ^.*?.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$){
            add_header Content-Disposition attachment;
            }
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
.......
....... 

新建一些测试文件和文件夹

# cd /usr/share/nginx/html/
# rm -fr *
# touch A{1..9}.txt
# mkdir docs
# mkdir pdf
# chown nginx.nginx /usr/share/nginx/html/ -R
# service nginx reload

浏览器验证访问,如图

未分类

Nginx作HTTP的负载均衡max_fails和fail_timeout如何设置?

很多同学,经常利用Nginx来作HTTP的负载均衡,但是对于Nginx的负载均衡原理却了解得不够,如,今天讲解的max_fails、fail_timeout参数。

一、描述

在Nginx的负载均衡检查模块中,对于负载均衡的节点可以配置如下可选参数参数:

max_fails=1 fail_timeout=10s

这个是Nginx在负载均衡功能中,用于判断后端节点状态,所用到两个参数。

Nginx基于连接探测,如果发现后端异常,在单位周期为fail_timeout设置的时间,中达到max_fails次数,这个周期次数内,如果后端同一个节点不可用,那么接将把节点标记为不可用,并等待下一个周期(同样时常为fail_timeout)再一次去请求,判断是否连接是否成功。

默认:fail_timeout为10s,max_fails为1次。

二、探讨

经过这几天的压力测试和研究,如下:

1、在后端服务能力正常,nginx 可以调节如下阀值,增加其并发能力。

这些参数主要是一次请求,超时时间间隔。课程中有介绍到。

client_header_timeout 30;
client_body_timeout 30;
send_timeout 30;
read_timeout 30;```

这里,表示允许后端节点失败次数,如果调大,Nginx相当于把请求缓冲,如果整体的的后端服务处于可用状态,对于高并发的场景来说,建议适当调大是有效的。

max_fails=10 fail_timeout=60s`

当然,和所有优化阀值一样,数值加大,对应于给Nginx的压力也会相应加大。

2、检测方式及对后端服务性能

Nginx只有当有访问时后,才发起对后端节点探测。如果本次请求中,节点正好出现故障,Nginx将把请求转交给接下来的节点处理。所以不会影响到这次请求的正常进行。

你真的掌握 LVS、Nginx 及 HAProxy 的工作原理吗

当前大多数的互联网系统都使用了服务器集群技术,集群是将相同服务部署在多台服务器上构成一个集群整体对外提供服务,这些集群可以是 Web 应用服务器集群,也可以是数据库服务器集群,还可以是分布式缓存服务器集群等等。

未分类

在实际应用中,在 Web 服务器集群之前总会有一台负载均衡服务器,负载均衡设备的任务就是作为 Web 服务器流量的入口,挑选最合适的一台 Web 服务器,将客户端的请求转发给它处理,实现客户端到真实服务端的透明转发。

最近几年很火的「云计算」以及分布式架构,本质上也是将后端服务器作为计算资源、存储资源,由某台管理服务器封装成一个服务对外提供,客户端不需要关心真正提供服务的是哪台机器,在它看来,就好像它面对的是一台拥有近乎无限能力的服务器,而本质上,真正提供服务的,是后端的集群。

LVS、Nginx、HAProxy 是目前使用最广泛的三种软件负载均衡软件。

一般对负载均衡的使用是随着网站规模的提升根据不同的阶段来使用不同的技术。具体的应用需求还得具体分析,如果是中小型的 Web 应用,比如日 PV 小于1000万,用 Nginx 就完全可以了;如果机器不少,可以用 DNS 轮询,LVS 所耗费的机器还是比较多的;大型网站或重要的服务,且服务器比较多时,可以考虑用 LVS。

目前关于网站架构一般比较合理流行的架构方案:Web 前端采用 Nginx/HAProxy+Keepalived 作负载均衡器;后端采用 MySQ L数据库一主多从和读写分离,采用 LVS+Keepalived 的架构。

LVS

LVS 是 Linux Virtual Server 的简称,也就是 Linux 虚拟服务器。现在 LVS 已经是 Linux 标准内核的一部分,从 Linux2.4 内核以后,已经完全内置了 LVS 的各个功能模块,无需给内核打任何补丁,可以直接使用 LVS 提供的各种功能。

LVS 自从1998年开始,发展到现在已经是一个比较成熟的技术项目了。

LVS 的体系结构

未分类

LVS 架设的服务器集群系统有三个部分组成:

  • 最前端的负载均衡层,用 Load Balancer 表示
  • 中间的服务器集群层,用 Server Array 表示
  • 最底端的数据共享存储层,用 Shared Storage 表示

LVS 负载均衡机制

LVS 不像 HAProxy 等七层软负载面向的是 HTTP 包,所以七层负载可以做的 URL 解析等工作,LVS 无法完成。

LVS 是四层负载均衡,也就是说建立在 OSI 模型的第四层——传输层之上,传输层上有我们熟悉的 TCP/UDP,LVS 支持 TCP/UDP 的负载均衡。因为 LVS 是四层负载均衡,因此它相对于其它高层负载均衡的解决办法,比如 DNS 域名轮流解析、应用层负载的调度、客户端的调度等,它的效率是非常高的。

所谓四层负载均衡 ,也就是主要通过报文中的目标地址和端口。七层负载均衡 ,也称为“内容交换”,也就是主要通过报文中的真正有意义的应用层内容。

未分类

LVS 的转发主要通过修改 IP 地址(NAT 模式,分为源地址修改 SNAT 和目标地址修改 DNAT)、修改目标 MAC(DR 模式)来实现。

NAT 模式:网络地址转换

NAT(Network Address Translation)是一种外网和内网地址映射的技术。

NAT 模式下,网络数据报的进出都要经过 LVS 的处理。LVS 需要作为 RS(真实服务器)的网关。

当包到达 LVS 时,LVS 做目标地址转换(DNAT),将目标 IP 改为 RS 的 IP。RS 接收到包以后,仿佛是客户端直接发给它的一样。RS 处理完,返回响应时,源 IP 是 RS IP,目标 IP 是客户端的 IP。这时 RS 的包通过网关(LVS)中转,LVS 会做源地址转换(SNAT),将包的源地址改为 VIP,这样,这个包对客户端看起来就仿佛是 LVS 直接返回给它的。

未分类

DR 模式:直接路由

DR 模式下需要 LVS 和 RS 集群绑定同一个 VIP(RS 通过将 VIP 绑定在 loopback 实现),但与 NAT 的不同点在于:请求由 LVS 接受,由真实提供服务的服务器(RealServer,RS)直接返回给用户,返回的时候不经过 LVS。

详细来看,一个请求过来时,LVS 只需要将网络帧的 MAC 地址修改为某一台 RS 的 MAC,该包就会被转发到相应的 RS 处理,注意此时的源 IP 和目标 IP 都没变,LVS 只是做了一下移花接木。RS 收到 LVS 转发来的包时,链路层发现 MAC 是自己的,到上面的网络层,发现 IP 也是自己的,于是这个包被合法地接受,RS 感知不到前面有 LVS 的存在。而当 RS 返回响应时,只要直接向源 IP(即用户的 IP)返回即可,不再经过 LVS。

未分类

DR 负载均衡模式数据分发过程中不修改 IP 地址,只修改 mac 地址,由于实际处理请求的真实物理 IP 地址和数据请求目的 IP 地址一致,所以不需要通过负载均衡服务器进行地址转换,可将响应数据包直接返回给用户浏览器,避免负载均衡服务器网卡带宽成为瓶颈。因此,DR 模式具有较好的性能,也是目前大型网站使用最广泛的一种负载均衡手段。

LVS 的优点

  • 抗负载能力强、是工作在传输层上仅作分发之用,没有流量的产生,这个特点也决定了它在负载均衡软件里的性能最强的,对内存和 cpu 资源消耗比较低。

  • 配置性比较低,这是一个缺点也是一个优点,因为没有可太多配置的东西,所以并不需要太多接触,大大减少了人为出错的几率。
    工作稳定,因为其本身抗负载能力很强,自身有完整的双机热备方案,如 LVS+Keepalived。

  • 无流量,LVS 只分发请求,而流量并不从它本身出去,这点保证了均衡器 IO 的性能不会受到大流量的影响。

  • 应用范围比较广,因为 LVS 工作在传输层,所以它几乎可以对所有应用做负载均衡,包括 http、数据库、在线聊天室等等。

LVS 的缺点

  • 软件本身不支持正则表达式处理,不能做动静分离;而现在许多网站在这方面都有较强的需求,这个是 Nginx、HAProxy+Keepalived 的优势所在。

  • 如果是网站应用比较庞大的话,LVS/DR+Keepalived 实施起来就比较复杂了,相对而言,Nginx/HAProxy+Keepalived就简单多了。

Nginx

Nginx 是一个强大的 Web 服务器软件,用于处理高并发的 HTTP 请求和作为反向代理服务器做负载均衡。具有高性能、轻量级、内存消耗少,强大的负载均衡能力等优势。

未分类

Nignx 的架构设计

相对于传统基于进程或线程的模型(Apache就采用这种模型)在处理并发连接时会为每一个连接建立一个单独的进程或线程,且在网络或者输入/输出操作时阻塞。这将导致内存和 CPU 的大量消耗,因为新起一个单独的进程或线程需要准备新的运行时环境,包括堆和栈内存的分配,以及新的执行上下文,当然,这些也会导致多余的 CPU 开销。最终,会由于过多的上下文切换而导致服务器性能变差。

反过来,Nginx 的架构设计是采用模块化的、基于事件驱动、异步、单线程且非阻塞。

Nginx 大量使用多路复用和事件通知,Nginx 启动以后,会在系统中以 daemon 的方式在后台运行,其中包括一个 master 进程,n(n>=1) 个 worker 进程。所有的进程都是单线程(即只有一个主线程)的,且进程间通信主要使用共享内存的方式。

其中,master 进程用于接收来自外界的信号,并给 worker 进程发送信号,同时监控 worker 进程的工作状态。worker 进程则是外部请求真正的处理者,每个 worker 请求相互独立且平等的竞争来自客户端的请求。请求只能在一个 worker 进程中被处理,且一个 worker 进程只有一个主线程,所以同时只能处理一个请求。(原理同 Netty 很像)

未分类

Nginx 负载均衡

Nginx 负载均衡主要是对七层网络通信模型中的第七层应用层上的 http、https 进行支持。

Nginx 是以反向代理的方式进行负载均衡的。反向代理(Reverse Proxy)方式是指以代理服务器来接受 Internet 上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给 Internet 上请求连接的客户端,此时代理服务器对外就表现为一个服务器。

Nginx 实现负载均衡的分配策略有很多,Nginx 的 upstream 目前支持以下几种方式:

  • 轮询(默认):每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。
  • weight:指定轮询几率,weight 和访问比率成正比,用于后端服务器性能不均的情况。
  • ip_hash:每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的问题。
    fair(第三方):按后端服务器的响应时间来分配请求,响应时间短的优先分配。
  • url_hash(第三方):按访问 url 的 hash 结果来分配请求,使每个 url 定向到同一个后端服务器,后端服务器为缓存时比较有效。

Nginx 的优点

  • 跨平台:Nginx 可以在大多数 Unix like OS编译运行,而且也有 Windows 的移植版本
  • 配置异常简单:非常容易上手。配置风格跟程序开发一样,神一般的配置
  • 非阻塞、高并发连接:官方测试能够支撑5万并发连接,在实际生产环境中跑到2~3万并发连接数
  • 事件驱动:通信机制采用 epoll 模型,支持更大的并发连接
  • Master/Worker 结构:一个 master 进程,生成一个或多个 worker 进程
  • 内存消耗小:处理大并发的请求内存消耗非常小。在3万并发连接下,开启的10个 Nginx 进程才消耗150M 内存(15M*10=150M)
  • 内置的健康检查功能:如果 Nginx 代理的后端的某台 Web 服务器宕机了,不会影响前端访问
  • 节省带宽:支持 GZIP 压缩,可以添加浏览器本地缓存的 Header 头
  • 稳定性高:用于反向代理,宕机的概率微乎其微

Nginx 的缺点

  • Nginx 仅能支 持http、https 和 Email 协议,这样就在适用范围上面小些,这个是它的缺点
  • 对后端服务器的健康检查,只支持通过端口来检测,不支持通过 ur l来检测。不支持 Session 的直接保持,但能通过 ip_hash 来解决

HAProxy

HAProxy 支持两种代理模式 TCP(四层)和HTTP(七层),也是支持虚拟主机的。

HAProxy 的优点能够补充 Nginx 的一些缺点,比如支持 Session 的保持,Cookie 的引导;同时支持通过获取指定的 url 来检测后端服务器的状态。

HAProxy 跟 LVS 类似,本身就只是一款负载均衡软件;单纯从效率上来讲 HAProxy 会比 Nginx 有更出色的负载均衡速度,在并发处理上也是优于 Nginx 的。

HAProxy 支持 TCP 协议的负载均衡转发,可以对 MySQL 读进行负载均衡,对后端的 MySQL 节点进行检测和负载均衡,大家可以用 LVS+Keepalived 对 MySQL 主从做负载均衡。

HAProxy 负载均衡策略非常多:Round-robin(轮循)、Weight-round-robin(带权轮循)、source(原地址保持)、RI(请求URL)、rdp-cookie(根据cookie)。

nginx 之前端图片webp

1. 需要有nginx+lua环境(参照前面)

2. 安装libjpeg,libpng

yum install libjpeg-turbo-devel libjpeg-turbo libpng-devel -y

3. 安装LibTIFF

cd /usr/local/src
wget http://download.osgeo.org/libtiff/tiff-4.0.8.tar.gz
tar -zxvf tiff-4.0.8.tar.gz
cd tiff-4.0.8
./configure
make
make install

4. 安装giflib

cd /usr/local/src
wget  https://downloads.sourceforge.net/giflib/giflib-5.1.4.tar.bz2
tar -jxvf giflib-5.1.4.tar.bz2
cd giflib-5.1.4
./configure
make
make install

5. 安装libwebp

cd /usr/local/src
wget http://downloads.webmproject.org/releases/webp/libwebp-0.6.0.tar.gz
tar -zxvf libwebp-0.6.0.tar.gz
cd libwebp-0.6.0
./configure --enable-libwebpmux 
--enable-libwebpdemux 
--enable-libwebpdecoder 
--enable-libwebpextras 
--enable-swap-16bit-csp 
--disable-static

make
make install

6. 测试转换

cwebp -q 75 IIS.png -o IIS-2.webp
cwebp -q 80 image.png -o image.webp

dwebp image.webp -o image.png

7. webp图片生成脚本

[root@tracker waf]# vim /usr/local/nginx/conf/lua/webp.lua
--检测路径是否目录,新增
local function is_dir(sPath)
    if type(sPath) ~= "string" then
        return false
     end
   local response = os.execute("cd " .. sPath)
    if response == 0 then
        return true
    end
    return false
end

-- 检测文件是否存在
function file_exists(name)
   local f=io.open(name,"r")
   if f~=nil then io.close(f) return true else return false end
end

--获取文件路径,新增
function getFileDir(filename)
    return string.match(filename, "(.+)/[^/]*%.%w+$")
end

-- 检测图片路径,新增
--if not is_dir(getFileDir(ngx.var.img_thumb_path)) then
--    os.execute("mkdir -p " .. getFileDir(ngx.var.img_thumb_path))
--  end
if not is_dir(getFileDir(ngx.var.filename)) then
    os.execute("mkdir -p " .. getFileDir(ngx.var.filename))
   end

local newFile = ngx.var.request_filename;
local originalFile = newFile:sub(1, #newFile - 5); -- 去掉 .webp 的后缀


if not file_exists(originalFile) then -- 原文件不存在
  ngx.exit(404);
  return;
end

--local command = [[/usr/local/GraphicsMagick/bin/gm convert -quality 75 -density 72 +profile "*"  ]] .. ngx.var.request_filepath ..  originalUri  .. " -geometry "  .. " " .. ngx.var.request_filename;
os.execute("/usr/local/bin/cwebp -q 75 " .. originalFile  .. " -o " .. newFile);   -- 转换原图片到 webp 格式,这里的质量是 75 ,你也可以改成别的

if file_exists(newFile) then -- 如果新文件存在(转换成功)
    ngx.exec(ngx.var.uri) -- Internal Redirect
else
    ngx.exit(404)
end

8. vhost.conf配置

server {
        listen 8216;
        #charset utf-8;
        server_name 10.160.43.105;
        index  index.php index.html;

        root /data/wwwroot/v2.50/wap.static/dist_dev;

        #if (!-e $request_filename) {
        #     rewrite .* /index.php last;
        #}
        # goole ngx_pagespeed
        # include extra/pagespeed.conf;

                #PHP support
                location ~ .*.php$ {
                        fastcgi_pass  127.0.0.1:9000;
                        fastcgi_index index.php;
                        include fastcgi.conf;
                }

                location ~* ^(.+.(jpg|jpeg|gif|png)).webp$ {
                        set     $webp_root /data/wwwroot/v2.50/wap.static/dist_dev;
                        root    $webp_root;
                        #if (!-f $request_filename) {
                        set $filename $webp_root$uri;
                        if (!-f $filename) {
                                set $request_filepath $webp_root$1;
                                content_by_lua_file "/usr/local/nginx/conf/lua/webp.lua";
                        }
                }
                location / {
                        proxy_set_header Host $host;
                        proxy_set_header X-Real-IP $remote_addr;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                }

                # 测试lua安装是否成功
                # location /hello {
                #       default_type 'text/plain';
                #       content_by_lua 'ngx.say("hello, lua")';
                #}

        #log file
         access_log  logs/m250_access.log access;
}

Django+Nginx+uWSGI部署

一、介绍

Django的部署有多种方式,采用Nginx+uWSGI的方式是最常见的一种方式。在这种方式中,将Nginx作为服务器前端,接收Web的所有请求,统一管理请求。Nginx自己处理所有静态请求,所有非静态请求通过uWSGI传递给Django,由Django来进行处理,从而完成一次Web请求。

二、安装配置

环境:Python(3.5.2)、Django(1.11.6)

1. 安装pip

root@787c084a44e4:~# apt-get install -y python3-pip 

2. 安装uWSGI

root@787c084a44e4:~# python3 -m pip install uWSGI 

3. 安装Django

root@787c084a44e4:~# python3 -m pip install Django 

4. 安装Nginx

root@787c084a44e4:~# apt-get install -y nginx 

5. 通过文件配置uwsgi

新建文件/root/web/uwsgi.ini

[uwsgi]
# 监听IP和端口
socket = 127.0.0.1:9090
# 项目主目录
chdir = /root/web/Django
# uwsgi文件路径
module = Django.wsgi:application
# 允许主进程存在
master = True
# 开启的进行数
workers = 1
# 服务器退出时自动清楚环境
vacuum = True
# 设置工作进程请求上限
max-requests = 1000
# 记录主进行的pid
pidfile = /var/uwsgi.pid
# 使进程在后台运行
daemonize = /var/uwsgi.log

6. 配置nginx虚拟主机

新建文件/etc/nginx/sites-

enabled/django.conf
# nginx需要连接的后端代理
upstream django {
    server 127.0.0.1:9090;
}

# 虚拟主机配置
server {
    # 监听端口
    listen 8080;
    # 域名
    server_name 127.0.0.1;
    # 编码
    charset utf-8;
    # 静态文件路径
    location /static {
        alias /root/web/Django/static;
    }
    # 将动态请求转发到uwsgi运行的django程序
    location / {
        uwsgi_pass django;
        include /etc/nginx/uwsgi_params;
    }
}

7. 修改nginx默认运行用户

修改/etc/nginx/nginx.conf文件,user www-data修改为user root

三、验证部署

运行工程

root@787c084a44e4:~/web# uwsgi --ini uwsgi.ini & nginx

访问地址http://127.0.0.1:8080/list,数据和页面css样式正常

未分类

四、异常情况

1. 出现DisabllowedHost

未分类

修改Django/Django/settings.py文件

ALLOWED_HOSTS = ["*"] 

2. 出现静态文件403异常

未分类

Nginx使用www-data用户运行,无web目录读取权限,修改/etc/nginx/nginx.conf文件,user www-data修改为user root

Python Flask 框架 终极部署教程,超详细。Uwsgi+Nginx+mysql+Supervisor+Virtualenv, 基于阿里云默认Linux

我发现网上还没完整详细版本的Flask 部署教程,而我在部署中遇到很多坑,所以在这里写下来,完整的教程以下是部署流程:

处理本地的代码

假设你已经完成了项目的开发,本地已经安装了git,那么首先将你的代码提交到git;

#进项目根目录
pip freeze > requirements.txt  #导flask 全部包,方便新环境下一次性安装。
git init # 之前如果没有做,那么需要做
git add --all #提交所有修改后的文件
git remote add origin http:xxxx.git  #这一步如果之前没做,那么你需要做。
git commmit -w 'first commit'

安装Mysql

现在进入服务器正式开始搭建环境,首先安装mysql.

新鲜出炉的阿里云需要更新下apt.
apt-get update
然后安装mysql, 注意一定记住root 密码,还要在配置文件中设置字符为UTF8!
增加新的数据库,注意名字和你项目的一样,
create database xxx;

安装虚拟环境 Vitualenv

pip install virtualenvwrapper #直接安装虚拟环境容器来顺便安装virtualenv
注意需要先修改环境变量。否则命名无法使用
vim ~/.bashrc   在最底部增加
#这是为了让 WORKON 命令能用。
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

然后你需要用 mkvirtualenv xxx   来创建一个叫xxx 的python 虚拟环境
workon xxx #切换到你的虚拟环境
在这个环境里,你可以升级你的python 到python3, 当然根据你的需求。

安装 Git 拿到全部源码

现在在服务器上安装git 拿到源码
apt install git
git init
git remote add origin http:xxx.git
git pull origin master # 拿到全部源码

正式部署源码

进去项目根目录:
pip install -r requirements.txt  #导入你项目的flask 依赖
pip manager.py db migerate #初始化数据库  
# 这里如果出现初始化失败,那么清空你的 migration 文件夹,你会丢掉你的测试数据,当然有不丢的办法
pip manager.py db upgrade #导入数据库,代码迁移完成

安装Nginx

apt install nginx #安装nginx 
贴下配置文件位于: /etc/nginx/conf.d/xxx.conf
# 配置nginx与uwsgi的通信方式和名称,mysite就是名称
upstream xxx {
    # nginx使用socket的方式与uwsgi进行通信
    # 这里指向你项目的目录下的socket文件,
    # 这个socket文件不需要单独创建,在运行的时候会自动创建。
    server unix:///srv/xxx/xxx.sock;
}
# 配置服务器
server {
    # 监听的端口号
    listen 80;
    # 域名或者本机的ip地址
    server_name dev.wudizu.com 47.104.22.138;
    charset     utf-8;
    # 最大的上传大小
    client_max_body_size 75M;  
    # adjust to taste

    # 访问根目录下的任何url的配置
    location / {
        # 指定访问哪个upstream
        uwsgi_pass wudi;
        # 包括uwsgi参数的文件
        include     /etc/nginx/uwsgi_params;
    }
    location /static {
        alias /srv/xxx/static;
   }

}

安装Uwsgi

pip install uwsgi

贴下配置文件:


[uwsgi]
chdir           = /srv/xxx
# 模块
module          = firstweb   #注意这里一定要你写的flask 首页文件名
# python的虚拟环境
home            = /root/.virtualenvs/flask-py2

# 是否启用mater模式
master          = true

# 进程数
processes       = 10

# socket文件地址

socket          = /srv/xxx/xxx.sock

# wsgi文件

wsgi-file       = /srv/xxx/xxx.ini

# wsgi文件中的app变量

callable        = app

# socket文件的权限

chmod-socket    = 666

安装Supervisor

sudo pip install supervisor
# supervisor的程序名字
[program:mysite]
# supervisor执行的命令
command=uwsgi --ini mysite_uwsgi.ini
# 项目的目录
directory = /srv/xxx 
# 开始的时候等待多少秒
startsecs=0
# 停止的时候等待多少秒
stopwaitsecs=0# 自动开始
autostart=true
# 程序挂了后自动重启
autorestart=true
# 输出的log文件
stdout_logfile=/srv/xxx/log/supervisord.log
# 输出的错误文件
stderr_logfile=/srv/xxx/log/supervisord.err

[supervisord]
# log的级别
loglevel=info

# 使用supervisorctl的配置
[supervisorctl]
# 使用supervisorctl登录的地址和端口号
serverurl = http://127.0.0.1:9001

# 登录supervisorctl的用户名和密码
username = admin
password = 123

[inet_http_server]
# supervisor的服务器
port = :9001
# 用户名和密码
username = admin
password = 123

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface


然后使用supervisor运行uwsgi:
supervisord -c supervisor.conf

使用supervisorctl管理supervisord:supervisor是一个C/S模型,跟redis一样,有一个服务器,也有一个客户端命令用来管理服务的,使用以下命令进入到命令界面:
supervisorctl -c supervisor.conf

指定的文件必须和supervisord服务端保持一致。 一些常用的命令有:
supervisorctl -c supervisor.conf
> status    # 查看状态
> start program_name # 启动程序
> restart program_name # 重新启动程序
> stop program_name # 停止程序
> reload # 重新加载配置文件
> quit # 退出当前的客户端

ansible 角色定义及调用(nginx)

Roles的介绍

Roles是ansible自1.2版本引入的新特性,用于层次性,结构化地组织playbook,roles能够根据层次型结构自动自动装在变量文件、tasks以及handlers等。

创建roles的步骤

  • 创建以roles命名的目录:

  • 在roles目录中分别创建以各角色名称命名的目录,如webservers等:

  • 在每个角色命名的目录中分别创建files、handlers、meta、tasks、templates和vars目录:用不到的目录可以创建为空目录,也可以不创建

  • 在playbook文件中,调用各角色

roles内各目录中可用的文件

  • tasks目录:至少创建一个名为main.yml的文件,其定义了此角色的任务列表:此文件可以使用include包含其他的位于此目录中的tasks文件

  • files目录:存放由copy或者script等模块调用的文件

  • templates目录:templates模块会自动在此目录中寻找模板文件

  • handlers目录:此目录中应当包含一个main

  • yml文件:用于定义此角色用到的各handler:在handler中使用include包含的其他的handler文件也应该位于此目录中

  • vars目录:应当包含一个main.yml文件,用于定义此角色用到的变量

  • meta目录:应当包含一个main.yml文件,用于定义此角色的特殊设定及其依赖关系:ansible 1.3及其以后的版本才支持

  • default目录:为当前角色定义默认变量时使用此目录,应该包含一个main.yml文件

实验环境

ansible:10.0.0.128
client :10.0.0.131

执行

1. 在服务器生成免密钥文件,推送到客户端

[root@ansible ~]# ssh-keygen 
[root@ansible ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]

2. 安装ansible

[root@ansible ~]# yum install -y ansible

3. 到/etc/ansible 有个可以自定义roles的目录

[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# ls
ansible.cfg  hosts  nginx.yaml  roles

4. 定义要执行的角色路径

[root@ansible ~]# cat /etc/ansible/nginx.yaml 
- hosts: 10.0.0.131
  remote_user: root
  roles:
  - nginx

5. 定义掩码安装nginx,在roles目录下的目录及文件都要自己创建

[root@ansible roles]# ls
nginx
[root@ansible roles]# cd nginx
[root@ansible nginx]# ls
files  handlers  tasks  templates  vars
[root@ansible ansible]# cd roles/
[root@ansible roles]# tree
.
└── nginx
    ├── files
    │   └── nginx-1.12.0.tar.gz
    ├── handlers
    │   └── main.yml
    ├── tasks
    │   └── main.yml
    ├── templates
    │   └── nginx.conf
    └── vars
        └── main.yml

6 directories, 5 files

6. 进入tasks目录创建任务

[root@ansible nginx]# cat tasks/main.yml 
- name: copy nginx packup to remote host
 copy: src=nginx-1.12.0.tar.gz dest=/usr/local/src/nginx-1.12.0.tar.gz
 tags: cppkg
- name: tar nginx
 shell: cd /usr/local/src/; tar -xf nginx-1.12.0.tar.gz
- name: install packger
 yum: name={{ item }} state=latest
 with_items:
   - openssl-devel
   - pcre-devel
   - gcc
- name: useradd
 shell: useradd nginx -s /sbin/nologin
- name: install nginx
 shell: cd /usr/local/src/nginx-1.12.0;./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre;make && make install
- name: copy conf file nginx.conf
 template: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf
 notify: start nginx

7. 存放nginx压缩包目录

[root@ansible nginx]# ls files/
nginx-1.12.0.tar.gz    ##对应tasks第二行

8. template这一行对应的是template这个目录和主服务端定义的变量

[root@ansible nginx]# cat templates/nginx.conf 
#user  nobody;
worker_processes  
{{ ansible_processor_vcpus }};
#pid        logs/nginx.pid;
events {
    worker_connections  65532;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    server {

     listen       {{ ngxport }};
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   /web;
            index  index.html index.htm;
        }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        #location ~ .php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
    }
    include vhosts/*.conf;
}

9. 查看我们定义的变量,在vars目录下

[root@ansible nginx]# cat vars/main.yml 
ngxport: "8080"

10. 编辑触发器

[root@ansible nginx]# cat handlers/main.yml 
- name: start nginx     
  shell: /usr/local/nginx/sbin/nginx

11. 开始执行

[root@ansible nginx]# ansible-playbook -C /etc/ansible/nginx.yaml(测试)
[root@ansible nginx]# ansible-playbook  /etc/ansible/nginx.yaml 

PLAY [10.0.0.131] ************************************************************* 

GATHERING FACTS *************************************************************** 
ok: [10.0.0.131]

TASK: [nginx | copy nginx packup to remote host] ****************************** 
changed: [10.0.0.131]

TASK: [nginx | tar nginx] ***************************************************** 
changed: [10.0.0.131]

TASK: [nginx | install packger] *********************************************** 
ok: [10.0.0.131] => (item=openssl-devel,pcre-devel,gcc)

TASK: [nginx | useradd] ******************************************************* 
changed: [10.0.0.131]

TASK: [nginx | install nginx] ************************************************* 
changed: [10.0.0.131]

TASK: [nginx | copy conf file nginx.conf] ************************************* 
changed: [10.0.0.131]

NOTIFIED: [nginx | start nginx] *********************************************** 
changed: [10.0.0.131]

PLAY RECAP ******************************************************************** 
10.0.0.131                 : ok=8    changed=6    unreachable=0    failed=0   

12. 查看client客户端nginx服务已经启动

[root@zxb4 ~]# ps -ef |grep nginx
root      34655      1  0 02:13 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     34656  34655  1 02:13 ?        00:00:01 nginx: worker process
root      34660  28130  0 02:16 pts/1    00:00:00 grep --color=auto nginx
[root@zxb4 ~]# netstat -tulnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      34655/nginx: master 

附加

假如我们经常要增加nginx站点,直接写好模板推送到vhos目录:

[root@ansible templates]# cat temp_server.conf
server
{
listen80;
server_name{{ server_name }};
indexindex.php index.html;
root {{root_dir }};
}

在vars定义变量:

[root@ansible vars]# cat main.yml
ngxport:"8080"
server_name:"www.xxx.com"
root_dir:"/web"

重写tasks步骤:

[root@ansible tasks]# cat main.yml
- name:copy conf file nginx.conf          # 调用templates模块
  template: src=temp_server.conf dest=/usr/local/nginx/conf/vhosts/{{server_name }}.conf
  tags: ngxconf
  notify: reload nginx service                # 调用handlers模块

Ansible使用playbook自动化编译安装Nginx

Ansible批量部署编译安装nginx

一、Ansible介绍

这次为大家介绍一款批量部署工具Ansible,主要有以下几点优点:1、充分利用现有设施。使用 Ansible 无需安装服务端和客户端,只要 SSH 即可。这意味着,任何一台装有 Ansible 的机器都可以成为强大的管理端。我觉得,这种去中心化的思路显得更为灵活。可能有人会担心 SSH 的效率,Ansible 的并行执行及加速模式或许可以打消你的顾虑。2、使用简单,快速上手相当容易。Ansible 上手十分快,用 Ad-Hoc 可以应付简单的管理任务,麻烦点的也可以定义 Playbook 文件来搞定。3、采用人类易读的格式。Ansible 的主机定义文件使用 INI 格式,支持分组,能够指定模式;此外也能动态生成,这对管理云主机应当很有用。而 Playbook 则是 YAML 格式。4、能够使用你熟悉的语言来编写模块。虽然 Ansible 是使用 Python 开发的,但它不会将你限制到某种具体的编程语言,Bash、Python、Perl、Ruby 等等都可以,你擅长什么就用什么。

一言以蔽之,Ansible 背后的简单化哲学深得我心。这也比较符合我选择软件的一贯原则。可能还有人会比较关心目前 Ansible 都有谁在用。毕竟,榜样的力量是无穷。Puppet 不正是因为 Google 在用而吸引了不少眼球么?据我所知,当前使用 Ansible 较为知名的用户包括 Fedora、Rackspace、Evernote 等等。

Ansible企业应用:

未分类

二、主要架构功能

Ansible Core.    //核心功能

Modules:

Core Modules    //核心功能

Customed Modules  //自定义模块

Host Inventory        //主机库和主机清单,用来定义要管理的主机

File

CMDB(配置管理数据)

PlayBooks            //剧本,定义没个主机扮演的角色

Hosts.        //主机

roles.        //角色

Connection Plugins.  //连接插件,连接至被管控主机,完成并发连接,默认一次管理5台,但是可以修改。

三、安装配置

安装:

# yum install ansible -y  (epel仓库中)

程序:

ansible

ansible-playbook  //唱剧本

ansible-doc        //获取帮助文档

配置文件

/etc/ansible/ansible.cfg    //核心配置文件

主机清单:

/etc/ansible/hosts

插件目录:

/usr/share/ansible_plugins/

1、设置ansble到各个主机的免密钥通讯:

[root@cml1~]# ssh-keygen
[root@cml1~]# ssh-copy-id 192.168.5.102

2、定义主机组:

[root@cml1~]# cd /etc/ansible/
[root@cml1ansible]# vim hosts 
[webserver]
192.168.5.102
192.168.5.104

3、查看主机组内的主机:

[root@cml1ansible]# ansible webserver --list-hosts 
  hosts (2):
    192.168.5.102
    192.168.5.104

4、定义角色路径

[root@cml1~]# cat /etc/ansible/nginx.yaml 
- hosts: 192.168.5.102
 remote_user: root
  roles:
  -nginx_install    ###roles目录下的nginx_install目录
  -nginx_config    ###roles目录下的nginx_config目录

5、查看目录结构:

[root@cml1 roles]# tree
.
├── nginx_config
│  ├── default
│  ├── files
│  ├── handlers
│  │  └── main.yml
│  ├── meta
│  ├── tasks
│  │  └── main.yml
│  ├── templates
│  │  └── temp_server.conf
│  └── vars
│      └── main.yml
└── nginx_install
    ├──default
    ├──files
  │  └── nginx-1.12.0.tar.gz
    ├──handlers
  │  └── main.yml
    ├──meta
    ├──tasks
  │  └── main.yml
    ├──templates
  │  └── nginx.conf
└── vars
[root@cml1roles]# cd nginx_install/
[root@cml1nginx_install]# ls
default  files handlers  meta  tasks templates  vars

6、task定义开始任务:

[root@cml1nginx_install]# cd tasks/
[root@cml1tasks]# cat main.yml 
- name: copynginx package to remote host  
  copy: src=nginx-1.12.0.tar.gz  dest=/tmp/nginx-1.12.0.tar.gz  ##拉取nginx解压吧
  tags: cppkg
- name: tarnginx
  shell: cd /tmp;tar -xf nginx-1.12.0.tar.gz  ##解压nginx包
- name: installpakger
  yum: name={{ item }} state=latest    ##安装依赖包
  with_items:
    - openssl-devel
    - pcre-devel
    - gcc
- name: installnginx
  shell: cd /tmp/nginx-1.12.0;./configure--user=nginx --group=nginx --prefix=/usr/local/nginx--with-http_stub_status_module --with-http_ssl_module --with-pcre;make&& make install      ####编译安装
- name: copyconf file nginx.conf          
  template: src=nginx.confdest=/usr/local/nginx/conf/nginx.conf  ###复制在template目录下的配置文件
  tags: ngxconf
- name: copyshell
  copy: src=/opt/create_users.shdest=/tmp/create_users.sh  ##拉取创建用户的shell脚本
- name: createuser nginx
  shell: /bin/bash /tmp/create_users.sh
  tags: addnginx
  notify: start nginx service

为什么要写这个脚本?因为加入有些主机创建的用户已存在就会报错

[root@cml1tasks]# cat /opt/create_users.sh 
#!/bin/bash
a=`cat/etc/passwd | grep nginx | wc -l`
if [ $a == 0];then
      useradd nginx
fi

6、第二行copy对应file目录:

[root@cml1nginx_install]# cd files/
[root@cml1files]# ls
nginx-1.12.0.tar.gz

7、template这一行对应的是template这个目录和主服务端定义的变量:

[root@cml1nginx_install]# cd templates/
[root@cml1templates]# ls
nginx.conf
[root@cml1templates]# cat nginx.conf 

user  nginx;
worker_processes  {{ ansible_processor_vcpus }};

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  65535;
}


http {


    include      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"';
  log_format xiaoluo  '$remote_addr -$remote_user  [$time_local]  '
                            '"$request"  $status$body_bytes_sent '
                            '"$http_referer" "$http_user_agent" ';
    #access_log logs/access.log  main;

    sendfile        on;
    #tcp_nopush    on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip on;

    server {
        listen      {{ ngxport }};  
        server_name  wwwNaNl.com
        access_log logs/wwwNaNl.com  xiaoluo;
        #location / {
        #  proxy_pass http://192.168.5.101;
        #}

        #error_page  404              /404.html;

        # redirect server error pages to thestatic page /50x.html
        #
        error_page  500 502 503 504  /50x.html;
        location = /50x.html {
            root  html;
        }

        # proxy the PHP scripts to Apachelistening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #  proxy_pass  http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGIserver listening on 127.0.0.1:9000
        #
        location ~ .php$ {
            root          /web;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME$document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, ifApache's document root
        # concurs with nginx's one
        #
        #location ~ /.ht {
        #  deny  all;
        #}
    }
  include vhosts/*.conf;
}##需要注意的就是模板变量(客户端自动采集)、和在服务端定义的变量{{ngx_port}}

8、在vars定义变量:

[root@cml1nginx_install]# cd vars/
[root@cml1vars]# cat main.yml 
ngxport:"8080"

9、定义触发器:

[root@cml1nginx_install]# cd handlers/
[root@cml1handlers]# cat main.yml 
- name: startnginx service 
  shell: /usr/loal/nginx/sbin/nginx

10、在nginx_config目录加入我们经常要增加nginx站点,直接写好模板推送到vhos目录:

[root@cml1roles]# cd nginx_config/
[root@cml1nginx_config]# ls
default  files handlers  meta  tasks templates  vars
[root@cml1nginx_config]# cd templates/
[root@cml1templates]# ls
temp_server.conf
[root@cml1templates]# cat temp_server.conf 
server 
{ 
listen 80; 
server_name {{server_name }}; 
index index.phpindex.html; 
root {{root_dir }}; 
}

###在var定义变量:
[root@cml1templates]# cd ../vars/
[root@cml1vars]# cat main.yml 
server_name: "www.xiaoluo.com"
root_dir:"/web"

11、写配置nginx的tasks步骤:

[root@cml1nginx_config]# cd tasks/
[root@cml1tasks]# ls
main.yml
[root@cml1tasks]# cat main.yml 
- name: createvhosts
  shell: mkdir -p /usr/local/nginx/conf/vhosts/
  tags: create_dir
- name: copyconf file nginx.conf          # 调用templates模块
  template: src=temp_server.confdest=/usr/local/nginx/conf/vhosts/{{ server_name }}.conf
  tags: ngxconf
  notify: reload nginx service 
###定义重启触发器:
[root@cml1tasks]# cd ../handlers/
You have newmail in /var/spool/mail/root
[root@cml1handlers]# cat main.yml 
- name: reloadnginx service
  shell: /usr/local/nginx/sbin/nginx-t;/usr/local/nginx/sbin/nginx -s reload

测试:

[root@cml1ansible]# ansible-playbook -C nginx.yaml 

PLAY[192.168.5.104] ********************************************************** 

GATHERING FACTS*************************************************************** 
ok:[192.168.5.104]

TASK:[nginx_install | copy nginx package to remote host] ********************* 
changed:[192.168.5.104]

TASK:[nginx_install | tar nginx] ********************************************* 
skipping:[192.168.5.104]
ok:[192.168.5.104]

TASK:[nginx_install | install pakger] **************************************** 
changed: [192.168.5.104]=> (item=openssl-devel,pcre-devel,gcc)

TASK:[nginx_install | install nginx] ***************************************** 
skipping:[192.168.5.104]
ok:[192.168.5.104]

TASK:[nginx_install | copy conf file nginx.conf] ***************************** 
changed:[192.168.5.104]

TASK:[nginx_install | copy shell] ******************************************** 
changed:[192.168.5.104]

TASK:[nginx_install | create user nginx] ************************************* 
skipping:[192.168.5.104]
ok: [192.168.5.104]

TASK:[nginx_config | create vhosts] ****************************************** 
skipping:[192.168.5.104]
ok:[192.168.5.104]

TASK:[nginx_config | copy conf file nginx.conf] ****************************** 
changed:[192.168.5.104]

NOTIFIED:[nginx_config | reload nginx service] ******************************* 
skipping:[192.168.5.104]
ok:[192.168.5.104]

PLAY RECAP******************************************************************** 
192.168.5.104              : ok=6    changed=5  unreachable=0    failed=0[root@cml1 ansible]# ansible-playbook  nginx.yaml 
PLAY [192.168.5.104] ********************************************************** 
GATHERING FACTS *************************************************************** 
ok: [192.168.5.104]
TASK: [nginx_install | copy nginx package to remote host] ********************* 
ok: [192.168.5.104]
TASK: [nginx_install | tar nginx] ********************************************* 
changed: [192.168.5.104]
TASK: [nginx_install | install pakger] **************************************** 
ok: [192.168.5.104] => (item=openssl-devel,pcre-devel,gcc)
TASK: [nginx_install | install nginx] ***************************************** 
changed: [192.168.5.104]
TASK: [nginx_install | copy conf file nginx.conf] ***************************** 
ok: [192.168.5.104]
TASK: [nginx_install | copy shell] ******************************************** 
ok: [192.168.5.104]
TASK: [nginx_install | create user nginx] ************************************* 
changed: [192.168.5.104]
TASK: [nginx_config | create vhosts] ****************************************** 
changed: [192.168.5.104]
TASK: [nginx_config | copy conf file nginx.conf] ****************************** 
ok: [192.168.5.104]
NOTIFIED: [nginx_install | start nginx service] ******************************* 
changed: [192.168.5.104]
PLAY RECAP ******************************************************************** 
192.168.5.104              : ok=11  changed=5    unreachable=0    failed=0[root@cml3 ~]# ifconfig
ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.5.104  netmask 255.255.255.0  broadcast 192.168.5.255


[root@cml3 ~]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address          Foreign Address        State      PID/Program name              
tcp        0      0 0.0.0.0:80              0.0.0.0:*              LISTEN      29264/nginx: master 
tcp        0      0 0.0.0.0:8080            0.0.0.0:*              LISTEN      29264/nginx: master 

四、定义日志文件

下面是介绍如何定义日志:(在ansible1.9.1版本之后有个bug所以定义不了日志文件只能降版本到1.9.1了)

1、ansible倒回去版本:1.9.1

需要安装gcc、python-devel

[root@cml1 ~]#yum install python-pip
[root@cml1 ~]#pip install ansible==1.9.1

2、callback插件:

[root@cml1tasks]# vim /etc/ansible/ansible.cfg
callback_plugins  = /usr/share/ansible/plugins/callback
bin_ansible_callbacks= True

3、在callback目录下创建日志处理文件:

[root@cml1tasks]# cd /usr/share/ansible/plugins/callback
[root@cml1callback]# ls
log.py  log.pyc
[root@cml1callback]# cat log.py
import os
import time
import json

TIME_FORMAT="%b%d %Y %H:%M:%S"
MSG_FORMAT="%(now)s- %(category)s - %(data)snn"

if notos.path.exists("/var/log/ansible/hosts"):
  os.makedirs("/var/log/ansible/hosts")

def log(host,category, data):
    if type(data) == dict:
        if 'verbose_override' in data:
            # avoid logging extraneous datafrom facts
            data = 'omitted'
        else:
            data = data.copy()
            invocation = data.pop('invocation',None)
            data = json.dumps(data)
            if invocation is not None:
                data = json.dumps(invocation) +" => %s " % data

    path =os.path.join("/var/log/ansible/hosts", host)
    now = time.strftime(TIME_FORMAT,time.localtime())
    fd =open(path, "a")
    fd.write(MSG_FORMAT % dict(now=now,category=category, data=data))
    fd.close()

classCallbackModule(object):
    """
    logs playbook results, per host, in/var/log/ansible/hosts
    """

    def on_any(self, *args, **kwargs):
        pass

    def runner_on_failed(self, host, res,ignore_errors=False):
        log(host, 'FAILED', res)

    def runner_on_ok(self, host, res):
        log(host, 'OK', res)

    def runner_on_skipped(self, host,item=None):
        log(host, 'SKIPPED', '...')

    def runner_on_unreachable(self, host, res):
        log(host, 'UNREACHABLE', res)

    def runner_on_no_hosts(self):
        pass

    def runner_on_async_poll(self, host, res,jid, clock):
        pass

    def runner_on_async_ok(self, host, res,jid):
        pass

    def runner_on_async_failed(self, host, res,jid):
        log(host, 'ASYNC_FAILED', res)

    def playbook_on_start(self):
        pass

    def playbook_on_notify(self, host,handler):
        pass

    def playbook_on_no_hosts_matched(self):
        pass

    def playbook_on_no_hosts_remaining(self):
        pass

    def playbook_on_task_start(self, name,is_conditional):
        pass

    def playbook_on_vars_prompt(self, varname,private=True, prompt=None, encrypt=None, confirm=False, salt_size=None,salt=None, default=None):
        pass

    def playbook_on_setup(self):
        pass

    def playbook_on_import_for_host(self, host,imported_file):
        log(host, 'IMPORTED', imported_file)

    def playbook_on_not_import_for_host(self,host, missing_file):
        log(host, 'NOTIMPORTED', missing_file)

    def playbook_on_play_start(self, name):
        pass

    def playbook_on_stats(self, stats):
        pass