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.

1
2
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.

1
cd ~/

Create and switch to a directory named ‘hello’ for our example.

1
2
mkdir hello
cd hello

screenshot 5

Create the boilerplate sub-directories for the OpenResty application.

1
2
mkdir logs conf
ls

screenshot 6

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

1
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’.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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.

1
nginx -p $PWD/ -t

screenshot 17

Looking good!

Now let’s start this OpenResty application for real.

1
nginx -p $PWD/

screenshot 19

And check if the nginx processes are running.

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

1
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 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!