OpenResty® Scalable Web Platform by Extending NGINX with Lua

OpenResty XRay

Advanced observability built for OpenResty and more

×

limited time offer

Request TRIAL today and receive a diagnostic REPORT
Learn more

Getting Started

Datong Sun , 05 Apr 2019 (created 20 Jun 2011)

First of all, you need to install OpenResty to your system.

If you are in Linux, please check out OpenResty's official pre-built packages if your Linux distribution is currently supported. If you use the official pre-built package, please replace the nginx command in this document below with the openresty command.

Failing that, you can go to the Download page to get the source code tarball of OpenResty, and see the Installation page for how to build and install it into your system.

HelloWorld

The simplest way of printing a hello world output to the console is to use the resty script shipped with OpenResty. For example, run the following command on the command line:

resty -e 'print("hello, world")'

It should print out the following line to the stdout device:

hello, world

The rest of this text details how to run a very efficient HTTP server that speaks "hello world".

If you install OpenResty through our official binary packages, then the resty utility should be visible to your PATH environment by default. Otherwise, you need to add the following line to your ~/.bashrc or ~/.bash_profile file:

export PATH=/usr/local/openresty/bin:$PATH

where we assume that you use the default installation prefix, /usr/local/openresty/, in your OpenResty installation.

Prepare directory layout

We first create a separate directory for our experiments. You can use an arbitrary directory. Here for simplicity, we just use ~/work:

mkdir ~/work
cd ~/work
mkdir logs/ conf/

Note that we've also created the logs/ directory for logging files and conf/ for our config files.

Prepare the nginx.conf config file

Create a simple plain text file named conf/nginx.conf with the following contents in it:

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 8080;
        location / {
            default_type text/html;
            content_by_lua_block {
                ngx.say("<p>hello, world</p>")
            }
        }
    }
}

If you're familiar with Nginx configuration, it should look very familiar to you. OpenResty is just an enhanced version of Nginx by means of addon modules anyway. You can take advantage of all the existing goodies in the Nginx world.

Start the Nginx server

Assuming you have installed OpenResty into /usr/local/openresty (this is the default), we make our nginx executable of our OpenResty installation available in our PATH environment:

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

Then we start the nginx server with our config file this way:

nginx -p `pwd`/ -c conf/nginx.conf

Error messages will go to the stderr device or the default error log files logs/error.log in the current working directory.

Access our HelloWorld web service

We can use curl to access our new web service that says HelloWorld:

curl http://localhost:8080/

If everything is okay, we should get the output

<p>hello, world</p>

You can surely point your favorite web browser to the location http://localhost:8080/.

Test performance

See Benchmark for details.

Where to go from here

View the documentation of each component at the Components page and find Nginx related stuff on the Nginx Wiki site.