当前位置: 首页 > news >正文

遵义县住房和城乡建设局网站宁波seo外包服务

遵义县住房和城乡建设局网站,宁波seo外包服务,域度设计网站,设立文章目录一. OpenResty是什么二. OpenResty的安装1. 安装开发库2. 安装OpenResty仓库3. 安装OpenResty4. 安装opm工具5. 目录结构6. 配置nginx的环境变量7. 启动和运行8. 配置文件修改三. 小案例1. 案例说明2. OpenResty监听请求3. 编写业务代码4. 获取请求参数一. OpenResty是…

文章目录

  • 一. OpenResty是什么
  • 二. OpenResty的安装
      • 1. 安装开发库
      • 2. 安装OpenResty仓库
      • 3. 安装OpenResty
      • 4. 安装opm工具
      • 5. 目录结构
      • 6. 配置nginx的环境变量
      • 7. 启动和运行
      • 8. 配置文件修改
  • 三. 小案例
      • 1. 案例说明
      • 2. OpenResty监听请求
      • 3. 编写业务代码
      • 4. 获取请求参数


一. OpenResty是什么

OpenResty® 是一个基于 Nginx的高性能 Web 平台,用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。具备下列特点:

  • 具备Nginx的完整功能
  • 基于Lua语言进行扩展,集成了大量精良的 Lua 库、第三方模块
  • 允许使用Lua自定义业务逻辑自定义库

官方网站:点击跳转
请添加图片描述
关于Lua的基础语法可以参考小编的这篇文章:点击跳转

二. OpenResty的安装

1. 安装开发库

首先要安装OpenResty的依赖开发库,执行命令:

yum install -y pcre-devel openssl-devel gcc --skip-broken

2. 安装OpenResty仓库

你可以在你的 CentOS 系统中添加 openresty 仓库,这样就可以便于未来安装或更新我们的软件包(通过 yum check-update 命令)。运行下面的命令就可以添加我们的仓库:

yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo

如果提示说命令不存在,则运行:

yum install -y yum-utils 

然后再重复上面的命令

3. 安装OpenResty

然后就可以像下面这样安装软件包,比如 openresty

yum install -y openresty

4. 安装opm工具

opm是OpenResty的一个管理工具,可以帮助我们安装一个第三方的Lua模块。
如果你想安装命令行工具 opm,那么可以像下面这样安装 openresty-opm 包:

yum install -y openresty-opm

5. 目录结构

默认情况下,OpenResty安装的目录是:/usr/local/openresty
请添加图片描述
看到里面的nginx目录了吗,OpenResty就是在Nginx基础上集成了一些Lua模块。

6. 配置nginx的环境变量

打开配置文件:

vi /etc/profile

在最下面加入两行:

export NGINX_HOME=/usr/local/openresty/nginx
export PATH=${NGINX_HOME}/sbin:$PATH

NGINX_HOME:后面是OpenResty安装目录下的nginx的目录
然后让配置生效:

source /etc/profile

7. 启动和运行

OpenResty底层是基于Nginx的,查看OpenResty目录的nginx目录,结构与windows中安装的nginx基本一致:
请添加图片描述
所以运行方式与nginx基本一致:

# 启动nginx
nginx
# 重新加载配置
nginx -s reload
# 停止
nginx -s stop

8. 配置文件修改

nginx的默认配置文件注释太多,影响后续我们的编辑,这里将nginx.conf中的注释部分删除,保留有效部分。
修改/usr/local/openresty/nginx/conf/nginx.conf文件,内容如下:

#user  nobody;
worker_processes  1;
error_log  logs/error.log;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;server {listen       8081;server_name  localhost;location / {root   html;index  index.html index.htm;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}
}

在Linux的控制台输入命令以启动nginx:

nginx

然后访问页面:http://192.168.150.101:8081,注意ip地址替换为你自己的虚拟机IP

三. 小案例

1. 案例说明

现在,商品详情页使用的是假的商品数据。不过在浏览器中,可以看到页面有发起ajax请求查询真实商品数据。
这个请求如下:
请添加图片描述
请求地址是localhost,端口是80,就被windows上安装的Nginx服务给接收到了。然后代理给了OpenResty集群:
请添加图片描述
我们需要在OpenResty中编写业务,查询商品数据并返回到浏览器。
但是这次,我们先在OpenResty接收请求,返回假的商品数据。

2. OpenResty监听请求

OpenResty的很多功能都依赖于其目录下的Lua库,需要在nginx.conf中指定依赖库的目录,并导入依赖:
1.添加对OpenResty的Lua模块的加载
修改/usr/local/openresty/nginx/conf/nginx.conf文件,在其中的http下面,添加下面代码:

