W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
?HelloWorld
? 是我們亙古不變的第一個入門程序。但是 ?OpenResty
? 不是一門編程語言,跟其他編程語言的? HelloWorld
?不一樣,讓我們看看都有哪些不一樣吧。
OpenResty 安裝之后就有配置文件及相關(guān)的目錄的,為了工作目錄與安裝目錄互不干擾,并順便學下簡單的配置文件編寫,我們另外創(chuàng)建一個 OpenResty 的工作目錄來練習,并且另寫一個配置文件。我選擇在當前用戶目錄下創(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
在 conf 目錄下創(chuàng)建一個文本文件作為配置文件,命名為 nginx.conf,文件內(nèi)容如下:
worker_processes 1; #nginx worker 數(shù)量
error_log logs/error.log; #指定錯誤日志文件路徑
events {
worker_connections 1024;
}
http {
server {
#監(jiān)聽端口,若你的6699端口已經(jīng)被占用,則需要修改
listen 6699;
location / {
default_type text/html;
content_by_lua_block {
ngx.say("HelloWorld")
}
}
}
}
提示:如果你安裝的是 openresty 1.9.3.1 及以下版本,請使用 ?content_by_lua
?命令代替示例中的 ?content_by_lua_block
???墒褂?nbsp;?nginx -V
?命令查看版本號。
我們啟動 Nginx 即可,輸入命令形式為:?nginx -p ~/openresty-test
?,如果沒有提示錯誤。如果提示 nginx 不存在,則需要在環(huán)境變量中加入安裝路徑,可以根據(jù)你的操作平臺,參考前面的安裝章節(jié)(一般需要重啟生效)。
啟動成功后,我們可以查看 nginx 進程是否存在,并通過訪問 HTTP 頁面查看應(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
在瀏覽器中完成同樣的訪問:
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: