Easy Tutorial
❮ Verilog Function Javascript Qrcodejs Library ❯

Introduction to OpenResty Usage

Category Programming Technology

Introduction to OpenResty Usage

At the T2 launch event, Hammer Technology donated the ticket revenue to the OpenResty open-source project. Today, we will introduce what OpenResty is.


Introduction to OpenResty

OpenResty (also known as ngx_openresty) is a scalable web platform based on NGINX, initiated by Chinese developer Yichun Zhang, and includes many high-quality third-party modules.

OpenResty is a powerful web application server. Web developers can use the Lua scripting language to leverage various C and Lua modules supported by NGINX. More importantly, in terms of performance, OpenResty can quickly build ultra-high-performance web application systems capable of handling over 10,000 concurrent connections.

Companies like 360, UPYUN, Alibaba Cloud, Sina, Tencent, Qunar, and Kugou Music are deep users of OpenResty.


OpenResty Installation

tutorialpro.org is based on ubuntu14.04 to install OpenResty.

OpenResty dependencies include: perl 5.6.1+, libreadline, libpcre, libssl.

So we need to install these dependencies first, which is quite simple:

apt-get install libreadline-dev libpcre3-dev libssl-dev perl

If your system is Centos or RedHat, you can use the following commands:

yum install readline-devel pcre-devel openssl-devel

Next, we can download the latest OpenResty source package from the official website (https://openresty.org/cn/) and unzip it for compilation and installation:

wget https://openresty.org/download/ngx_openresty-1.9.7.1.tar.gz   # Download
tar xzvf ngx_openresty-1.9.7.1.tar.gz       # Unzip
cd ngx_openresty-1.9.7.1/ 
./configure
make 
make install

By default, the program will be installed in the /usr/local/openresty directory. You can use ./configure --help to view more configuration options.


Hello World Example

After successful installation, we can use OpenResty to directly output HTML pages.

First, we can create a working directory:

mkdir /home/www
cd /home/www/
mkdir logs/ conf/

The logs directory is used to store logs, and the conf directory is used to store configuration files.

Next, we create an nginx.conf file in the conf directory with the following code:

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 9000;
        location / {
            default_type text/html;
            content_by_lua '
                ngx.say("<p>Hello, World!</p>")
            ';
        }
    }
}

If you are familiar with Nginx configuration, you should be familiar with the above code. Here, we directly write the HTML code in the configuration file.

Start OpenResty

By default, OpenResty is installed in the /usr/local/openresty directory, and the startup command is:

cd /home/www
/usr/local/openresty/nginx/sbin/nginx -p `pwd`/ -c conf/nginx.conf

If there is no output, the startup is successful. -p specifies our project directory, and -c specifies the configuration file.

Next, we can use curl to test if it can be accessed normally:

curl http://localhost:9000/

The output result is:

<p>Hello, World!</p>

Or access http://localhost:9000/ through a browser:

You see, the HTML we wrote in the configuration file has been output normally.

OpenResty aims to run your web service directly within the Nginx service, leveraging Nginx's non-blocking I/O model, not only for HTTP client requests but also for remote backends such as MySQL, PostgreSQL, Memcached, and Redis, providing consistent high-performance responses.

Therefore, for some high-performance services, you can directly use OpenResty to access databases like MySQL or Redis without going through third-party languages (PHP, Python, Ruby) to access the database and then return, which greatly improves the performance of the application.


Related Sites

** Share My Notes

Cancel

-

-

-

❮ Verilog Function Javascript Qrcodejs Library ❯