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

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

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

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

worker_processes  ;
events {
worker_connections ;
}
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 ;
upstream servers{
server s101: weight=;
server s102: weight=;
}
map $http_x_forwarded_for $clientRealIp {
~^(?P<firstAddr>[-\.]+),?.*$ $firstAddr;
}
server {
listen ;
server_name localhost;
underscores_in_headers on;
location / {
error_page = $;
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 /50x.html;
location = /50x.html {
root html;
}
}
}

4>.启动openresty

Nginx插件之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=
repo_gpgcheck=
gpgkey=https://openresty.org/package/pubkey.gpg
enabled=
enabled_metadata=
[root@s101 yinzhengjie]#
[root@s101 yinzhengjie]# grep keepcache /etc/yum.conf
keepcache=
[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=
[root@s101 yinzhengjie]#

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

[root@s101 yinzhengjie]# yum -y search cache openresty
Loaded plugins: fastestmirror
base | 3.6 kB ::
epel/x86_64/metalink | 7.6 kB ::
epel | 3.2 kB ::
extras | 3.4 kB ::
openresty//x86_64/signature | B ::
Retrieving key from https://openresty.org/package/pubkey.gpg
Importing GPG key 0xD5EDEB74:
Userid : "OpenResty Admin <admin@openresty.com>"
Fingerprint: e522 18e7 97dc 6dea 6d6d 97db d5ed eb74
From : https://openresty.org/package/pubkey.gpg
openresty//x86_64/signature | 2.9 kB :: !!!
updates | 3.4 kB ::
(/): extras//x86_64/primary_db | kB ::
(/): openresty//x86_64/primary_db | kB ::
(/): updates//x86_64/primary_db | 4.3 MB ::
(/): epel/x86_64/updateinfo | kB ::
(/): epel/x86_64/primary | 3.6 MB ::
Determining fastest mirrors
* base: mirrors.huaweicloud.com
* epel: mirrors.tuna.tsinghua.edu.cn
* extras: mirror.bit.edu.cn
* updates: mirrors.huaweicloud.com
epel /
==================================================================== 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 : 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-)
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 :1.13.6.2-.el7.centos will be installed
--> Processing Dependency: openresty-zlib >= 1.2.- for package: openresty-1.13.6.2-.el7.centos.x86_64
--> Processing Dependency: openresty-pcre >= 8.42- for package: openresty-1.13.6.2-.el7.centos.x86_64
--> Processing Dependency: openresty-openssl >= 1.1.0h- for package: openresty-1.13.6.2-.el7.centos.x86_64
--> Running transaction check
---> Package openresty-openssl.x86_64 :1.1.0h-.el7.centos will be installed
---> Package openresty-pcre.x86_64 :8.42-.el7.centos will be installed
---> Package openresty-zlib.x86_64 :1.2.-.el7.centos will be installed
--> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================================
Package Arch Version Repository Size
============================================================================================================================================================
Installing:
openresty x86_64 1.13.6.2-.el7.centos openresty 1.1 M
Installing for dependencies:
openresty-openssl x86_64 1.1.0h-.el7.centos openresty 1.4 M
openresty-pcre x86_64 8.42-.el7.centos openresty k
openresty-zlib x86_64 1.2.-.el7.centos openresty k Transaction Summary
============================================================================================================================================================
Install Package (+ Dependent packages) Total download size: 2.6 M
Installed size: 7.4 M
Downloading packages:
warning: /var/cache/yum/x86_64//openresty/packages/openresty-1.13.6.2-.el7.centos.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID d5edeb74: NOKEY: ETA
Public key for openresty-1.13.6.2-.el7.centos.x86_64.rpm is not installed
(/): openresty-1.13.6.2-.el7.centos.x86_64.rpm | 1.1 MB ::
(/): openresty-pcre-8.42-.el7.centos.x86_64.rpm | kB ::
(/): openresty-zlib-1.2.-.el7.centos.x86_64.rpm | kB ::
(/): openresty-openssl-1.1.0h-.el7.centos.x86_64.rpm | 1.4 MB ::
------------------------------------------------------------------------------------------------------------------------------------------------------------
Total kB/s | 2.6 MB ::
Retrieving key from https://openresty.org/package/pubkey.gpg
Importing GPG key 0xD5EDEB74:
Userid : "OpenResty Admin <admin@openresty.com>"
Fingerprint: e522 18e7 97dc 6dea 6d6d 97db 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.-.el7.centos.x86_64 /
Installing : openresty-openssl-1.1.0h-.el7.centos.x86_64 /
Installing : openresty-pcre-8.42-.el7.centos.x86_64 /
Installing : openresty-1.13.6.2-.el7.centos.x86_64 /
Verifying : openresty-zlib-1.2.-.el7.centos.x86_64 /
Verifying : openresty-openssl-1.1.0h-.el7.centos.x86_64 /
Verifying : openresty-pcre-8.42-.el7.centos.x86_64 /
Verifying : openresty-1.13.6.2-.el7.centos.x86_64 / Installed:
openresty.x86_64 :1.13.6.2-.el7.centos Dependency Installed:
openresty-openssl.x86_64 :1.1.0h-.el7.centos openresty-pcre.x86_64 :8.42-.el7.centos openresty-zlib.x86_64 :1.2.-.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)

