• Nginx插件之openresty反向代理和日志滚动配置案例


               Nginx插件之openresty反向代理和日志滚动配置案例

                                        作者:尹正杰

    版权声明:原创作品,谢绝转载!否则将追究法律责任。

    一.openresty介绍

    1>.Nginx介绍

      Nginx是一款高性能web服务器和反向代理服务器,在互联网项目中使用非常频繁,尤其其出色的性能以及轻量级进程占用,已经超过了apache的httpd服务器的使用量。内部可以配置零拷贝(sendfile)实现快速文件传输。Nginx可以做反向代理,负载均衡,HTTP服务器(包含动静分离)以及正向代理。

    2>.Openresty介绍

       openresty是将nginx现有一些重要插件做了集成,省去安装nginx之后还需要在安装插件的繁琐步骤,内置luajit插件,能解决接受post提交请求、json消息体解析等功能。

    二.Windows环境安装openresty

    1>.下载openresty安装包

      下载地址:http://openresty.org/cn/download.html

    2>.安装openresty

      安装方式很简单,解压即可。

    3>.修改Windows版本的nginx.conf的配置文件内容

    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        log_format  main  escape=json $remote_addr#$http_client_time#$time_local#$status#$request_body;
        access_log  logs/access.log  main;
        sendfile        on;
        keepalive_timeout  65;
        upstream servers{
            server s101:80 weight=1;
            server s102:80 weight=1;
        }
        map $http_x_forwarded_for  $clientRealIp {
            ~^(?P<firstAddr>[0-9.]+),?.*$    $firstAddr;
        }
        server {
            listen       80;
            server_name  localhost;
            underscores_in_headers on;
            location / {
                error_page 405 =200 $1;
                lua_need_request_body on;                                                                                            
                content_by_lua 'local s = ngx.var.request_body';
                proxy_pass http://servers;
                proxy_set_header Host $host;
                proxy_set_header remove_user_ip $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
            root   html;
            }
        }
    }

    4>.启动openresty

    三.Linux环境安装openresty

      为了配置起来方便,我推荐大家使用root权限,因为使用普通用户做啥都不方便,各种需要提权操作(sudo)。如果实际生产环境中你没法拿到root权限的话,那就还是乖乖用普通用户执行吧,配置起来也都大同小异。

    1>.在s101上使用root用户手动添加yum源并修改yum的配置文件

    [yinzhengjie@s101 ~]$ su root
    Password: 
    [root@s101 yinzhengjie]#
    [root@s101 yinzhengjie]# vi /etc/yum.repos.d/openresty.repo
    [root@s101 yinzhengjie]# cat /etc/yum.repos.d/openresty.repo 
    [openresty]
    name=Official OpenResty Open Source Repository for CentOS
    baseurl=https://openresty.org/package/centos/$releasever/$basearch
    skip_if_unavailable=False
    gpgcheck=1
    repo_gpgcheck=1
    gpgkey=https://openresty.org/package/pubkey.gpg
    enabled=1
    enabled_metadata=1
    [root@s101 yinzhengjie]# 
    [root@s101 yinzhengjie]# grep keepcache /etc/yum.conf 
    keepcache=0
    [root@s101 yinzhengjie]# sed -i 's/keepcache=0/keepcache=1/' /etc/yum.conf             #会将下载的文件保存
    [root@s101 yinzhengjie]# 
    [root@s101 yinzhengjie]# grep keepcache /etc/yum.conf 
    keepcache=1
    [root@s101 yinzhengjie]# 

    2>.在s101上通过yum搜索openresty安装包

    [root@s101 yinzhengjie]# yum -y search cache openresty
    Loaded plugins: fastestmirror
    base                                                                                                                                 | 3.6 kB  00:00:00     
    epel/x86_64/metalink                                                                                                                 | 7.6 kB  00:00:00     
    epel                                                                                                                                 | 3.2 kB  00:00:00     
    extras                                                                                                                               | 3.4 kB  00:00:00     
    openresty/7/x86_64/signature                                                                                                         |  490 B  00:00:00     
    Retrieving key from https://openresty.org/package/pubkey.gpg
    Importing GPG key 0xD5EDEB74:
     Userid     : "OpenResty Admin <admin@openresty.com>"
     Fingerprint: e522 18e7 0878 97dc 6dea 6d6d 97db 7443 d5ed eb74
     From       : https://openresty.org/package/pubkey.gpg
    openresty/7/x86_64/signature                                                                                                         | 2.9 kB  00:00:00 !!! 
    updates                                                                                                                              | 3.4 kB  00:00:00     
    (1/5): extras/7/x86_64/primary_db                                                                                                    | 173 kB  00:00:03     
    (2/5): openresty/7/x86_64/primary_db                                                                                                 |  57 kB  00:00:07     
    (3/5): updates/7/x86_64/primary_db                                                                                                   | 4.3 MB  00:00:21     
    (4/5): epel/x86_64/updateinfo                                                                                                        | 932 kB  00:00:22     
    (5/5): epel/x86_64/primary                                                                                                           | 3.6 MB  00:00:33     
    Determining fastest mirrors
     * base: mirrors.huaweicloud.com
     * epel: mirrors.tuna.tsinghua.edu.cn
     * extras: mirror.bit.edu.cn
     * updates: mirrors.huaweicloud.com
    epel                                                                                                                                            12629/12629
    ==================================================================== N/S matched: cache ====================================================================
    cachefilesd.x86_64 : CacheFiles user-space management daemon
    ccache.x86_64 : C/C++ compiler cache
    collectd-memcachec.x86_64 : Memcachec plugin for collectd
    collectd-rrdcached.x86_64 : RRDCacheD plugin for collectd
    dmlite-plugins-memcache.x86_64 : Memcached plugin for dmlite
    ghc-date-cache.x86_64 : Date cacher
    ghc-date-cache-devel.x86_64 : Haskell date-cache library development files
    ghc-lrucache.x86_64 : A simple, pure LRU cache
    ghc-lrucache-devel.x86_64 : Haskell lrucache library development files
    globus-gass-cache.x86_64 : Globus Toolkit - Globus Gass Cache
    globus-gass-cache-devel.x86_64 : Globus Toolkit - Globus Gass Cache Development Files
    globus-gass-cache-doc.noarch : Globus Toolkit - Globus Gass Cache Documentaton Files
    globus-gass-cache-program.x86_64 : Globus Toolkit - Tools to manipulate local and remote GASS caches
    golang-github-patrickmn-go-cache-devel.noarch : An in-memory key:value store/cache library for Go (Golang)
    golang-github-patrickmn-go-cache-unit-test-devel.x86_64 : Unit tests for golang-github-patrickmn-go-cache package
    kdesdk-kcachegrind.x86_64 : a browser for data provided by profiling tools (like cachegrind)
    libmemcached.i686 : Client library and command line tools for memcached server
    libmemcached.x86_64 : Client library and command line tools for memcached server
    libmemcached-devel.i686 : Header files and development libraries for libmemcached
    libmemcached-devel.x86_64 : Header files and development libraries for libmemcached
    memcached.x86_64 : High Performance, Distributed Memory Object Cache
    memcached-devel.i686 : Files needed for development using memcached protocol
    memcached-devel.x86_64 : Files needed for development using memcached protocol
    menu-cache-devel.x86_64 : Development files for menu-cache
    nodejs-async-cache.noarch : Cache your async lookups and don't fetch the same thing more than necessary
    nodejs-lru-cache.noarch : A least recently used cache object for Node.js
    nodejs-require-uncached.noarch : Require a module bypassing the cache
    nordugrid-arc-acix-cache.noarch : ARC cache index - cache server
    nordugrid-arc-cache-service.x86_64 : ARC cache service
    opensips-memcached.x86_64 : Memcached connector
    pcp-pmda-memcache.x86_64 : Performance Co-Pilot (PCP) metrics for Memcached
    perl-Cache-Cache.noarch : Generic cache interface and implementations
    perl-Cache-FastMmap.x86_64 : Uses an mmap'ed file to act as a shared memory interprocess cache
    perl-Cache-Memcached.noarch : Perl client for memcached
    perl-HTTP-Cache-Transparent.noarch : Cache the result of http get-requests persistently
    perl-IPC-SharedCache.noarch : Perl module to manage a cache in SysV IPC shared memory
    perl-Tie-Cache.noarch : LRU Cache in Memory
    php-ZendFramework-Cache-Backend-Apc.noarch : Zend Framework APC cache backend
    php-ZendFramework-Cache-Backend-Libmemcached.noarch : Zend Framework libmemcache cache backend
    php-ZendFramework-Cache-Backend-Memcached.noarch : Zend Framework memcache cache backend
    php-ZendFramework2-Cache.noarch : Zend Framework 2: Cache Component
    php-doctrine-cache.noarch : Doctrine Cache
    php-doctrine-doctrine-cache-bundle.noarch : Symfony2 Bundle for Doctrine Cache
    php-ezc-Cache.noarch : Provides a collection of lightweight classes to cache different kinds of data
    php-horde-Horde-Memcache.noarch : Horde Memcache API
    php-pear-Cache-Lite.noarch : Fast and Safe little cache system for PHP
    php-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemon
    php-pecl-memcached.x86_64 : Extension to work with the Memcached caching daemon
    php-pecl-zendopcache.x86_64 : The Zend OPcache
    php-psr-cache.noarch : PSR Cache: Common interface for caching libraries
    php-xcache.x86_64 : Fast, stable PHP opcode cacher
    python-fastcache.x86_64 : C implementation of python3 lru_cache
    python-flask-cache.noarch : Adds cache support to your Flask application
    python-flask-cache-doc.noarch : Documentation for the Flask-Cache module
    python-linecache2.noarch : Backport of the linecache module
    python-memcached.noarch : A Python memcached client library
    python2-backports-functools_lru_cache.noarch : A backport of functools.lru_cache from Python 3.3 as published at ActiveState
    python2-cached_property.noarch : A cached-property for decorating methods in Python classes.
    python34-cached_property.noarch : A cached-property for decorating methods in Python classes.
    root-io-dcache.x86_64 : dCache input/output library for ROOT
    rubygem-memcache-client.noarch : A Ruby library for accessing memcached
    rubygem-rack-cache-doc.noarch : Documentation for rubygem-rack-cache
    statscache-common.noarch : Statscache common library
    statscache-consumer.noarch : Statscache consumer
    statscache-plugins.noarch : Plugins for statscache daemon
    statscache-web.noarch : A REST API server on top of statscache
    uwsgi-plugin-cache.x86_64 : uWSGI - Plugin for cache support
    uwsgi-router-cache.x86_64 : uWSGI - Plugin for Cache router support
    uwsgi-router-memcached.x86_64 : uWSGI - Plugin for Memcached router support
    xcache-admin.noarch : XCache Administration
    appstream-data.noarch : Cached AppStream metadata
    apt-cacher-ng.x86_64 : Caching proxy for package files from Debian
    dcap.x86_64 : Client Tools for dCache
    dcap-devel.x86_64 : Client Development Files for dCache
    dcap-libs.x86_64 : Client Libraries for dCache
    dcap-tunnel-gsi.x86_64 : GSI tunnel for dCache
    dcap-tunnel-krb.x86_64 : Kerberos tunnel for dCache
    dcap-tunnel-ssl.x86_64 : SSL tunnel for dCache
    dcap-tunnel-telnet.x86_64 : Telnet tunnel for dCache
    gtk-update-icon-cache.x86_64 : Icon theme caching utility
    libmediaart.i686 : Library for managing media art caches
    libmediaart.x86_64 : Library for managing media art caches
    menu-cache.x86_64 : Caching mechanism for freedesktop.org compliant menus
    nordugrid-arc-acix-core.noarch : ARC cache index - core
    nordugrid-arc-acix-index.noarch : ARC cache index - index server
    pcp-pmda-dm.x86_64 : Performance Co-Pilot (PCP) metrics for the Device Mapper Cache and Thin Client
    perl-CHI.noarch : Unified cache handling interface
    php-horde-Horde-Cache.noarch : Horde Caching API
    php-pear-Cache.noarch : Framework for caching of arbitrary data
    php-pecl-apcu.x86_64 : APC User Cache
    php-pecl-yac.x86_64 : Lockless user data cache
    php-psr-simple-cache.noarch : Common interfaces for simple caching (PSR-16)
    php-react-cache.noarch : Async caching
    python-cachetools.noarch : Extensible memoizing collections and decorators
    python-django-compressor.noarch : Compresses linked and inline JavaScript or CSS into single cached files
    python-dogpile-cache.noarch : A caching front-end based on the Dogpile lock
    python-pytest-cache.noarch : Pytest plugin with mechanisms for caching across test runs
    python-repoze-lru.noarch : A tiny LRU cache implementation and decorator
    python2-cccolutils.x86_64 : Python Kerberos Credential Cache Collection Utilities
    python2-django-redis.noarch : Full featured redis cache backend for Django
    rubygem-dalli.noarch : High performance memcached client for Ruby
    rubygem-rack-cache.noarch : HTTP Caching for Rack
    yum-NetworkManager-dispatcher.noarch : NetworkManager script which tells yum to check its cache on network change
    yum-plugin-rpm-warm-cache.noarch : Yum plugin to access the rpmdb files early to warm up access to the db
    
    ================================================================== N/S matched: openresty ==================================================================
    openresty.x86_64 : OpenResty, scalable web platform by extending NGINX with Lua
    openresty-asan.x86_64 : The clang AddressSanitizer (ASAN) version of OpenResty
    openresty-asan-debuginfo.x86_64 : Debug information for package openresty-asan
    openresty-debug.x86_64 : The debug version of OpenResty
    openresty-debug-debuginfo.x86_64 : Debug information for package openresty-debug
    openresty-debuginfo.x86_64 : Debug information for package openresty
    openresty-doc.noarch : OpenResty documentation tool, restydoc
    openresty-openssl.x86_64 : OpenSSL library for OpenResty
    openresty-openssl-asan.x86_64 : Clang AddressSanitizer Debug version of the OpenSSL library for OpenResty
    openresty-openssl-asan-debuginfo.x86_64 : Debug information for package openresty-openssl-asan
    openresty-openssl-asan-devel.x86_64 : Clang AddressSanitizer version of development files for OpenResty's OpenSSL library
    openresty-openssl-debug.x86_64 : Debug version of the OpenSSL library for OpenResty
    openresty-openssl-debug-debuginfo.x86_64 : Debug information for package openresty-openssl-debug
    openresty-openssl-debug-devel.x86_64 : Debug version of development files for OpenResty's OpenSSL library
    openresty-openssl-debuginfo.x86_64 : Debug information for package openresty-openssl
    openresty-openssl-devel.x86_64 : Development files for OpenResty's OpenSSL library
    openresty-opm.noarch : OpenResty Package Manager
    openresty-pcre.x86_64 : Perl-compatible regular expression library for OpenResty
    openresty-pcre-asan.x86_64 : Clang AddressSanitizer version of the Perl-compatible regular expression library for OpenResty
    openresty-pcre-asan-debuginfo.x86_64 : Debug information for package openresty-pcre-asan
    openresty-pcre-asan-devel.x86_64 : Development files for openresty-pcre-asan
    openresty-pcre-debuginfo.x86_64 : Debug information for package openresty-pcre
    openresty-pcre-devel.x86_64 : Development files for openresty-pcre
    openresty-resty.noarch : OpenResty command-line utility, resty
    openresty-valgrind.x86_64 : The Valgrind debug version of OpenResty
    openresty-valgrind-debuginfo.x86_64 : Debug information for package openresty-valgrind
    openresty-zlib.x86_64 : The zlib compression library for OpenResty
    openresty-zlib-asan.x86_64 : Clang AddressSanitizer version for the zlib compression library for OpenResty
    openresty-zlib-asan-debuginfo.x86_64 : Debug information for package openresty-zlib-asan
    openresty-zlib-asan-devel.x86_64 : Development files for OpenResty's zlib library
    openresty-zlib-debuginfo.x86_64 : Debug information for package openresty-zlib
    openresty-zlib-devel.x86_64 : Development files for OpenResty's zlib library
    perl-Test-Nginx.noarch : Data-driven test scaffold for Nginx C module and Nginx/OpenResty-based libraries and applications
    
      Name and summary matches mostly, use "search all" for everything.
    [root@s101 yinzhengjie]# 
    搜索openresty([root@s101 yinzhengjie]# yum -y search cache openresty)
    [root@s101 yinzhengjie]# yum -y install openresty
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base: mirrors.huaweicloud.com
     * epel: mirrors.tuna.tsinghua.edu.cn
     * extras: mirror.bit.edu.cn
     * updates: mirrors.huaweicloud.com
    Resolving Dependencies
    --> Running transaction check
    ---> Package openresty.x86_64 0:1.13.6.2-1.el7.centos will be installed
    --> Processing Dependency: openresty-zlib >= 1.2.11-3 for package: openresty-1.13.6.2-1.el7.centos.x86_64
    --> Processing Dependency: openresty-pcre >= 8.42-1 for package: openresty-1.13.6.2-1.el7.centos.x86_64
    --> Processing Dependency: openresty-openssl >= 1.1.0h-1 for package: openresty-1.13.6.2-1.el7.centos.x86_64
    --> Running transaction check
    ---> Package openresty-openssl.x86_64 0:1.1.0h-3.el7.centos will be installed
    ---> Package openresty-pcre.x86_64 0:8.42-1.el7.centos will be installed
    ---> Package openresty-zlib.x86_64 0:1.2.11-3.el7.centos will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    ============================================================================================================================================================
     Package                                  Arch                          Version                                      Repository                        Size
    ============================================================================================================================================================
    Installing:
     openresty                                x86_64                        1.13.6.2-1.el7.centos                        openresty                        1.1 M
    Installing for dependencies:
     openresty-openssl                        x86_64                        1.1.0h-3.el7.centos                          openresty                        1.4 M
     openresty-pcre                           x86_64                        8.42-1.el7.centos                            openresty                        154 k
     openresty-zlib                           x86_64                        1.2.11-3.el7.centos                          openresty                         54 k
    
    Transaction Summary
    ============================================================================================================================================================
    Install  1 Package (+3 Dependent packages)
    
    Total download size: 2.6 M
    Installed size: 7.4 M
    Downloading packages:
    warning: /var/cache/yum/x86_64/7/openresty/packages/openresty-1.13.6.2-1.el7.centos.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID d5edeb74: NOKEY:03 ETA 
    Public key for openresty-1.13.6.2-1.el7.centos.x86_64.rpm is not installed
    (1/4): openresty-1.13.6.2-1.el7.centos.x86_64.rpm                                                                                    | 1.1 MB  00:00:11     
    (2/4): openresty-pcre-8.42-1.el7.centos.x86_64.rpm                                                                                   | 154 kB  00:00:00     
    (3/4): openresty-zlib-1.2.11-3.el7.centos.x86_64.rpm                                                                                 |  54 kB  00:00:00     
    (4/4): openresty-openssl-1.1.0h-3.el7.centos.x86_64.rpm                                                                              | 1.4 MB  00:00:13     
    ------------------------------------------------------------------------------------------------------------------------------------------------------------
    Total                                                                                                                       199 kB/s | 2.6 MB  00:00:13     
    Retrieving key from https://openresty.org/package/pubkey.gpg
    Importing GPG key 0xD5EDEB74:
     Userid     : "OpenResty Admin <admin@openresty.com>"
     Fingerprint: e522 18e7 0878 97dc 6dea 6d6d 97db 7443 d5ed eb74
     From       : https://openresty.org/package/pubkey.gpg
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      Installing : openresty-zlib-1.2.11-3.el7.centos.x86_64                                                                                                1/4 
      Installing : openresty-openssl-1.1.0h-3.el7.centos.x86_64                                                                                             2/4 
      Installing : openresty-pcre-8.42-1.el7.centos.x86_64                                                                                                  3/4 
      Installing : openresty-1.13.6.2-1.el7.centos.x86_64                                                                                                   4/4 
      Verifying  : openresty-zlib-1.2.11-3.el7.centos.x86_64                                                                                                1/4 
      Verifying  : openresty-openssl-1.1.0h-3.el7.centos.x86_64                                                                                             2/4 
      Verifying  : openresty-pcre-8.42-1.el7.centos.x86_64                                                                                                  3/4 
      Verifying  : openresty-1.13.6.2-1.el7.centos.x86_64                                                                                                   4/4 
    
    Installed:
      openresty.x86_64 0:1.13.6.2-1.el7.centos                                                                                                                  
    
    Dependency Installed:
      openresty-openssl.x86_64 0:1.1.0h-3.el7.centos        openresty-pcre.x86_64 0:8.42-1.el7.centos        openresty-zlib.x86_64 0:1.2.11-3.el7.centos       
    
    Complete!
    [root@s101 yinzhengjie]# 
    安装openresty服务([root@s101 yinzhengjie]# yum -y install openresty)
    [root@s101 yinzhengjie]# which openresty
    /usr/bin/openresty
    [root@s101 yinzhengjie]# 
    验证openresty是否安装成功([root@s101 yinzhengjie]# which openresty)

    3>.将s101的安装成功的rpm包分发到s102上

    [root@s101 yinzhengjie]# grep cachedir /etc/yum.conf 
    cachedir=/var/cache/yum/$basearch/$releasever
    [root@s101 yinzhengjie]# cd /var/cache/yum/$basearch/$releasever
    [root@s101 yum]# ll
    total 0
    drwxr-xr-x. 3 root root 14 May 24 00:21 x86_64
    [root@s101 yum]# cd x86_64/
    [root@s101 x86_64]# ll
    total 4
    drwxr-xr-x. 10 root root 4096 Aug  6 18:21 7
    [root@s101 x86_64]# cd 7
    [root@s101 7]# ll
    total 40
    drwxr-xr-x. 4 root root 4096 Aug  6 18:16 base
    drwxr-xr-x. 4 root root 4096 Aug  6 18:16 epel
    drwxr-xr-x. 4 root root 4096 Aug  6 18:16 extras
    drwxr-xr-x. 4 root root 4096 Jun  3 19:34 mysql56-community
    drwxr-xr-x. 4 root root 4096 Jun  3 19:34 mysql-connectors-community
    drwxr-xr-x. 4 root root 4096 Jun  3 19:34 mysql-tools-community
    drwxr-xr-x  4 root root 4096 Aug  6 18:15 openresty
    -rw-r--r--  1 root root  960 Aug  6 18:21 timedhosts
    -rw-r--r--. 1 root root 1184 Aug  6 18:16 timedhosts.txt
    drwxr-xr-x. 4 root root 4096 Aug  6 18:16 updates
    [root@s101 7]# cd openresty/
    [root@s101 openresty]# ll
    total 76
    -rw-r--r-- 1 root root 58801 May 14 23:20 91a55c04e6f818158d041bb9bc0f7f03750a015e4436c6b5649371dc1036aaa4-primary.sqlite.bz2
    -rw-r--r-- 1 root root     0 Aug  6 18:15 cachecookie
    drwxr-xr-x 2 root root    30 Aug  6 18:16 gen
    drwxr-xr-x 2 root root  4096 Aug  6 18:21 packages
    -rw-r--r-- 1 root root  2991 May 14 23:20 repomd.xml
    -rw-r--r-- 1 root root   490 May 14 23:20 repomd.xml.asc
    [root@s101 openresty]# cd packages/
    [root@s101 packages]# ll
    total 2740
    -rw-r--r-- 1 root root 1122948 May 14 23:08 openresty-1.13.6.2-1.el7.centos.x86_64.rpm
    -rw-r--r-- 1 root root 1441932 May 14 20:03 openresty-openssl-1.1.0h-3.el7.centos.x86_64.rpm
    -rw-r--r-- 1 root root  157592 May 14 16:46 openresty-pcre-8.42-1.el7.centos.x86_64.rpm
    -rw-r--r-- 1 root root   55004 Jul 14  2017 openresty-zlib-1.2.11-3.el7.centos.x86_64.rpm
    [root@s101 packages]# 
    [root@s101 packages]# scp * s102:~
    openresty-1.13.6.2-1.el7.centos.x86_64.rpm                                                                                100% 1097KB   1.1MB/s   00:00    
    openresty-openssl-1.1.0h-3.el7.centos.x86_64.rpm                                                                          100% 1408KB   1.4MB/s   00:00    
    openresty-pcre-8.42-1.el7.centos.x86_64.rpm                                                                               100%  154KB 153.9KB/s   00:00    
    openresty-zlib-1.2.11-3.el7.centos.x86_64.rpm                                                                             100%   54KB  53.7KB/s   00:00    
    [root@s101 packages]#
    将s101下载好的rpm包拷贝到s102([root@s101 packages]# scp * s102:~)
    [yinzhengjie@s102 ~]$ su root
    Password: 
    [root@s102 yinzhengjie]# cd
    [root@s102 ~]# ll
    total 289812
    -rw-------. 1 root root      2748 May 23 23:15 anaconda-ks.cfg
    -rw-r--r--. 1 root root 214092195 May 27 01:06 blk_1073741826
    -rw-r--r--. 1 root root  79874467 May 27 01:05 blk_1073741827
    drwxr-xr-x. 9 root root      4096 Aug 17  2016 hadoop-2.7.3
    -rw-r--r--. 1 root root   1122948 Aug  6 18:43 openresty-1.13.6.2-1.el7.centos.x86_64.rpm
    -rw-r--r--. 1 root root   1441932 Aug  6 18:43 openresty-openssl-1.1.0h-3.el7.centos.x86_64.rpm
    -rw-r--r--. 1 root root    157592 Aug  6 18:43 openresty-pcre-8.42-1.el7.centos.x86_64.rpm
    -rw-r--r--. 1 root root     55004 Aug  6 18:43 openresty-zlib-1.2.11-3.el7.centos.x86_64.rpm
    [root@s102 ~]#
    [root@s102 ~]# rpm -ivh  *.rpm --force
    warning: openresty-1.13.6.2-1.el7.centos.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID d5edeb74: NOKEY
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:openresty-zlib-1.2.11-3.el7.cento################################# [ 25%]
       2:openresty-openssl-1.1.0h-3.el7.ce################################# [ 50%]
       3:openresty-pcre-8.42-1.el7.centos ################################# [ 75%]
       4:openresty-1.13.6.2-1.el7.centos  ################################# [100%]
    [root@s102 ~]# 
    在s102使用rpm方式安装openresty([root@s102 ~]# rpm -ivh *.rpm --force)
    [root@s102 ~]# which openresty            #验证是否安装成功
    /usr/bin/openresty
    [root@s102 ~]# 
    验证openresty是否安装成功([root@s102 ~]# which openresty)

    4>.修改s101和s102的Nginx配置文件

    [root@s101 ~]# more /usr/local/openresty/nginx/conf/nginx.conf
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        log_format  main  escape=json $msec#$time_local#$clientRealIp#$http_client_time#$status#$request_body;
        access_log  logs/access.log  main;
        sendfile        on;    
        keepalive_timeout  65;
        map $http_x_forwarded_for  $clientRealIp {
            ~^(?P<firstAddr>[0-9.]+),?.*$    $firstAddr;
        }
        server {
            listen       80;
            server_name  localhost;
            underscores_in_headers on;
            location / {
                root   html;
                index  index.html index.htm;
                error_page 405 =200 $1;
                lua_need_request_body on;                                                                     
                content_by_lua 'local s = ngx.var.request_body';
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }
    [root@s101 ~]# openresty -t      #检查配置文件是否存在异常
    nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
    [root@s101 ~]# 
    [root@s101 ~]# openresty        #运行nginx
    [root@s101 ~]# 
    [root@s101 ~]# netstat -untalp | grep 80
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      19757/nginx: master 
    tcp        0      0 172.30.100.101:8019     0.0.0.0:*               LISTEN      6685/java           
    tcp        0      0 172.30.100.101:8020     0.0.0.0:*               LISTEN      6380/java           
    tcp        0      0 172.30.100.101:50070    0.0.0.0:*               LISTEN      6380/java           
    tcp        0      0 172.30.100.101:8020     172.30.100.104:45635    ESTABLISHED 6380/java           
    tcp        0      0 172.30.100.101:8020     172.30.100.102:54883    ESTABLISHED 6380/java           
    tcp        0      0 172.30.100.101:8020     172.30.100.101:49054    ESTABLISHED 6380/java           
    tcp        0      0 172.30.100.101:49054    172.30.100.101:8020     ESTABLISHED 6685/java           
    tcp        0      0 172.30.100.101:46284    172.30.100.105:8020     TIME_WAIT   -                   
    tcp        0      0 172.30.100.101:35841    172.30.100.103:8480     TIME_WAIT   -                   
    tcp        0      0 172.30.100.101:8020     172.30.100.103:35239    ESTABLISHED 6380/java           
    tcp6       0      0 172.30.100.101:8088     :::*                    LISTEN      6883/java           
    tcp6       0      0 172.30.100.101:8030     :::*                    LISTEN      6883/java           
    tcp6       0      0 172.30.100.101:8031     :::*                    LISTEN      6883/java           
    tcp6       0      0 172.30.100.101:8032     :::*                    LISTEN      6883/java           
    tcp6       0      0 172.30.100.101:8033     :::*                    LISTEN      6883/java           
    tcp6       0      0 172.30.100.101:8031     172.30.100.103:38640    ESTABLISHED 6883/java           
    tcp6       0      0 172.30.100.101:8031     172.30.100.104:57601    ESTABLISHED 6883/java           
    tcp6       0      0 172.30.100.101:8031     172.30.100.102:58908    ESTABLISHED 6883/java           
    [root@s101 ~]# 
    [root@s102 logs]# more /usr/local/openresty/nginx/conf/nginx.conf
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        log_format  main  escape=json $msec#$time_local#$clientRealIp#$http_client_time#$status#$request_body;
        access_log  logs/access.log  main;
        sendfile        on;    
        keepalive_timeout  65;
        map $http_x_forwarded_for  $clientRealIp {
            ~^(?P<firstAddr>[0-9.]+),?.*$    $firstAddr;
        }
        server {
            listen       80;
            server_name  localhost;
            underscores_in_headers on;
            location / {
                root   html;
                index  index.html index.htm;
                error_page 405 =200 $1;
                lua_need_request_body on;                                                                     
                content_by_lua 'local s = ngx.var.request_body';
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }
    [root@s102 logs]# openresty -t
    nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
    [root@s102 logs]# openresty 
    [root@s102 logs]# 
    [root@s102 logs]# ps -ef | grep nginx | grep -v grep
    root       6865      1  0 19:20 ?        00:00:00 nginx: master process openresty
    nobody     6866   6865  0 19:20 ?        00:00:00 nginx: worker process
    [root@s102 logs]# 
    [root@s102 logs]# netstat -untalp | grep 80
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6865/nginx: master  
    tcp        0      0 0.0.0.0:8480            0.0.0.0:*               LISTEN      4240/java           
    tcp        0      0 172.30.100.102:54883    172.30.100.101:8020     ESTABLISHED 4159/java           
    tcp        0      0 172.30.100.102:37051    172.30.100.105:8020     ESTABLISHED 4159/java           
    tcp6       0      0 :::8040                 :::*                    LISTEN      4387/java           
    tcp6       0      0 :::8042                 :::*                    LISTEN      4387/java           
    tcp6       0      0 :::58022                :::*                    LISTEN      4081/java           
    tcp6       0      0 172.30.100.102:58908    172.30.100.101:8031     ESTABLISHED 4387/java           
    [root@s102 logs]# 
    s102节点的nginx.conf配置文件内容([root@s102 logs]# more /usr/local/openresty/nginx/conf/nginx.conf)

    5>.在s101编写滚动日志脚本并同步到s102(别忘记给脚本添加执行权限)

    [root@s101 logs]# more /usr/local/openresty/nginx/logs/roll_log.sh
    #!/bin/bash
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie
    #EMAIL:y1053419035@qq.com
    
    #定义日期格式
    dateStr=`date '+%Y-%m-%d-%H-%M'`
    
    #将之前的日志文件改名,将之前的access.log添加我们上面定义的日期格式
    mv /usr/local/openresty/nginx/logs/access.log /usr/local/openresty/nginx/logs/access.log.${dateStr}
    
    #改名之后,我们需要创建出access.log文件,因为新的日志文件会持续往这里发送哟
    touch access.log
    
    #重新加载配置文件信息
    openresty -s reload
    [root@s101 logs]# 
    [root@s101 logs]# chmod +x roll_log.sh        #别忘记加执行权限哟
    [root@s101 logs]# ll
    total 12
    -rw-r--r-- 1 root root   0 Aug  6 20:48 access.log
    -rw-r--r-- 1 root root 684 Aug  6 20:48 error.log
    -rw-r--r-- 1 root root   6 Aug  6 19:17 nginx.pid
    -rwxr-xr-x 1 root root 531 Aug  6 20:19 roll_log.sh
    [root@s101 logs]# 
    [root@s101 logs]# scp /usr/local/openresty/nginx/logs/roll_log.sh s102:/usr/local/openresty/nginx/logs/roll_log.sh
    roll_log.sh                                                                                                               100%  531     0.5KB/s   00:00    
    [root@s101 logs]#     

    6>.通过linux crontab调度脚本执行【注意,s101和s102要执行相同的操作哟】

    [root@s101 logs]# echo "* * * * * root source /etc/profile;/usr/local/openresty/nginx/logs/roll_log.sh" >> /etc/crontab 
    [root@s101 logs]# 
    [root@s101 logs]# cat /etc/crontab 
    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    
    # For details see man 4 crontabs
    
    # Example of job definition:
    # .---------------- minute (0 - 59)
    # |  .------------- hour (0 - 23)
    # |  |  .---------- day of month (1 - 31)
    # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
    # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
    # |  |  |  |  |
    # *  *  *  *  * user-name  command to be executed
    
    * * * * * root source /etc/profile;/usr/local/openresty/nginx/logs/roll_log.sh
    [root@s101 logs]# systemctl enable crond.service
    [root@s101 logs]# service crond status
    Redirecting to /bin/systemctl status  crond.service
    ● crond.service - Command Scheduler
       Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
       Active: inactive (dead) since Mon 2018-08-06 20:27:26 PDT; 3min 46s ago
     Main PID: 767 (code=exited, status=0/SUCCESS)
    
    Aug 04 19:26:03 s101 systemd[1]: Started Command Scheduler.
    Aug 04 19:26:03 s101 systemd[1]: Starting Command Scheduler...
    Aug 04 19:26:04 s101 crond[767]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 78% if used.)
    Aug 04 19:26:04 s101 crond[767]: (CRON) INFO (running with inotify support)
    Aug 06 20:27:26 s101 crond[767]: (CRON) INFO (Shutting down)
    Aug 06 20:27:26 s101 systemd[1]: Stopping Command Scheduler...
    Aug 06 20:27:26 s101 systemd[1]: Stopped Command Scheduler.
    [root@s101 logs]# service crond start
    Redirecting to /bin/systemctl start  crond.service
    [root@s101 logs]# service crond status
    Redirecting to /bin/systemctl status  crond.service
    ● crond.service - Command Scheduler
       Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
       Active: active (running) since Mon 2018-08-06 20:31:21 PDT; 3s ago
     Main PID: 20654 (crond)
       CGroup: /system.slice/crond.service
               └─20654 /usr/sbin/crond -n
    
    Aug 06 20:31:21 s101 crond[20654]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 79% if used.)
    Aug 06 20:31:21 s101 systemd[1]: Started Command Scheduler.
    Aug 06 20:31:21 s101 systemd[1]: Starting Command Scheduler...
    Aug 06 20:31:21 s101 crond[20654]: (CRON) INFO (running with inotify support)
    Aug 06 20:31:21 s101 crond[20654]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
    [root@s101 logs]# 
    [root@s102 ~]# echo "* * * * * root source /etc/profile;/usr/local/openresty/nginx/logs/roll_log.sh" >> /etc/crontab 
    [root@s102 ~]# cat /etc/crontab 
    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    
    # For details see man 4 crontabs
    
    # Example of job definition:
    # .---------------- minute (0 - 59)
    # |  .------------- hour (0 - 23)
    # |  |  .---------- day of month (1 - 31)
    # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
    # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
    # |  |  |  |  |
    # *  *  *  *  * user-name  command to be executed
    
    * * * * * root source /etc/profile;/usr/local/openresty/nginx/logs/roll_log.sh
    [root@s102 ~]# service crond status
    Redirecting to /bin/systemctl status  crond.service
    ● crond.service - Command Scheduler
       Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
       Active: active (running) since Sat 2018-08-04 19:26:12 PDT; 2 days ago
     Main PID: 783 (crond)
       CGroup: /system.slice/crond.service
               └─783 /usr/sbin/crond -n
    
    Aug 04 19:26:12 s102 systemd[1]: Started Command Scheduler.
    Aug 04 19:26:12 s102 systemd[1]: Starting Command Scheduler...
    Aug 04 19:26:12 s102 crond[783]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 18% if used.)
    Aug 04 19:26:13 s102 crond[783]: (CRON) INFO (running with inotify support)
    [root@s102 ~]# 
    s102只需要重复s101的步骤即可([root@s102 ~]# echo "* * * * * root source /etc/profile;/usr/local/openresty/nginx/logs/roll_log.sh" >> /etc/crontab )

    四.使用Java代码测试Nginx反向代理是否成功

    1>.编写资源工具类

    {
      "appChannel": "anroid bus",
      "appId": "sohuvideo",
      "appPlatform": "ios",
      "appVersion": "1.1.0",
      "deviceStyle": "oppo 1",
      "errorLogs": [
        {
          "appChannel": "umeng",
          "appId": "gaodemap",
          "appPlatform": "blackberry",
          "appVersion": "1.1.0",
          "deviceStyle": "vivo 3",
          "errorBrief": "at cn.lift.dfdf.web.AbstractBaseController.validInbound(AbstractBaseController.java:72)",
          "errorDetail": "at cn.lift.dfdfdf.control.CommandUtil.getInfo(CommandUtil.java:67) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606)",
          "osType": "android 4.0",
          "tenantId": "tnt023"
        }
      ],
      "eventLogs": [
        {
          "appChannel": "anroid bus",
          "appId": "tianya",
          "appPlatform": "android",
          "appVersion": "2.0.0",
          "deviceStyle": "红米",
          "eventId": "popmenu",
          "osType": "ios11",
          "tenantId": "tnt009"
        },
        {
          "appChannel": "appstore",
          "appId": "gaodemap",
          "appPlatform": "android",
          "appVersion": "1.1.0",
          "deviceStyle": "iphone 7 plus",
          "eventId": "popmenu",
          "osType": "android 4.0",
          "tenantId": "tnt009"
        },
        {
          "appChannel": "appstore",
          "appId": "faobengplay",
          "appPlatform": "blackberry",
          "appVersion": "1.0.0",
          "deviceStyle": "vivo 3",
          "eventId": "autoImport",
          "osType": "ios11",
          "tenantId": "tnt009"
        },
        {
          "appChannel": "anroid bus",
          "appId": "tianya",
          "appPlatform": "blackberry",
          "appVersion": "1.1.0",
          "deviceStyle": "iphone 7 plus",
          "eventId": "popmenu",
          "osType": "mi 5.5",
          "tenantId": "tnt009"
        },
        {
          "appChannel": "anroid bus",
          "appId": "tianya",
          "appPlatform": "android",
          "appVersion": "1.2.0",
          "deviceStyle": "iphone 7",
          "eventId": "bookstore",
          "osType": "ios11",
          "tenantId": "tnt501"
        }
      ],
      "osType": "ios11",
      "pageLogs": [
        null,
        null,
        null,
        null,
        null
      ],
      "startupLogs": [
        {
          "appChannel": "anroid bus",
          "appId": "faobengplay",
          "appPlatform": "ios",
          "appVersion": "1.2.0",
          "brand": "魅族",
          "carrier": "中国铁通",
          "country": "america",
          "deviceStyle": "vivo 3",
          "network": "wifi",
          "osType": "android 4.0",
          "province": "guangdong",
          "screenSize": "480 * 320",
          "tenantId": "tnt501"
        },
        {
          "appChannel": "appstore",
          "appId": "sohuvideo",
          "appPlatform": "blackberry",
          "appVersion": "2.0.0",
          "brand": "Apple",
          "carrier": "中国铁通",
          "country": "china",
          "deviceStyle": "iphone 7",
          "network": "3g",
          "osType": "ios11",
          "province": "guangxi",
          "screenSize": "1136 * 640",
          "tenantId": "tnt501"
        }
      ],
      "tenantId": "tnt009",
      "usageLogs": [
        {
          "appChannel": "umeng",
          "appId": "gaodemap",
          "appPlatform": "winphone",
          "appVersion": "1.1.0",
          "deviceStyle": "iphone 7",
          "osType": "ios11",
          "tenantId": "tnt009"
        },
        {
          "appChannel": "anroid bus",
          "appId": "tianya",
          "appPlatform": "android",
          "appVersion": "1.2.0",
          "deviceStyle": "iphone 6",
          "osType": "android 4.0",
          "tenantId": "tnt501"
        },
        {
          "appChannel": "umeng",
          "appId": "tianya",
          "appPlatform": "winphone",
          "appVersion": "1.0.0",
          "deviceStyle": "vivo 3",
          "osType": "mi 5.5",
          "tenantId": "tnt023"
        },
        {
          "appChannel": "umeng",
          "appId": "sohuvideo",
          "appPlatform": "android",
          "appVersion": "1.0.0",
          "deviceStyle": "iphone 6 plus",
          "osType": "mi 5.5",
          "tenantId": "tnt023"
        }
        ]
    }
    demo.json 文件内容
     1 /*
     2 @author :yinzhengjie
     3 Blog:http://www.cnblogs.com/yinzhengjie/tag/%E5%89%8D%E7%AB%AF%E9%82%A3%E4%BA%9B%E4%BA%8B/
     4 EMAIL:y1053419035@qq.com
     5 */
     6 package cn.org.yinzhengjie.umeng.phone;
     7 
     8 import java.io.ByteArrayOutputStream;
     9 import java.io.IOException;
    10 import java.io.InputStream;
    11 
    12 /**
    13  * 资源工具类
    14  */
    15 public class ResourceUtil {
    16 
    17     /**
    18      * 获得类路径下的资源文件流
    19      */
    20     public static InputStream getResource(String file){
    21         //得到当前线程使用的类加载器
    22         ClassLoader loader = Thread.currentThread().getContextClassLoader();
    23         InputStream in = loader.getResourceAsStream(file) ;
    24         return in ;
    25     }
    26 
    27     /**
    28      * 以String方式读取流内容
    29      */
    30     public static String getResourceInString(String file) throws IOException {
    31         InputStream in = getResource(file) ;
    32         ByteArrayOutputStream baos = new ByteArrayOutputStream() ;
    33         byte[] buf = new byte[1024] ;
    34         int len =0 ;
    35         while((len = in.read(buf)) != -1){
    36             baos.write(buf , 0 , len);
    37         }
    38         in.close();
    39         return new String(baos.toByteArray()) ;
    40     }
    41 }

    2>.编写上传日志代码

     1 /*
     2 @author :yinzhengjie
     3 Blog:http://www.cnblogs.com/yinzhengjie/tag/%E5%89%8D%E7%AB%AF%E9%82%A3%E4%BA%9B%E4%BA%8B/
     4 EMAIL:y1053419035@qq.com
     5 */
     6 package cn.org.yinzhengjie.umeng.phone;
     7 
     8 import java.io.OutputStream;
     9 import java.net.HttpURLConnection;
    10 import java.net.URL;
    11 import java.util.Date;
    12 
    13 /**
    14  * 上传日志
    15  */
    16 public class Uploader {
    17     public static void main(String[] args) {
    18         try {
    19             String str = "http://localhost:80/index.html" ;
    20 
    21             URL url = new URL(str);
    22             //获得url连接
    23             HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    24             //post方式提交
    25             conn.setRequestMethod("POST");
    26             //允许输出
    27             conn.setDoOutput(true);
    28             //设置上传的数据类型
    29             conn.setRequestProperty("content-type" , "application/json");
    30             //设置时间对齐
    31             conn.setRequestProperty("client_time", new Date().getTime() + "");
    32 
    33             //从连接中获取输出流
    34             OutputStream out = conn.getOutputStream();
    35             //读取资源样本日志文件
    36             String json = ResourceUtil.getResourceInString("demo.json") ;
    37             //将json数据通过输出流发送出去
    38             out.write(json.getBytes());
    39             out.flush();
    40             out.close();
    41             //通过返回的响应信息判断是否传输成功
    42             int code = conn.getResponseCode() ;
    43             //如果状态码是200说明发送成功了
    44             if(code == 200){
    45                 System.out.println("OK");
    46             }
    47         } catch (Exception e) {
    48             e.printStackTrace();
    49         }
    50     }
    51 }
    52 
    53 
    54 
    55 /*
    56 以上代码执行结果如下:
    57 OK
    58  */

    3>.查看s101服务器端日志是否收到

     

    4>.查看s102端日志是否收到

     

  • 相关阅读:
    【k8s部署】6. 部署 worker 节点
    【k8s部署】5. 部署 master 节点
    【k8s部署】4. 部署 etcd 集群
    【k8s部署】3. 安装和配置 kubectl
    【k8s部署】2. 创建CA根证书和秘钥
    【k8s部署】1. 环境准备和初始化
    【踩坑】OpenStack4j使用过程中关于OSClientSession被更改的问题记录
    10. 配置Horizon — OpenStack Queens 三节点部署
    9. 配置网络(VXLAN_OpenvSwitch)— OpenStack Queens 三节点部署
    Mac安装svn(解决新系统Xcode不支持问题)
  • 原文地址:https://www.cnblogs.com/yinzhengjie/p/9428404.html
Copyright © 2020-2023  润新知