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.
1 | export PATH=/usr/local/openresty/nginx/sbin:$PATH |
It’s usually in this path.
And then we go to the home directory.
1 | cd ~/ |
Create and switch to a directory named ‘hello’ for our example.
1 | mkdir hello |
Create the boilerplate sub-directories for the OpenResty application.
1 | mkdir logs conf |
Then let’s create a simple nginx.conf file under the ‘conf’ sub-directory. Here we use vim.
1 | 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’.
1 | worker_processes 1; |
Now let’s test if the configuration is correct with the ‘-t’ option.
1 | nginx -p $PWD/ -t |
Looking good!
Now let’s start this OpenResty application for real.
1 | nginx -p $PWD/ |
And check if the nginx processes are running.
1 | 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.
1 | 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 This Article and Associated Video
This article and its associated video are both generated automatically from a simple screenplay file.
About The Author
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.
Translations
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 are hiring
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 talents@openresty.com
. Thank you!