Nginx插件之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
drwxr-xr-x. root root May : x86_64
[root@s101 yum]# cd x86_64/
[root@s101 x86_64]# ll
total
drwxr-xr-x. root root Aug :
[root@s101 x86_64]# cd
[root@s101 ]# ll
total
drwxr-xr-x. root root Aug : base
drwxr-xr-x. root root Aug : epel
drwxr-xr-x. root root Aug : extras
drwxr-xr-x. root root Jun : mysql56-community
drwxr-xr-x. root root Jun : mysql-connectors-community
drwxr-xr-x. root root Jun : mysql-tools-community
drwxr-xr-x root root Aug : openresty
-rw-r--r-- root root Aug : timedhosts
-rw-r--r--. root root Aug : timedhosts.txt
drwxr-xr-x. root root Aug : updates
[root@s101 ]# cd openresty/
[root@s101 openresty]# ll
total
-rw-r--r-- root root May : 91a55c04e6f818158d041bb9bc0f7f03750a015e4436c6b5649371dc1036aaa4-primary.sqlite.bz2
-rw-r--r-- root root Aug : cachecookie
drwxr-xr-x root root Aug : gen
drwxr-xr-x root root Aug : packages
-rw-r--r-- root root May : repomd.xml
-rw-r--r-- root root May : repomd.xml.asc
[root@s101 openresty]# cd packages/
[root@s101 packages]# ll
total
-rw-r--r-- root root May : openresty-1.13.6.2-.el7.centos.x86_64.rpm
-rw-r--r-- root root May : openresty-openssl-1.1.0h-.el7.centos.x86_64.rpm
-rw-r--r-- root root May : openresty-pcre-8.42-.el7.centos.x86_64.rpm
-rw-r--r-- root root Jul openresty-zlib-1.2.-.el7.centos.x86_64.rpm
[root@s101 packages]#
[root@s101 packages]# scp * s102:~
openresty-1.13.6.2-.el7.centos.x86_64.rpm % 1097KB .1MB/s :
openresty-openssl-1.1.0h-.el7.centos.x86_64.rpm % 1408KB .4MB/s :
openresty-pcre-8.42-.el7.centos.x86_64.rpm % 154KB .9KB/s :
openresty-zlib-1.2.-.el7.centos.x86_64.rpm % 54KB .7KB/s :
[root@s101 packages]#