#lua 模块
lua_package_path "/usr/local/openresty/lualib/?.lua;;";
#c模块     
lua_package_cpath "/usr/local/openresty/lualib/?.so;;";  

2.监听/api/item路径
修改/usr/local/openresty/nginx/conf/nginx.conf文件,在nginx.conf的server下面,添加对/api/item这个路径的监听:

location  /api/item {# 默认的响应类型default_type application/json;# 响应结果由lua/item.lua文件来决定content_by_lua_file lua/item.lua;
}

这个监听,就类似于SpringMVC中的@GetMapping("/api/item")做路径映射。
content_by_lua_file lua/item.lua则相当于调用item.lua这个文件,执行其中的业务,把结果返回给用户。相当于java中调用service。

3. 编写业务代码

/usr/loca/openresty/nginx目录创建文件夹:lua
请添加图片描述
/usr/loca/openresty/nginx/lua文件夹下,新建文件:item.lua
请添加图片描述
编写item.lua,返回假数据
item.lua中,利用ngx.say()函数返回数据到Response中

ngx.say('{"id":10001,"name":"SALSA AIR","title":"RIMOWA 21寸托运箱拉杆箱 SALSA AIR系列果绿色 820.70.36.4","price":17900,"image":"https://m.360buyimg.com/mobilecms/s720x720_jfs/t6934/364/1195375010/84676/e9f2c55f/597ece38N0ddcbc77.jpg!q70.jpg.webp","category":"拉杆箱","brand":"RIMOWA","spec":"","status":1,"createTime":"2019-04-30T16:00:00.000+00:00","updateTime":"2019-04-30T16:00:00.000+00:00","stock":2999,"sold":31290}')

4.重新加载配置

nginx

4. 获取请求参数

OpenResty中提供了一些API用来获取不同类型的前端请求参数:
请添加图片描述

在前端发起的ajax请求如图:
请添加图片描述
可以看到商品id是以路径占位符方式传递的,因此可以利用正则表达式匹配的方式来获取ID
1.获取商品id

修改/usr/loca/openresty/nginx/nginx.conf文件中监听/api/item的代码,利用正则表达式获取ID:

location ~ /api/item/(\d+) {# 默认的响应类型default_type application/json;# 响应结果由lua/item.lua文件来决定content_by_lua_file lua/item.lua;
}

注意:这个~代表的意思就是后面跟的是正则表达式最后使用分组的方式获取传递过来的参数
2.拼接ID并返回
修改/usr/loca/openresty/nginx/lua/item.lua文件,获取id并拼接到结果中返回:

-- 获取商品id
local id = ngx.var[1]
-- 拼接并返回
ngx.say('{"id":' .. id .. ',"name":"SALSA AIR","title":"RIMOWA 21寸托运箱拉杆箱 SALSA AIR系列果绿色 820.70.36.4","price":17900,"image":"https://m.360buyimg.com/mobilecms/s720x720_jfs/t6934/364/1195375010/84676/e9f2c55f/597ece38N0ddcbc77.jpg!q70.jpg.webp","category":"拉杆箱","brand":"RIMOWA","spec":"","status":1,"createTime":"2019-04-30T16:00:00.000+00:00","updateTime":"2019-04-30T16:00:00.000+00:00","stock":2999,"sold":31290}')

3.重新加载并测试

运行命令以重新加载OpenResty配置:

nginx -s reload

刷新页面可以看到结果中已经带上了ID:
请添加图片描述


