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.
It’s usually in this path.
And then we go to the home directory.
Create and switch to a directory named ‘hello’ for our example.
Create the boilerplate sub-directories for the OpenResty application.
mkdir logs conf
Then let’s create a simple nginx.conf file under the ‘conf’ sub-directory. Here we use vim.
- 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’.
Now let’s test if the configuration is correct with the ‘-t’ option.
nginx -p $PWD/ -t
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.
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!
This article and its associated video are both generated automatically from a simple screenplay file.
Yichun Zhang is the creator of the OpenResty® open source project. He is also the founder and CEO of the OpenResty Inc. company. He contributed a dozen open source Nginx 3rd-party modules, quite some Nginx and LuaJIT core patches, and designed the OpenResty XRay platform.
We provide the Chinese translation for this article on blog.openresty.com.cn. We also welcome interested readers to contribute translations in other natural languages as long as the full article is translated without any omissions. We thank them in advance.
We always welcome talented and enthusiastic engineers to join our team at OpenResty Inc.
to explore various open source software’s internals and build powerful analyzers and
visualizers for real world applications built atop the open source software. If you are
interested, please send your resume to
email@example.com . Thank you!