在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 讲》

相关推荐
凯子坚持 c2 小时前
Docker实战深度解析:从Nginx部署到私有镜像仓库管理
nginx·docker·eureka
lwhdjbcjdjd7 小时前
Nginx与Tomcat协作处理流程及数据流向
运维·nginx·tomcat
AI_56787 小时前
接口测试“零基础通关“:Postman从入门到自动化测试实战指南
开发语言·lua
kewu89718 小时前
Postman 完全入门教程
测试工具·lua·postman
大大水瓶9 小时前
Nginx学习
学习·nginx·dubbo
百***256110 小时前
Nginx作用以及应用场景
运维·nginx
小徐敲java10 小时前
window使用phpStudy在nginx部署前端测试
运维·前端·nginx
Crazy________10 小时前
38nginx四层负载均衡配置,和动静分离解析
linux·运维·nginx·负载均衡
百***359411 小时前
如何在树莓派部署Nginx并实现无公网ip远程访问内网制作的web网站
前端·tcp/ip·nginx
roman_日积跬步-终至千里13 小时前
【Nginx】Nginx 多协议负载均衡实战:StarRocks 与 MinIO 代理配置全解析
运维·nginx·负载均衡