Background introduction
During project development, a service we handled before stored the generated data in redis, and the client obtained the specific data in redis through a specific key. In previous development, the architecture solution of nginx+wsgi+python was adopted. Projects can also be quickly implemented through python, and pushing to the test environment has been in use.
As time passed, I slowly thought about the project and found that this implementation method also had certain drawbacks. Because there is no complicated logic for the service, nginx receives the request and forwards it to the background python service; then the python service gets the specific request, fetches the data from redis and returns it to the client for calling. The whole process is actually relatively simple and clear. There is no need to add another layer of python services between nginx and redis, so I am thinking about whether the implementation of this architecture can be optimized.
nginx and lua
Later, after a simple search for nginx+redis, I found some keywords related to nginx+lua+openresty. The next step is to start researching openresty. Looking at the relevant documents, we found that openresty is a project set that integrates nginx core modules and various third-party modules. If you use openresty, you avoid installing various additional third-party packages, and they are all inherited into its installation package. This is definitely a good choice for lazy people.
Introduction to lua
lua is a scripting language, also known as glue language. It is a very small language that can be easily coupled with other languages. Through Lua's own characteristics, coupled with the language it is attached to, using the characteristics of two languages in one service is definitely not as powerful as usual. I have seen some awesome games using Lua as a glue-type functional language.
However, through simple use, I found that Lua's string processing is not powerful enough, because it directly uses the C library, and its string processing is only the interface provided by the C library, without additional enrichment and supplementation of the interface. In many scenarios, you need to implement some common interfaces yourself, such as split.
Integrating lua and ngxin
Since lua is a glue-type language, can lua be directly coupled in nginx? The answer is very yes. Lua can run directly in nginx to do some logical processing and log control. Then we can consider using nginx+lua to develop a web service to meet the needs and complete the convenience and rapid development that other server-side languages cannot complete.
In addition to the traversal mentioned above, what advantages will nginx+lua bring:
1. Reduce one layer of forwarding and use other service languages to develop services. A protocol will definitely be used to communicate directly between nginx and the server. Such as cgi, fcig, wsgi, etc. If you use lua, because lua runs directly in nginx, it is necessary to do an additional nginx forwarding.
2. Event-based response, because Lua is a runtime environment that directly runs nginx, so Lua inherits all the features of nginx. Under normal circumstances, nginx provides services based on events, select or epoll. It will definitely be awesome in terms of performance.
Based on the above reasons, nginx+lua was started. Because lazy people directly adopt the entire openresty installation strategy, of course you can also install nginx separately, then install lua, and then nginx_lua_module. After installing openresty, I started writing simple tests. In fact, there are related examples on openresty's github. You can directly do some simple test development based on the examples.
After completing the first step of installation and writing test examples, lua+nginx means that the integration has been successful. The next step is to develop your own business logic. What needs to be noted here is that openresty actually integrates nginx, so running two nginx on one machine may have corresponding problems. Therefore, after installing openresty, the existing nginx on the machine may have a certain impact.
Upgrade the original service
The development environment has been configured. The next step is to directly develop the business logic, that is, receive the request to access redis, read the data and return the client request. Because Lua can be written directly in the nginx configuration file, but this is not a good strategy. Although Lua is directly coupled with other languages, the degree of coupling must also consider software engineering-related issues. For example, the maintainability of later code and the readability of nginx configuration files.
In view of the above reasons, it is also recommended that the function implementation of Lua be written into a separate file, and then be declared in the nginx configuration file to tell nginx to load and run. On the basis of implementing functions, try to avoid the degree of coupling and maintainability of the code.
In the specific implementation, two files handle the entire service, of course because the service itself is simple.
-redis.conf #redis host, port
-init.lua #Initialization configuration file
The concept of nginx's shared memory is utilized in the implementation.
<code><span>--redis.conf</span> host:<span>127.0</span><span>.0</span><span>.1</span> port:<span>6379</span><span>--init.lua</span> tmp = {} <span>for</span> l <span>in</span> io.<span>lines</span>(<span>"lua/redis.conf"</span>) <span>do</span><span>for</span> i <span>in</span><span>string</span>.gmatch(l, <span>"([^:]+)"</span>) <span>do</span> table.insert(tmp, i) <span><span>end</span></span><span><span>end</span></span> ngx.shared.config:<span>set</span>(tmp[<span>1</span>], tmp[<span>2</span>]) ngx.shared.config:<span>set</span>(tmp[<span>3</span>], tmp[<span>4</span>])</code>
Here we need to read redis.conf when nginx starts, so we need to add a configuration to nginx to tell nginx that init.lua needs to be executed when it starts. In addition, the shared memory config of nginx must be declared, so the configuration of nginx is as follows
<code>lu<span>a_shared</span>_dict config <span>1</span>m<span>;</span> init_by_lu<span>a_file</span> 'lua/init.lua'<span>;</span></code>
第一步已经完成,就是redis相关配置的读取,共享内存的声明和初始化,那接下来就是具体的逻辑实现,几十行代码分分钟搞定。
<code> location /vector{ content_by_lua ' <span>local</span> redis = require <span>"resty.redis"</span><span>local</span> server = redis:new() <span>local</span> conf = ngx.shared.config <span>local</span> ok, err = server:connect(conf:<span>get</span>(<span>"host"</span>), conf:<span>get</span>(<span>"port"</span>)) ngx.header.content_type = <span>"text/plain"</span><span>if</span><span>not</span> ok <span>then</span> ngx.<span>log</span>(ngx.ERR, err) ngx.<span>exit</span>(ngx.HTTP_SERVICE_UNAVAILABLE) <span>end</span><span>local</span> x = ngx.var.arg_x; <span>local</span> y = ngx.var.arg_y; <span>local</span> z = ngx.var.arg_z; <span>if</span> x == nil <span>or</span> y == nil <span>or</span> z == nil <span>then</span> ngx.<span>say</span>(<span>"{\\\"ret\\\": -1}"</span>) ngx.<span>exit</span>(ngx.HTTP_SERVICE_UNAVAILABLE) <span>end</span><span>local</span> key = z..<span>"_"</span>..x..<span>"_"</span>..y <span>local</span> data = server:<span>get</span>(key) '; }</code>
版权声明:本文为博主原创文章,转载请注明来源。
以上就介绍了nginx中集成lua开发web服务,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

