安裝OpenResty(Nginx倉庫)
1.安裝
首先你的Linux虛擬機(jī)必須聯(lián)網(wǎng)
1)安裝開發(fā)庫
首先要安裝OpenResty的依賴開發(fā)庫,執(zhí)行命令:
yum install -y pcre-devel openssl-devel gcc --skip-broken
2)安裝OpenResty倉庫
你可以在你的 CentOS 系統(tǒng)中添加 openresty
倉庫,這樣就可以便于未來安裝或更新我們的軟件包(通過 yum check-update
命令)。運(yùn)行下面的命令就可以添加我們的倉庫:
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
如果提示說命令不存在,則運(yùn)行:
yum install -y yum-utils
然后再重復(fù)上面的命令
3)安裝OpenResty
然后就可以像下面這樣安裝軟件包,比如 openresty
:
yum install -y openresty
4)安裝opm工具
opm是OpenResty的一個(gè)管理工具,可以幫助我們安裝一個(gè)第三方的Lua模塊。
如果你想安裝命令行工具 opm
,那么可以像下面這樣安裝 openresty-opm
包:
yum install -y openresty-opm
5)目錄結(jié)構(gòu)
默認(rèn)情況下,OpenResty安裝的目錄是:/usr/local/openresty
看到里面的nginx目錄了嗎,OpenResty就是在Nginx基礎(chǔ)上集成了一些Lua模塊。
6)配置nginx的環(huán)境變量
打開配置文件:
vi /etc/profile
在最下面加入兩行:
export NGINX_HOME=/usr/local/openresty/nginx export PATH=${NGINX_HOME}/sbin:$PATH
NGINX_HOME:后面是OpenResty安裝目錄下的nginx的目錄
然后讓配置生效:
source /etc/profile
2.啟動(dòng)和運(yùn)行
OpenResty底層是基于Nginx的,查看OpenResty目錄的nginx目錄,結(jié)構(gòu)與windows中安裝的nginx基本一致:
所以運(yùn)行方式與nginx基本一致:
# 啟動(dòng)nginx nginx # 重新加載配置 nginx -s reload # 停止 nginx -s stop
nginx的默認(rèn)配置文件注釋太多,影響后續(xù)我們的編輯,這里將nginx.conf中的注釋部分刪除,保留有效部分。
修改/usr/local/openresty/nginx/conf/nginx.conf
文件,內(nèi)容如下:
#user nobody; worker_processes 1; error_log logs/error.log; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 8081; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
在Linux的控制臺(tái)輸入命令以啟動(dòng)nginx:
nginx
然后訪問頁面:http://192.168.150.101:8081,注意ip地址替換為你自己的虛擬機(jī)IP:
3.備注
加載OpenResty的lua模塊:
#lua 模塊 lua_package_path "/usr/local/openresty/lualib/?.lua;;"; #c模塊 lua_package_cpath "/usr/local/openresty/lualib/?.so;;";
common.lua
-- 封裝函數(shù),發(fā)送http請求,并解析響應(yīng) local function read_http(path, params) local resp = ngx.location.capture(path,{ method = ngx.HTTP_GET, args = params, }) if not resp then -- 記錄錯(cuò)誤信息,返回404 ngx.log(ngx.ERR, "http not found, path: ", path , ", args: ", args) ngx.exit(404) end return resp.body end -- 將方法導(dǎo)出 local _M = { read_http = read_http } return _M
釋放Redis連接API:
-- 關(guān)閉redis連接的工具方法,其實(shí)是放入連接池 local function close_redis(red) local pool_max_idle_time = 10000 -- 連接的空閑時(shí)間,單位是毫秒 local pool_size = 100 --連接池大小 local ok, err = red:set_keepalive(pool_max_idle_time, pool_size) if not ok then ngx.log(ngx.ERR, "放入redis連接池失敗: ", err) end end
讀取Redis數(shù)據(jù)的API:
-- 查詢r(jià)edis的方法 ip和port是redis地址,key是查詢的key local function read_redis(ip, port, key) -- 獲取一個(gè)連接 local ok, err = red:connect(ip, port) if not ok then ngx.log(ngx.ERR, "連接redis失敗 : ", err) return nil end -- 查詢r(jià)edis local resp, err = red:get(key) -- 查詢失敗處理 if not resp then ngx.log(ngx.ERR, "查詢Redis失敗: ", err, ", key = " , key) end --得到的數(shù)據(jù)為空處理 if resp == ngx.null then resp = nil ngx.log(ngx.ERR, "查詢Redis數(shù)據(jù)為空, key = ", key) end close_redis(red) return resp end
開啟共享詞典:
# 共享字典,也就是本地緩存,名稱叫做:item_cache,大小150m lua_shared_dict item_cache 150m;
p == ngx.null then
resp = nil
ngx.log(ngx.ERR, "查詢Redis數(shù)據(jù)為空, key = ", key)
end
close_redis(red)
return resp
end
開啟共享詞典: ```nginx # 共享字典,也就是本地緩存,名稱叫做:item_cache,大小150m lua_shared_dict item_cache 150m;
到此這篇關(guān)于安裝OpenResty(Nginx倉庫)的文章就介紹到這了,更多相關(guān)OpenResty Nginx倉庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
CentOS7安裝Nginx并配置自動(dòng)啟動(dòng)的方法步驟
這篇文章主要介紹了CentOS7安裝Nginx并配置自動(dòng)啟動(dòng)的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10Nginx虛擬主機(jī)的搭建的實(shí)現(xiàn)步驟
本文主要介紹了Nginx虛擬主機(jī)的搭建的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Nginx捕獲并自定義proxy_pass返回的錯(cuò)誤問題
這篇文章主要介紹了Nginx捕獲并自定義proxy_pass返回的錯(cuò)誤問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06為高負(fù)載網(wǎng)絡(luò)優(yōu)化Nginx和Node.js的方法
如果不先對Nginx和Node.js的底層傳輸機(jī)制有所了解,并進(jìn)行針對性優(yōu)化,可能對兩者再細(xì)致的調(diào)優(yōu)也會(huì)徒勞無功。一般情況下,Nginx通過TCP socket來連接客戶端與上游應(yīng)用2013-02-02Nginx反向代理轉(zhuǎn)發(fā)tomcat的實(shí)現(xiàn)
本文主要介紹了Nginx反向代理轉(zhuǎn)發(fā)tomcat的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Nginx偽靜態(tài)配置和常用Rewrite偽靜態(tài)規(guī)則集錦
偽靜態(tài)是一種可以把文件后綴改成任何可能的一種方法,如果我想把php文件偽靜態(tài)成html文件,這種相當(dāng)簡單的,下面我來介紹nginx 偽靜態(tài)配置方法有需要了解的朋友可參考。2014-06-06