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

相关推荐
Joy T33 分钟前
从 Nginx 到 Gateway 再到微服务:一次 AI 请求的完整链路
nginx·微服务·gateway·controller·路由转发·ai service·接口错误排查
小匠石钧知1 小时前
07_在k8s集群中安装ingress-nginx
nginx·容器·kubernetes·ingress
百度一下吧13 小时前
Nginx 使用手册:从安装配置到实战部署
运维·nginx
JAVA面经实录9171 天前
Java高级后端 · 全套面试通关手册(Nginx)
java·nginx·面试
二喵❥(^_-)1 天前
接口测试流程的三个核心环节
开发语言·lua
lpfasd1233 天前
WinSW在Win7上失败真相-实测与修复
windows·nginx
lpfasd1233 天前
nginx服务化五套方案原理解剖与选型
运维·nginx
ue星空3 天前
ALS V4 中文拆解手册
运维·nginx
吴声子夜歌4 天前
Nginx应用与运维——Nginx集群负载与配置管理(二)
运维·nginx
cpolar技术支持4 天前
外网测试机的异常传不回内网?自托管 Sentry,用 cpolar 打通错误上报链路
python·nginx·docker·cpolar·sentry