通过yum安装

在 /etc/yum.repos.d/ 下新建 OpenResty.repo 内容

[openresty]
name=Official OpenResty Repository
baseurl=https://copr-be.cloud.fedoraproject.org/results/openresty/openresty/epel-$releasever-$basearch/
skip_if_unavailable=True
gpgcheck=
gpgkey=https://copr-be.cloud.fedoraproject.org/results/openresty/openresty/pubkey.gpg
enabled=
enabled_metadata=

查看可用的openresty软件

yum --disablerepo="*" --enablerepo="openresty" list available

当前安装的是 openresty.x86_64 版本1.11.2.2-8.el7.centos, 内置的openssl 是 1.0.2j

安装

yum install openresty

默认会安装到 /usr/local/openresty/ 目录下, 目录下包含了 luajit, lualib, nginx, openssl, pcre, zlib 这些组件

如果安装时显示Require GeoIP, 需要先安装geoip后再安装openresty

yum install epel-release
yum --enablerepo=epel install geoip

使用命令行直接启动

/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf

OpenResty安装时, 已经把路径加入了/usr/bin, 并且添加了服务 /etc/init.d/openresty, 可以通过服务脚本启动

systemctl start/stop/status openresty

可以通过-p参数设置工作目录, 对应nginx的conf, html, log都可以放到这个目录下

openresty -p /opt/my-fancy-app/

防火墙检查和配置

# 查看状态
systemctl status firewalld
# 查看开放的端口
firewall-cmd --zone=public --list-all
# 添加80端口
firewall-cmd --permanent --zone=public --add-port=/tcp
firewall-cmd --reload

Update 2018-07-17: 如果使用了-p参数, 将工作区间指向了其他目录, 那么在配置nginx.conf时, 需要将 user  nobody 修改为其他用户, 例如新建用户nginx, 或openresty, 或tomcat, 如果直接用nobody, 会导致启动进程一直被阻塞, 在/var/log/secure里能看到类似如下的权限错误

Unregistered Authentication Agent for unix-process:17339:1142872114 (system bus name :1.389659, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.utf8) (disconnected from bus)

配置Openresty结合Redis进行IP封禁

修改 nginx.conf, 因为使用的是Openresty的安装包, 所以不需要在前面再设置模块路径, 可以直接引用

# http 下增加
lua_shared_dict ip_blacklist 1m; # server 下增加
location /ipblacklist {
access_by_lua_file lua/ip_blacklist.lua;
default_type text/html;
content_by_lua '
ngx.say("<p>Hello, this is lua.</p>")
';
}

在/usr/local/openresty/nginx/lua下新建 ip_blacklist.lua

-- a quick LUA access script for nginx to check IP addresses against an
-- `ip_blacklist` set in Redis, and if a match is found send a HTTP 403.
--
-- allows for a common blacklist to be shared between a bunch of nginx
-- web servers using a remote redis instance. lookups are cached for a
-- configurable period of time.
--
-- block an ip:
-- redis-cli SADD ip_blacklist 10.1.1.1
-- remove an ip:
-- redis-cli SREM ip_blacklist 10.1.1.1
--
-- also requires lua-resty-redis from:
-- https://github.com/agentzh/lua-resty-redis
--
-- your nginx http context should contain something similar to the
-- below: (assumes resty/redis.lua exists in /etc/nginx/lua/)
--
-- lua_package_path "/etc/nginx/lua/?.lua;;";
-- lua_shared_dict ip_blacklist 1m;
--
-- you can then use the below (adjust path where necessary) to check
-- against the blacklist in a http, server, location, if context:
--
-- access_by_lua_file /etc/nginx/lua/ip_blacklist.lua;
--
-- from https://gist.github.com/chrisboulton/6043871
-- modify by Ceelog local redis_host = "192.168.1.18"
local redis_port =
local redis_auth = "foobar"
local redis_db = -- connection timeout for redis in ms. don't set this too high!
local redis_connection_timeout = -- check a set with this key for blacklist entries
local redis_key = "ip_blacklist" -- cache lookups for this many seconds
local cache_ttl = -- end configuration ngx.log(ngx.DEBUG, "Redis host: " .. redis_host); local ip = ngx.var.remote_addr
local ip_blacklist = ngx.shared.ip_blacklist
local last_update_time = ip_blacklist:get("last_update_time"); -- only update ip_blacklist from Redis once every cache_ttl seconds:
if last_update_time == nil or last_update_time < ( ngx.now() - cache_ttl ) then local redis = require "resty.redis";
local red = redis:new(); red:set_timeout(redis_connect_timeout); local ok, err = red:connect(redis_host, redis_port);
if not ok then
ngx.log(ngx.DEBUG, "Redis connection error while retrieving ip_blacklist: " .. err);
else
local ok, err = red:auth(redis_auth);
if not ok then
ngx.log(ngx.DEBUG, "Redis auth error while retrieving ip_blacklist:" .. err);
end
red:select(redis_db);
local new_ip_blacklist, err = red:smembers(redis_key);
if err then
ngx.log(ngx.DEBUG, "Redis read error while retrieving ip_blacklist: " .. err);
else
-- replace the locally stored ip_blacklist with the updated values:
ip_blacklist:flush_all();
for index, banned_ip in ipairs(new_ip_blacklist) do
ip_blacklist:set(banned_ip, true);
end -- update time
ip_blacklist:set("last_update_time", ngx.now());
end
end
end if ip_blacklist:get(ip) then
ngx.log(ngx.DEBUG, "Banned IP detected and refused access: " .. ip);
return ngx.exit(ngx.HTTP_FORBIDDEN);
end