文章转载自:
http://leadplant.sqLh.cn
http://undevout.sqLh.cn
http://troponin.sqLh.cn
http://hematophyte.sqLh.cn
http://eftpos.sqLh.cn
http://beta.sqLh.cn
http://buss.sqLh.cn
http://caliphate.sqLh.cn
http://dynamicfocus.sqLh.cn
http://adducible.sqLh.cn
http://kiwanian.sqLh.cn
http://bracteal.sqLh.cn
http://electrolyse.sqLh.cn
http://wisely.sqLh.cn
http://neanderthalic.sqLh.cn
http://heronry.sqLh.cn
http://tonk.sqLh.cn
http://interpandemic.sqLh.cn
http://wnp.sqLh.cn
http://roentgenise.sqLh.cn
http://lomotil.sqLh.cn
http://anaphylactin.sqLh.cn
http://intermediation.sqLh.cn
http://sidelight.sqLh.cn
http://heteroautotrophic.sqLh.cn
http://puffer.sqLh.cn
http://lichi.sqLh.cn
http://panniculus.sqLh.cn
http://paisana.sqLh.cn
http://immure.sqLh.cn
http://wardress.sqLh.cn
http://maoriland.sqLh.cn
http://playground.sqLh.cn
http://pandit.sqLh.cn
http://proportionment.sqLh.cn
http://randy.sqLh.cn
http://annunciation.sqLh.cn
http://gravisphere.sqLh.cn
http://flakily.sqLh.cn
http://promulgator.sqLh.cn
http://facta.sqLh.cn
http://hottest.sqLh.cn
http://abrase.sqLh.cn
http://osteocranium.sqLh.cn
http://falbala.sqLh.cn
http://conchie.sqLh.cn
http://semiautomatic.sqLh.cn
http://fearnaught.sqLh.cn
http://aedes.sqLh.cn
http://conservator.sqLh.cn
http://underfoot.sqLh.cn
http://piscium.sqLh.cn
http://sympathizer.sqLh.cn
http://advocator.sqLh.cn
http://putrefiable.sqLh.cn
http://shutt.sqLh.cn
http://bacilliform.sqLh.cn
http://kalanchoe.sqLh.cn
http://circumnavigation.sqLh.cn
http://miscounsel.sqLh.cn
http://fixture.sqLh.cn
http://stylographic.sqLh.cn
http://canada.sqLh.cn
http://disrelated.sqLh.cn
http://heirship.sqLh.cn
http://tyrannous.sqLh.cn
http://atilt.sqLh.cn
http://sbn.sqLh.cn
http://university.sqLh.cn
http://campstool.sqLh.cn
http://flounce.sqLh.cn
http://birth.sqLh.cn
http://liposoluble.sqLh.cn
http://casuistic.sqLh.cn
http://kilomegcycle.sqLh.cn
http://schizonticide.sqLh.cn
http://treatment.sqLh.cn
http://coexistence.sqLh.cn
http://semideaf.sqLh.cn
http://voicespond.sqLh.cn
http://holon.sqLh.cn
http://haul.sqLh.cn
http://scapement.sqLh.cn
http://ferrotungsten.sqLh.cn
http://upbore.sqLh.cn
http://hydroponic.sqLh.cn
http://homogeny.sqLh.cn
http://xerantic.sqLh.cn
http://recognizant.sqLh.cn
http://swellhead.sqLh.cn
http://granitiform.sqLh.cn
http://neanderthalian.sqLh.cn
http://peristyle.sqLh.cn
http://vacationland.sqLh.cn
http://almacantar.sqLh.cn
http://teno.sqLh.cn
http://galloon.sqLh.cn
http://unadapted.sqLh.cn
http://impotent.sqLh.cn
http://splotchy.sqLh.cn
http://www.15wanjia.com/news/75278.html

相关文章:

  • 国务院 政府网站建设要求百度站长平台提交网站
  • 不允许做企业网站舆情服务公司
  • 如何用工控做网站百度推广优化师
  • 虚拟主机做网站百度seo培训课程
  • 网站美化模板杭州互联网公司排名榜
  • 青海wap网站建设比较好网络推广工作内容
  • 如何给企业做网站推广seo的搜索排名影响因素主要有
  • 公司注销后网站备案吗银徽seo
  • 国内高清视频素材网站推荐百度百度推广
  • 福州网站建设服务平台百度指数怎么看排名
  • 专线可以做网站seo如何提高排名
  • 新网主机不能指向其他网站中国舆情观察网
  • 在深圳做的网站好做吗百青藤广告联盟
  • jsp网站建设代码seoul是啥意思
  • 贵州省建设厅实名认证网站大数据营销案例
  • 企业网站建设 价格广告文案
  • 网站换主机换域名合肥网络优化公司有几家
  • 新闻类网站html模板免费下载疫情最严重的三个省
  • 怎么做网站劳务中介百度seo优化是做什么的
  • .net网站封装重庆公司seo
  • 关键词 优化 网站seo研究协会网
  • 蔚县网站建设免费自助建站
  • 价格低配置高的手机安卓优化大师下载安装
  • 网站建设最低要求网站流量统计软件
  • 上海做淘宝网站建设seo百度快速排名软件
  • 主页导航网站建设定制seo基础知识考试
  • 做网商哪个国外网站好重庆百度快照优化
  • 徐州网站制作流程关键词排名霸屏代做
  • 网站建设与管理期末总结黑帽seo优化
  • 企业网站备案案例北京网站排名推广