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

相关推荐
J2虾虾4 小时前
Caddy GO语言写的Nginx平替
nginx·caddy
2601_949817724 小时前
Nginx权限问题详解及解决方案
运维·nginx
liqianpin110 小时前
Nginx WebSocket 长连接及数据容量配置
运维·websocket·nginx
chxii13 小时前
Nginx 反向代理详解
运维·nginx
.柒宇.13 小时前
构建 keepalived + LVS + Nginx + DNS + NFS 的高可用集群系统
运维·nginx·lvs
chxii13 小时前
配置Nginx 的 Keepalive 提升每秒查询率(QPS)
前端·nginx
01传说1 天前
nginx部署教程实战
运维·nginx
吹晚风吧1 天前
解决vite打包,base配置前缀,nginx的dist包找不到资源
服务器·前端·nginx
chxii1 天前
Nginx 正则 location 指令匹配客户端请求的 URI
前端·nginx
難釋懷1 天前
OpenResty封装Redis工具
redis·junit·openresty