https://www.bilibili.com/vide…

I’ll show you how to implement a “Hello World” HTTP interface using OpenResty.

First, we need to make sure that we are using Nginx for OpenResty.

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

It’s always going to be this path.

Then we go to the home directory.

cd ~/

In our example, create and switch to a directory named hello.

mkdir hello
cd hello

Create a template subdirectory for your OpenResty application.

mkdir logs conf
ls

Then let’s create a simple nginx.conf file in the “conf” subdirectory.

vim conf/nginx.conf
  1. For simplicity’s sake, let’s enable a single NGINX worker process.
  2. We enable a maximum of 1024 connections per worker process.
  3. Here we configure an HTTP server.
  4. To enable thereuseportAfter listening on port 8080.
  5. Finally, we add a root location to the server.
  6. We set the default MIME type to text/plain.
  7. We embed some Lua code to emit a response body that is “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 the configuration with the -t option.

nginx -p $PWD/ -t

It looks good!

Now let’s actually launch the OpenResty application.

nginx -p $PWD/

And check to see if the nginx process is running.

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

Very good! They’re all up. A master process, a worker process.

Now we can use the curl command-line utility to send a test HTTP request to the server.

The curl 'http://127.0.0.1:8080/'

We do get the response body Hello, world.

We can also try accessing/URIs in a web browser.

As you can see, it also displays “Hello World” as expected. If you enjoyed this tutorial, please subscribe to this blog site and our YouTube channel or Station B channel. Thank you very much!

About this article and associated videos

This article and the associated videos were generated entirely automatically by our OpenResty Demo system from a very simple script file.

About the author

Yicchun Zhang is the founder of OpenRESTY ®, an open source project, and OpenRESTY Inc. Founder and CEO of the company. He has contributed many third-party modules for Nginx, quite a few Nginx and Luajit core patches, and has designed products such as OpenResty Xray.

Pay attention to our

If you enjoyed this article, please follow our blog at OpenResty Inc. Also welcome to scan the code to pay attention to our WeChat official account:

translation

We have provided the original English and Chinese versions (this article). We also welcome readers to provide translations in other languages. As long as the full text is translated without omission, we will consider using it. Thank you very much!