游戏人生
首页
(current)
GameDevTools
登陆
|
注册
个人中心
注销
OpenResty 教程
OpenResty 简介
OpenResty Ubuntu安装
OpenResty 第一个例子
OpenResty 目录结构
OpenResty 启动和关闭
OpenResty 热重启
OpenResty 执行lua代码
OpenResty 执行lua文件
OpenResty 网站服务
OpenResty 访问指定网页
OpenResty 多网页网站
OpenResty 日志
OpenResty 流水线
OpenResty ip黑名单
OpenResty 反向代理
OpenResty 负载均衡
<< OpenResty 网站服务
OpenResty 多网页网站 >>
OpenResty 访问指定网页
在上一篇,用OpenResty做了简单的网站,返回了一个简单的网页。 但是一个网站肯定不会只有一个网页,怎么也要有2个网页。 但是我发现,不管我输入什么网址,总是返回同一个网页。 例如访问 ```shell http://localhost:8080/about.html ``` 返回的还是这个网页  正常来说,不是要报个404 找不到对象吗? ------------ #### 1. 原因解析 之所以出现上面的问题,是因为在 `conf/nginx.conf` 这个配置文件中,将任意网址,都指向了 `test.lua` 。 配置文件内容如下 ```shell worker_processes 1; error_log logs/error.log; events { worker_connections 1024; } http { server { listen 8080; location / { default_type text/html; content_by_lua_file service/http/test.lua; } } } ``` 注意这一行 ```shell location / ``` 这个` /` 就表示,所有的网址 都按照中括号里面的配置进行处理。 ------------ #### 2.修改配置,根据网址路径 执行不同的lua脚本 在配置文件中,为 `about.html` 这个网址,新增一段代码: ```shell worker_processes 1; error_log logs/error.log; events { worker_connections 1024; } http { server { listen 8080; location / { default_type text/html; content_by_lua_file service/http/test.lua; } location /about.html { default_type text/html; content_by_lua_file service/http/about.lua; } } } ``` 这里将 `about.html`,丢给了 `service/http/about.lua` 处理。 ------------ #### 3. 创建 lua 脚本。 创建 `service/http/about.lua`,粘贴以下代码: ```shell local sHtmlCode=[[<html> <head> <title>about ThisisGame</title> </head> <body> <p>ThisisGame powerd by OpenResty</p> </body> </html>]] ngx.say(sHtmlCode) ``` ------------ #### 4. 重启OpenResty服务 重启 OpenResty: ```shell nginx -p `pwd`/ -s reload ``` ------------ #### 5. 访问网站 打开浏览器,访问下面地址 ```shell http://localhost:8080/about.html ```  可以看到 网页正常显示了 然后再访问 ```shell http://localhost:8080 ```  是不同的网页。 这样访问不同的链接,就打开了不同的网页。 现在有2个网页了,算是一个网站了。
<< OpenResty 网站服务
OpenResty 多网页网站 >>
提交
5ec8e919f9046103c7132d0d