Check out how OpenResty XRay helps organizations troubleshoot issues and optimize the performance of their applications.

Learn More LIVE DEMO

This tutorial demonstrates how to implement a “hello world” HTTP interface using OpenResty.

screenshot 1

First of all, we make sure we are using OpenResty’s nginx.

export PATH=/usr/local/openresty/nginx/sbin:$PATH
which nginx

screenshot 2

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

screenshot 5

Create the boilerplate sub-directories for the OpenResty application.

mkdir logs conf
ls

screenshot 6

Then let’s create a simple nginx.conf file under the ‘conf’ sub-directory. Here we use vim.

vim conf/nginx.conf
  1. Let’s enable a single nginx worker process for simplicity.
  2. We enable at most 1024 per-worker connections.
  3. And here we configure an HTTP server.
  4. Listen to the 8080 port with ‘reuseport’ enabled.
  5. Finally we add a root location to this server.
  6. We set the default MIME type to text/plain.
  7. 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

screenshot 17

Looking good!

Now let’s start this OpenResty application for real.

nginx -p $PWD/

screenshot 19

And check if the nginx processes are running.

ps aux|grep nginx|grep -v /tmp/

screenshot 20

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’.

screenshot 23

We can also try accessing the / URI in a web browser.

screenshot 25

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.