OpenResty HelloWorld

2021-08-12 16:00 更新

?HelloWorld? 是我們亙古不變的第一個(gè)入門程序。但是 ?OpenResty? 不是一門編程語(yǔ)言,跟其他編程語(yǔ)言的? HelloWorld ?不一樣,讓我們看看都有哪些不一樣吧。

創(chuàng)建工作目錄

OpenResty 安裝之后就有配置文件及相關(guān)的目錄的,為了工作目錄與安裝目錄互不干擾,并順便學(xué)下簡(jiǎn)單的配置文件編寫(xiě),我們另外創(chuàng)建一個(gè) OpenResty 的工作目錄來(lái)練習(xí),并且另寫(xiě)一個(gè)配置文件。我選擇在當(dāng)前用戶目錄下創(chuàng)建 openresty-test 目錄,并在該目錄下創(chuàng)建 logs 和 conf 子目錄分別用于存放日志和配置文件。

$ mkdir ~/openresty-test ~/openresty-test/logs/ ~/openresty-test/conf/
$
$ tree ~/openresty-test
/Users/yuansheng/openresty-test
├── conf
└── logs

2 directories, 0 files

創(chuàng)建配置文件

在 conf 目錄下創(chuàng)建一個(gè)文本文件作為配置文件,命名為 nginx.conf,文件內(nèi)容如下:

worker_processes  1;        #nginx worker 數(shù)量
error_log logs/error.log;   #指定錯(cuò)誤日志文件路徑
events {
    worker_connections 1024;
}

http {
    server {
        #監(jiān)聽(tīng)端口,若你的6699端口已經(jīng)被占用,則需要修改
        listen 6699;
        location / {
            default_type text/html;

            content_by_lua_block {
                ngx.say("HelloWorld")
            }
        }
    }
}

提示:如果你安裝的是 openresty 1.9.3.1 及以下版本,請(qǐng)使用 ?content_by_lua ?命令代替示例中的 ?content_by_lua_block???墒褂?nbsp;?nginx -V ?命令查看版本號(hào)。

萬(wàn)事俱備只欠東風(fēng)

我們啟動(dòng) Nginx 即可,輸入命令形式為:?nginx -p ~/openresty-test?,如果沒(méi)有提示錯(cuò)誤。如果提示 nginx 不存在,則需要在環(huán)境變量中加入安裝路徑,可以根據(jù)你的操作平臺(tái),參考前面的安裝章節(jié)(一般需要重啟生效)。

啟動(dòng)成功后,我們可以查看 nginx 進(jìn)程是否存在,并通過(guò)訪問(wèn) HTTP 頁(yè)面查看應(yīng)答內(nèi)容。操作提示如下:

?  ~ nginx -p ~/openresty-test
?  ~ ps -ef | grep nginx
  501 88620     1   0 10:58AM ?? 0:00.00 nginx: master process nginx -p
                                    /Users/yuansheng/openresty-test
  501 88622 88620   0 10:58AM ?? 0:00.00 nginx: worker process
?  ~ curl http://localhost:6699 -i
HTTP/1.1 200 OK
Server: openresty/1.9.7.3
Date: Sun, 20 Mar 2016 03:01:35 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive

HelloWorld

在瀏覽器中完成同樣的訪問(wèn):


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)