本篇文章给大家带来了关于nginx的相关知识,其中主要介绍了nginx拦截爬虫相关的,感兴趣的朋友下面一起来看一下吧,希望对大家有帮助。

高并发系统有三把利器:缓存、降级和限流;限流的目的是通过对并发访问/请求进行限速来保护系统,一旦达到限制速率则可以拒绝服务(定向到错误页)、排队等待(秒杀)、降级(返回兜底数据或默认数据);高并发系统常见的限流有:限制总并发数(数据库连接池)、限制瞬时并发数(如nginx的limit_conn模块,用来限制瞬时并发连接数)、限制时间窗口内的平均速率(nginx的limit_req模块,用来限制每秒的平均速率);另外还可以根据网络连接数、网络流量、cpu或内存负载等来限流。1.限流算法最简单粗暴的

nginx php403错误的解决办法:1、修改文件权限或开启selinux;2、修改php-fpm.conf,加入需要的文件扩展名;3、修改php.ini内容为“cgi.fix_pathinfo = 0”;4、重启php-fpm即可。

实验环境前端nginx:ip192.168.6.242,对后端的wordpress网站做反向代理实现复杂均衡后端nginx:ip192.168.6.36,192.168.6.205都部署wordpress,并使用相同的数据库1、在后端的两个wordpress上配置rsync+inotify,两服务器都开启rsync服务,并且通过inotify分别向对方同步数据下面配置192.168.6.205这台服务器vim/etc/rsyncd.confuid=nginxgid=nginxport=873ho

跨域是开发中经常会遇到的一个场景,也是面试中经常会讨论的一个问题。掌握常见的跨域解决方案及其背后的原理,不仅可以提高我们的开发效率,还能在面试中表现的更加

nginx部署react刷新404的解决办法:1、修改Nginx配置为“server {listen 80;server_name https://www.xxx.com;location / {root xxx;index index.html index.htm;...}”;2、刷新路由,按当前路径去nginx加载页面即可。

linux版本:64位centos6.4nginx版本:nginx1.8.0php版本:php5.5.28&php5.4.44注意假如php5.5是主版本已经安装在/usr/local/php目录下,那么再安装其他版本的php再指定不同安装目录即可。安装php#wgethttp://cn2.php.net/get/php-5.4.44.tar.gz/from/this/mirror#tarzxvfphp-5.4.44.tar.gz#cdphp-5.4.44#./configure--pr

nginx禁止访问php的方法:1、配置nginx,禁止解析指定目录下的指定程序;2、将“location ~^/images/.*\.(php|php5|sh|pl|py)${deny all...}”语句放置在server标签内即可。


Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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

Atom editor mac version download
The most popular open source editor