因为这里设置了缓存, 60s更新一次, 所以不需要再使用redis pool, 如果需要使用pool, 可以参考

https://github.com/openresty/lua-resty-redis#synopsis

# you do not need the following line if you are using
# the OpenResty bundle:
lua_package_path "/path/to/lua-resty-redis/lib/?.lua;;"; server {
location /test {
content_by_lua_block {
local redis = require "resty.redis"
local red = redis:new() red:set_timeout() -- 1 sec -- or connect to a unix domain socket file listened
-- by a redis server:
-- local ok, err = red:connect("unix:/path/to/redis.sock") local ok, err = red:connect("127.0.0.1", )
if not ok then
ngx.say("failed to connect: ", err)
return
end ... -- put it into the connection pool of size 100,
-- with 10 seconds max idle time
local ok, err = red:set_keepalive(, )
if not ok then
ngx.say("failed to set keepalive: ", err)
return
end -- or just close the connection right away:
-- local ok, err = red:close()
-- if not ok then
-- ngx.say("failed to close: ", err)
-- return
-- end
...
}

以及

如果你想让 A 和 B 这两个不同的 redis 后端分别保持最多  个长连接,则在访问 A 或者 B 的 resty.redis 对象上都调用 set_keepalive(, ),因为默认情况下不同的 redis 后端拥有不同的连接池。

如果你希望 A 和 B 都共享一个连接池,则可以在 connect() 方法中指定 pool 选项,例如:
redA:connect("A", , { pool = "my_redis_cluster" })
redB:connect("B", , { pool = "my_redis_cluster" }) 然后在调用 set_keepalive 时使用 作为这个 A、B 共享池的连接数上限:
redA:set_keepalive(, )
redB:set_keepalive(, ) 不过要注意的一点是,连接池是每 worker 的粒度,所以实际每个 redis 后端的总连接数上限还要再用 剩上 worker 数。

Centos7安装Openresty的更多相关文章

  1. Centos7安装Openresty和orange

    1.说明 以下全部操作均已root用户执行 2.安装 2.1 安装依赖 yum install readline-devel pcre-devel openssl-devel gcc 2.2 下载op ...

  2. 全网最详细的Centos7系统里安装Openresty(图文详解)

    不多说,直接上干货! 介绍: Nginx 采用一个 master 进程管理多个 worker 进程(master-worker)模式,基本的事件处理都在 woker 中,master 负责一些全局初始 ...

  3. 在CentOS7上源码安装OpenResty

    您必须将这些库perl 5.6.1+libreadlinelibpcrelibssl安装在您的电脑之中. 对于 Linux来说, 您需要确认使用 ldconfig 命令,让其在您的系统环境路径中能找到 ...

  4. 安装OpenResty开发环境

    OpenResty是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超高并发.扩展性极高的动态 Web ...

  5. HP服务器 hp 360g5 centos7安装问题

    HP服务器  hp 360g5 centos7安装问题 一 :启动盘无法识别硬盘 1.进入安装光盘,用上下键选择安装centos--Install Centos7(注意不可按Enter键),如图: 2 ...

  6. CentOS7 安装Mono及Jexus

    CentOS7安装Mono及Juxes 1 安装Mono 1.1 安装yum-utils 因为安装要用到yum-config-manager,默认是没有安装的,所以要先安装yum-utils包.命令如 ...

  7. CentOS7安装mysql提示“No package mysql-server available.”

    针对centos7安装mysql,提示"No package mysql-server available."错误,解决方法如下: Centos 7 comes with Mari ...

  8. CentOS7安装Oracle 11gR2 安装

    概述 Oracle 在Linux和window上的安装不太一样,公司又是Linux系统上的Oracle,实在没辙,研究下Linux下Oracle的使用,oracle默认不支持CentOS系统安装,所以 ...

  9. Centos7安装完毕后重启提示Initial setup of CentOS Linux 7 (core)的解决方法

    问题: CentOS7安装完毕,重新开机启动后显示: Initial setup of CentOS Linux 7 (core) 1) [x] Creat user 2) [!] License i ...

