第1步 - 下載OpenResty的源代碼和依賴關系
在本節中,我們將從源代碼安裝OpenResty。首先,從OpenResty網站的下載頁面找到最新的OpenResty源代碼版本。下載tarball,確保如果更改了版本號,請使用最新版本號。
wget Explorer 6(及更早版本)並不總是正確處理GZIP內容。
添加include ../sites/*; ,它告訴OpenResty在/usr/local/openresty/nginx/sites目錄中查找額外的配置文件,稍後我們將創建它。
刪除所有server塊,我們將在此步驟中稍後重新定位到新文件。
接下來,創建我們在include行中指定的新sites目錄。 sudo mkdir /usr/local/openresty/nginx/sites
創建default網站。 sudo nano /usr/local/openresty/nginx/sites/default.conf
在此新文件中添加以下內容。這是從nginx.conf重新定位原始服務器塊,但有更多細節的內聯註釋。
/usr/local/openresty/nginx/sites/default.conf
server {
# Listen on port 80.
listen 80 default_server;
listen [::]:80 default_server;
# The document root.
root /usr/local/openresty/nginx/html/default;
# Add index.php if you are using PHP.
index index.html index.htm;
# The server name, which isn't relevant in this case, because we only have one.
server_name _;
# When we try to access this site...
location / {
# ... first attempt to serve request as file, then as a directory,
# then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# Redirect server error pages to the static page /50x.html.
error_page ? 500 502 503 504 ?/50x.html;
location = /50x.html {
root /usr/local/openresty/nginx/html;
}}
保存並關閉文件。 現在,為此網站創建壹個新目錄。 sudo mkdir /usr/local/openresty/nginx/html/default
然後將原始index.html從其原始位置移動到新目錄。 sudo mv /usr/local/openresty/nginx/html/index.html /usr/local/openresty/nginx/html/default
最後,重新啟動OpenResty以使用此新站點。 sudo systemctl restart openresty
您現在可以再次訪問http:// your_server_ip ,並查看與之前相同的網頁。 現在OpenResty是完全配置的,我們可以嘗試壹些由OpenResty介紹的,在Nginx默認情況下不可用的功能。
第5步 - 使用OpenResty Lua模塊
在本節中,我們將看看OpenResty添加的不同模塊的組合,這些模塊都存在以適應Lua腳本。我們將在整個步驟中/usr/local/openresty/nginx/sites/default.conf /usr/local/openresty/nginx/sites/default.conf,因此首先打開它。 sudo nano /usr/local/openresty/nginx/sites/default.conf
首先,我們將看壹下content_by_lua_block配置選項。 從下面的示例配置中復制location塊,並將其添加到server塊中,位於兩個現有location塊下面。
/usr/local/openresty/nginx/sites/default.conf content_by_lua_block示例
server {
. . .
location /example {
default_type 'text/plain';
content_by_lua_block {
ngx.say('Hello, Sammy!')
}
}}
保存並關閉文件,然後重新加載配置。 sudo systemctl reload openresty
如果您http:// your_server_ip /example訪問http:// your_server_ip /example ,您會看到壹個http:// your_server_ip /example 您好,Sammy的頁面! 。讓我們解釋這是如何工作的。 content_by_lua_block配置指令執行其中的所有內容作為Lua代碼。 在這裏,我們使用Lua函數ngx.say來打印消息Hello,Sammy!到頁面。 對於另壹個示例,將location /example塊的內容替換為:
/usr/local/openresty/nginx/sites/default.conf content_by_lua_file示例
server {
. . .
location /example {
default_type 'text/plain';
content_by_lua_file /usr/local/openresty/nginx/html/default/index.lua;
}}
content_by_lua_file從外部文件加載Lua內容,所以讓我們創建上面指定的內容: /usr/local/openresty/nginx/html/default/index.lua 。 sudo nano /usr/local/openresty/nginx/html/default/index.lua
將以下內容添加到文件,然後保存並將其關閉。
/usr/local/openresty/nginx/html/default/index.lua
local name = ngx.var.arg_name or "Anonymous"ngx.say("Hello, ", name, "!")
這是壹個簡單的Lua,它讀取URL中的查詢參數, name並自定義問候消息。如果沒有傳遞參數,則使用“匿名”。 再次重新加載配置。 sudo systemctl reload openresty
現在,在瀏覽器中訪問http:// your_server_ip /example?name= Sammy 。 這將顯示妳好,Sammy! 。 您可以更改name查詢參數,或完全忽略它。 Hello, Sammy!
您還可以更改name查詢參數以顯示任何其他名稱。 警告:請勿將您要加載的Lua文件從Web訪問到的位置。如果這樣做,如果有人訪問此文件,則可能會包含應用程序代碼。將文件放在文檔根目錄之外,例如,將文檔根目錄更改為/usr/local/openresty/nginx/html/default/public ,並將Lua文件放在其上壹個目錄。