Hello World HTTP Example in OpenResty
This tutorial demonstrates how to implement a “hello world” HTTP interface using OpenResty.
First of all, we make sure we are using OpenResty’s nginx.
export PATH=/usr/local/openresty/nginx/sbin:$PATH
which nginx
It’s usually in this path.
And then we go to the home directory.
cd ~/
Create and switch to a directory named ‘hello’ for our example.
mkdir hello
cd hello
Create the boilerplate sub-directories for the OpenResty application.
mkdir logs conf
ls
Then let’s create a simple nginx.conf file under the ‘conf’ sub-directory. Here we use vim.
vim conf/nginx.conf
- Let’s enable a single nginx worker process for simplicity.
- We enable at most 1024 per-worker connections.
- And here we configure an HTTP server.
- Listen to the 8080 port with ‘reuseport’ enabled.
- Finally we add a root location to this server.
- We set the default MIME type to text/plain.
- We embed some Lua code to emit a response with the body ‘Hello World’.
worker_processes 1;
events {
worker_connections 1024;
}
http {
server {
listen 8080 reuseport;
location / {
default_type text/plain;
content_by_lua_block {
ngx.say("Hello World")
}
}
}
}
Now let’s test if the configuration is correct with the ‘-t’ option.
nginx -p $PWD/ -t
Looking good!
Now let’s start this OpenResty application for real.
nginx -p $PWD/
And check if the nginx processes are running.
ps aux|grep nginx|grep -v /tmp/
Nice. They are up. One master and one worker.
We can now send a test HTTP request to this server with the ‘curl’ command-line utility.
curl 'http://127.0.0.1:8080/'
We’re indeed getting the response body ‘Hello World’.
We can also try accessing the / URI in a web browser.
As we can see, it also displays “Hello World” as expected.
If you like this tutorial, please subscribe to this blog site and our YouTube channel. Thank you!
About The Author
Yichun Zhang (Github handle: agentzh), is the original creator of the OpenResty® open-source project and the CEO of OpenResty Inc..
Yichun is one of the earliest advocates and leaders of “open-source technology”. He worked at many internationally renowned tech companies, such as Cloudflare, Yahoo!. He is a pioneer of “edge computing”, “dynamic tracing” and “machine coding”, with over 22 years of programming and 16 years of open source experience. Yichun is well-known in the open-source space as the project leader of OpenResty®, adopted by more than 40 million global website domains.
OpenResty Inc., the enterprise software start-up founded by Yichun in 2017, has customers from some of the biggest companies in the world. Its flagship product, OpenResty XRay, is a non-invasive profiling and troubleshooting tool that significantly enhances and utilizes dynamic tracing technology. And its OpenResty Edge product is a powerful distributed traffic management and private CDN software product.
As an avid open-source contributor, Yichun has contributed more than a million lines of code to numerous open-source projects, including Linux kernel, Nginx, LuaJIT, GDB, SystemTap, LLVM, Perl, etc. He has also authored more than 60 open-source software libraries.