在Openresty中使用lua语言向请求浏览器返回请求头User-Agent里边的值

可以参考《Linux学习之Ubuntu 20.04在https://openresty.org下载源码安装Openresty 1.19.3.1,使用systemd管理OpenResty服务》安装Openresty。

然后把下边的内容写入到openresty配置文件/usr/local/openresty/nginx/conf/nginx.conf(根据实际情况进行选择文件):

bash 复制代码
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location /lua {
            default_type text/html;
            content_by_lua '
ngx.say("User-Agent: ",ngx.req.get_headers()["User-Agent"])
';
        }
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

然后sudo openresty启动openresty。

最后在浏览器里边输入ip/lua就可以显示出来效果了。

此文章为10月Day 10学习笔记,内容来源于极客时间《Nginx 核心知识 150 讲》

相关推荐
好好风格3 小时前
宝塔面板 HTTPS 端口证书不生效排查记录
linux·运维·nginx
宋冠巡4 小时前
OpenEuler 系统下 Nginx 安装配置与管理指南(基于 OpenEuler 22.03 LTS SP4)
nginx·openeuler
x***r1515 小时前
Postman-win64-7.2.2-Setup安装步骤详解(附API接口测试与参数配置教程)
开发语言·lua
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ5 小时前
nginx部署教程
运维·网络·nginx
liulilittle7 小时前
麻将牌堆渲染(Lua)
开发语言·lua
我是一颗柠檬7 小时前
【Redis】事务与Lua脚本Day7(2026年)
数据库·redis·后端·lua·database
tongluowan0079 小时前
负载均衡之硬件与软件层面的异同
运维·nginx·负载均衡·f5
宋冠巡10 小时前
华为云开发桌面OpenEuler搭建Nginx服务器实操记录
服务器·nginx·华为云
FFZero110 小时前
[mpv插件系统] (一) Lua 闭包与上值 — 从概念到 C API
c语言·junit·lua
zz07232014 小时前
Lua 脚本
lua·脚本语言·redis+lua