nginx tutorial
Aug 08, 2016 am 09:23 AMNginx and Lua
Quotations from Fire Cloud Evil God: In martial arts in the world, nothing is unbreakable, only speed! Nginx's specialty is speed, and Lua's specialty is speed. The combination of the two undoubtedly has a genetic advantage in speed.
OpenResty was the first to combine Nginx and Lua. It has an ngx_lua module that embeds Lua into Nginx; then Tengine also included the ngx_lua module. As for the difference between the two: OpenResty is the Bundle of Nginx; while Tengine is the Fork of Nginx. It is worth mentioning that OpenResty and Tengine are both projects created by Chinese people. The former is mainly developed by Chun Ge and Xiaozhe, and the latter is mainly managed by Taobao.
As for the advantages and disadvantages of OpenResty and Tengine, it is left to everyone to judge for themselves. The following information is available for reference:
- ngx_openresty: an Nginx ecosystem glued by Lua
- Taobao Nginx application, customization and development practice
Recommended to read the transcript of Brother Chun's speech on "Nginx Ecological Environment Glued by Lua" at Tech-Club, there is information!
Installation
requires the latest version of Nginx, LuaJIT, ngx_devel_kit, ngx_lua and other installation files.
It is possible to install Lua or LuaJIT, but for efficiency reasons, it is recommended to install LuaJIT.
Because it is installed in the default path, the lib and include corresponding to LuaJIT are all in the /usr/local directory.
Now you can compile Nginx:
Try to start Nginx and see if you are unlucky, you may encounter the following error:
cannot open shared object file: No such file or directory
Is this a crazy situation? You can use the ldd command to check:
Such problems can usually be solved using the ldconfig command:
Try to start Nginx again and it should be OK.
Application
Let’s warm things up first with a simple program: add the following code to Nginx’s configuration file nginx.conf, restart Nginx, and then browse to see the effect.
Before learning ngx_lua in depth, it is recommended that you carefully read the Nginx tutorial written by Brother Chun.
Here I will talk about the key point: the language used in the Nginx configuration file is essentially "declarative" rather than "procedural". When Nginx processes a request, the execution of instructions is not determined by the physical order when the instructions are defined, but depends on the stage to which the instructions belong. The commonly used stages in Nginx are: rewrite stage, access stage, content stage, etc. . The set instruction in the demo code belongs to the rewrite stage, and the content_by_lua instruction belongs to the content stage. If you try to exchange the order of the two instructions, you will find that the program can still run normally.
Let’s try to write a more practical example using Redis.
First, we need to create a Redis configuration file config.json with the following content:
Then, we create a script init.lua that parses the configuration file, which uses Lua CJSON module:
Description: Shared memory is used in the code, so that the configuration file does not need to be parsed for every request.
Next, we create a script content.lua that renders content, using Resty Redis module:
Note: It is recommended to put the Resty Redis module in the vendor directory and set it up in Nginx later.
Finally, we need to set it in the Nginx configuration file:
Note: To facilitate debugging, I turned off lua_code_cache. If it is a production environment, it should be turned on.
In addition, when installing CJSON, you need to pay attention to the path of the header file in the Makefile. The default is:
If you are installing LuaJIT, it is best to copy the header file to the corresponding directory:
…
I recently participated A project of , which provides some interfaces for Web polling, all implemented using Nginx + Lua. Although there are only a dozen servers in total, it can provide billions of requests every day, which is very powerful.
Finally, let me conclude by quoting a quote from a loser: Lua is the favorite of unmarried male programmers.
This entry was published by Lao Wang in the Technical category and tagged Lua and Nginx. The fixed link Add to Favorites.There are 29 comments on "NGINX and LUA"
The above has introduced the nginx tutorial, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

Question: How to start Nginx? Answer: Install Nginx Startup Nginx Verification Nginx Is Nginx Started Explore other startup options Automatically start Nginx

NGINX and Apache have their own advantages and disadvantages and are suitable for different scenarios. 1.NGINX is suitable for high concurrency and low resource consumption scenarios. 2. Apache is suitable for scenarios where complex configurations and rich modules are required. By comparing their core features, performance differences, and best practices, you can help you choose the server software that best suits your needs.

Practical Tips for Improving PhpStorm Performance in CentOS Systems This article provides a variety of methods to help you optimize the performance of PhpStorm in CentOS systems and thus improve development efficiency. Before implementing any optimization measures, be sure to back up important data and verify the results in the test environment. 1. System-level optimization and streamline system services: Disable unnecessary system services and daemons to reduce system resource usage. Interfaceless Mode: Switching to interfaceless mode can significantly save resources if you do not need a graphical interface. Uninstall redundant software: Remove software packages and services that are no longer in use and free up system resources. 2. PHP configuration optimization enable OPcache: install and configure OPcache extensions to display