将s101下载好的rpm包拷贝到s102([root@s101 packages]# scp * s102:~)

[yinzhengjie@s102 ~]$ su root
Password:
[root@s102 yinzhengjie]# cd
[root@s102 ~]# ll
total
-rw-------. root root May : anaconda-ks.cfg
-rw-r--r--. root root May : blk_1073741826
-rw-r--r--. root root May : blk_1073741827
drwxr-xr-x. root root Aug hadoop-2.7.
-rw-r--r--. root root Aug : openresty-1.13.6.2-.el7.centos.x86_64.rpm
-rw-r--r--. root root Aug : openresty-openssl-1.1.0h-.el7.centos.x86_64.rpm
-rw-r--r--. root root Aug : openresty-pcre-8.42-.el7.centos.x86_64.rpm
-rw-r--r--. root root Aug : openresty-zlib-1.2.-.el7.centos.x86_64.rpm
[root@s102 ~]#
[root@s102 ~]# rpm -ivh *.rpm --force
warning: openresty-1.13.6.2-.el7.centos.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID d5edeb74: NOKEY
Preparing... ################################# [%]
Updating / installing...
:openresty-zlib-1.2.-.el7.cento################################# [ %]
:openresty-openssl-1.1.0h-.el7.ce################################# [ %]
:openresty-pcre-8.42-.el7.centos ################################# [ %]
:openresty-1.13.6.2-.el7.centos ################################# [%]
[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 ;
events {
worker_connections ;
}
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 ;
map $http_x_forwarded_for $clientRealIp {
~^(?P<firstAddr>[-\.]+),?.*$ $firstAddr;
}
server {
listen ;
server_name localhost;
underscores_in_headers on;
location / {
root html;
index index.html index.htm;
error_page = $;
lua_need_request_body on;
content_by_lua 'local s = ngx.var.request_body';
}
error_page /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
tcp 0.0.0.0: 0.0.0.0:* LISTEN /nginx: master
tcp 172.30.100.101: 0.0.0.0:* LISTEN /java
tcp 172.30.100.101: 0.0.0.0:* LISTEN /java
tcp 172.30.100.101: 0.0.0.0:* LISTEN /java
tcp 172.30.100.101: 172.30.100.104: ESTABLISHED /java
tcp 172.30.100.101: 172.30.100.102: ESTABLISHED /java
tcp 172.30.100.101: 172.30.100.101: ESTABLISHED /java
tcp 172.30.100.101: 172.30.100.101: ESTABLISHED /java
tcp 172.30.100.101: 172.30.100.105: TIME_WAIT -
tcp 172.30.100.101: 172.30.100.103: TIME_WAIT -
tcp 172.30.100.101: 172.30.100.103: ESTABLISHED /java
tcp6 172.30.100.101: :::* LISTEN /java
tcp6 172.30.100.101: :::* LISTEN /java
tcp6 172.30.100.101: :::* LISTEN /java
tcp6 172.30.100.101: :::* LISTEN /java
tcp6 172.30.100.101: :::* LISTEN /java
tcp6 172.30.100.101: 172.30.100.103: ESTABLISHED /java
tcp6 172.30.100.101: 172.30.100.104: ESTABLISHED /java
tcp6 172.30.100.101: 172.30.100.102: ESTABLISHED /java
[root@s101 ~]#
[root@s102 logs]# more /usr/local/openresty/nginx/conf/nginx.conf
worker_processes ;
events {
worker_connections ;
}
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 ;
map $http_x_forwarded_for $clientRealIp {
~^(?P<firstAddr>[-\.]+),?.*$ $firstAddr;
}
server {
listen ;
server_name localhost;
underscores_in_headers on;
location / {
root html;
index index.html index.htm;
error_page = $;
lua_need_request_body on;
content_by_lua 'local s = ngx.var.request_body';
}
error_page /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 : ? :: nginx: master process openresty
nobody : ? :: nginx: worker process
[root@s102 logs]#
[root@s102 logs]# netstat -untalp | grep
tcp 0.0.0.0: 0.0.0.0:* LISTEN /nginx: master
tcp 0.0.0.0: 0.0.0.0:* LISTEN /java
tcp 172.30.100.102: 172.30.100.101: ESTABLISHED /java
tcp 172.30.100.102: 172.30.100.105: ESTABLISHED /java
tcp6 ::: :::* LISTEN /java
tcp6 ::: :::* LISTEN /java
tcp6 ::: :::* LISTEN /java
tcp6 172.30.100.102: 172.30.100.101: ESTABLISHED /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
-rw-r--r-- root root Aug : access.log
-rw-r--r-- root root Aug : error.log
-rw-r--r-- root root Aug : nginx.pid
-rwxr-xr-x root root Aug : 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 % .5KB/s :
[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 crontabs # Example of job definition:
# .---------------- minute ( - )
# | .------------- hour ( - )
# | | .---------- day of month ( - )
# | | | .------- month ( - ) OR jan,feb,mar,apr ...
# | | | | .---- day of week ( - ) (Sunday= or ) 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 -- :: PDT; 3min 46s ago
Main PID: (code=exited, status=/SUCCESS) Aug :: s101 systemd[]: Started Command Scheduler.
Aug :: s101 systemd[]: Starting Command Scheduler...
Aug :: s101 crond[]: (CRON) INFO (RANDOM_DELAY will be scaled with factor % if used.)
Aug :: s101 crond[]: (CRON) INFO (running with inotify support)
Aug :: s101 crond[]: (CRON) INFO (Shutting down)
Aug :: s101 systemd[]: Stopping Command Scheduler...
Aug :: s101 systemd[]: 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 -- :: PDT; 3s ago
Main PID: (crond)
CGroup: /system.slice/crond.service
└─ /usr/sbin/crond -n Aug :: s101 crond[]: (CRON) INFO (RANDOM_DELAY will be scaled with factor % if used.)
Aug :: s101 systemd[]: Started Command Scheduler.
Aug :: s101 systemd[]: Starting Command Scheduler...
Aug :: s101 crond[]: (CRON) INFO (running with inotify support)
Aug :: s101 crond[]: (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 crontabs # Example of job definition:
# .---------------- minute ( - )
# | .------------- hour ( - )
# | | .---------- day of month ( - )
# | | | .------- month ( - ) OR jan,feb,mar,apr ...
# | | | | .---- day of week ( - ) (Sunday= or ) 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 -- :: PDT; days ago
Main PID: (crond)
CGroup: /system.slice/crond.service
└─ /usr/sbin/crond -n Aug :: s102 systemd[]: Started Command Scheduler.
Aug :: s102 systemd[]: Starting Command Scheduler...
Aug :: s102 crond[]: (CRON) INFO (RANDOM_DELAY will be scaled with factor % if used.)
Aug :: s102 crond[]: (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 文件内容

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/%E5%89%8D%E7%AB%AF%E9%82%A3%E4%BA%9B%E4%BA%8B/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.umeng.phone; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; /**
* 资源工具类
*/
public class ResourceUtil { /**
* 获得类路径下的资源文件流
*/
public static InputStream getResource(String file){
//得到当前线程使用的类加载器
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream in = loader.getResourceAsStream(file) ;
return in ;
} /**
* 以String方式读取流内容
*/
public static String getResourceInString(String file) throws IOException {
InputStream in = getResource(file) ;
ByteArrayOutputStream baos = new ByteArrayOutputStream() ;
byte[] buf = new byte[] ;
int len = ;
while((len = in.read(buf)) != -){
baos.write(buf , , len);
}
in.close();
return new String(baos.toByteArray()) ;
}
}

2>.编写上传日志代码

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/%E5%89%8D%E7%AB%AF%E9%82%A3%E4%BA%9B%E4%BA%8B/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.umeng.phone; import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date; /**
* 上传日志
*/
public class Uploader {
public static void main(String[] args) {
try {
String str = "http://localhost:80/index.html" ; URL url = new URL(str);
//获得url连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//post方式提交
conn.setRequestMethod("POST");
//允许输出
conn.setDoOutput(true);
//设置上传的数据类型
conn.setRequestProperty("content-type" , "application/json");
//设置时间对齐
conn.setRequestProperty("client_time", new Date().getTime() + ""); //从连接中获取输出流
OutputStream out = conn.getOutputStream();
//读取资源样本日志文件
String json = ResourceUtil.getResourceInString("demo.json") ;
//将json数据通过输出流发送出去
out.write(json.getBytes());
out.flush();
out.close();
//通过返回的响应信息判断是否传输成功
int code = conn.getResponseCode() ;
//如果状态码是200说明发送成功了
if(code == ){
System.out.println("OK");
}
} catch (Exception e) {
e.printStackTrace();
}
}
} /*
以上代码执行结果如下:
OK
*/

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

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

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

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

上一篇:批量修改文件夹及文件用户权限和用户组权限 centos


下一篇:Alt.js的入门