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

相关推荐
吴懿不在12 小时前
apisix~OpenResty各阶段的介绍
openresty
donoot15 小时前
一次 Nginx 502 问题的深度排查:从 SELinux 到容器网络
nginx·docker·selinux·反向代理·502 bad gateway
難釋懷20 小时前
Nginx浏览器强制缓存
运维·nginx·缓存
ITKEY_1 天前
macOS brew 安装的nginx 文件在哪里?
运维·nginx·macos
Felix-lxd1 天前
Ubuntu 22.04 配置 Nginx
linux·nginx·ubuntu
孫治AllenSun1 天前
【Nginx】配置参数和使用案例
运维·nginx
brave_zhao2 天前
nginx的进程架构
java·学习·nginx
jieyucx2 天前
从零搭建 Zabbix 监控系统:7.0 版本原理与 Nginx 生产部署实操
运维·nginx·zabbix
jieyucx2 天前
Shell实战:无限循环监控Nginx服务,异常自动重启+双份日志留存
linux·运维·nginx·自动化·shell
zhz52142 天前
Nginx + OpenSSL 自签名证书配置:优劣总结
运维·nginx