随机推荐

  1. 转 Objective-C中不同方式实现锁(二)

    在上一文中,我们已经讨论过用Objective-C锁几种实现(跳转地址),也用代码实际的演示了如何通过构建一个互斥锁来实现多线程的资源共享及线程安全,今天我们继续讨论锁的一些高级用法. .NSRecu ...

  2. 算法:希尔排序(Shell Sort)

    背景 在三种简单的排序算法中(冒泡.选择和插入)插入排序的算法最好,不过插入过程可能需要进行大量的移动,如何尽可能少的移动元素呢?希尔排序正是基于对这个问题的思考而想出来的,考虑到希尔排序对已排序数组 ...

  3. Eclipse设置打印线

    在调出Preferences之后,选中Text Editors.先选中Show print margin,在Print margin column框中填入180就可以,然后选择以下的Print mar ...

  4. 一个对 Dijkstra 的采访视频

    之前在微博上推荐了一个对 Dijkstra 的采访视频,看了两遍之后觉得实在很好,所以再正式推荐一下.大部分人可能都知道他对图论算法和操作系统的贡献,而其实 Dijkstra 在程序语言上的造诣也很深 ...

  5. 谷歌死忠速查:可访问谷歌google搜索的IP们

    [见谅! 此文已废了,下面的办法已经无效了!]但是访问我博客并回复的哥们“问天何必”提供了一个方法,详见http://www.gaomn.com/,他提供了一个网站来访问google的,速度还算可以, ...

  6. “==”和equals之间的区别

    通俗点讲,==是看看左右是不是一个东西.equals是看看左右是不是长得一样.如果单纯是想记住,==:等于.equals:相同.两个长得一样的人,只能说长的相同(equals),但是不等于他们俩是一个 ...

  7. Rsync服务介绍与配置

    Rsync 简要介绍 rsync 是一个用于增量文件传输的开源工具,不得不说,rsync简直是不同服务器间传输文件.同步文件的利器.与FTP相比,它具有非常简单的安装和配置方法.而且,rsync可以只 ...

  8. 删除在Godaddy注册的域名,申请退款的全过程

    1,删除域名. 登录进 Godaddy ,进入域名管理(Domain Manager),点击你要删除的域名,把要删除的域名前面打上对勾,再点击“delete selected”,确认,稍等一会就删除了 ...

  9. 五个瓶颈影响你的Asp.Net程序(网站)性能

    在今天的手机设备世界里,生活的节奏继续加快,因此访问你的网站的用户的耐心也在渐渐失去.同时,我提供了非常多的特性,为了防止你的网站变得过时或者廉价,你必须跟上竞争对手.你想赢得访问者的喝彩,但访问者没 ...

  10. jQuery的搜索关键词自动匹配插件

    相信许多人都会用过搜索栏自动匹配关键词的功能,无论是像google的专业搜索引擎,还是普通的网站,现在许多都用上了这种关键词匹配技术,本文介绍的用jQuery实现的关键词匹配技术,当然要整合到自己的系 ...