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

相关推荐
J_liaty8 小时前
Nginx核心功能解析与实战指南
运维·nginx·负载均衡
softshow10269 小时前
Nginx 零改造在 Kubernetes 中用 iptables 劫持流量到 Envoy Sidecar
运维·nginx·kubernetes
曲幽9 小时前
从安装到上线:一份 Nginx 实战指南,让你的 Web 应用稳建安全
python·nginx·flask·fastapi·web·gunicorn·uvicorn
成都犀牛10 小时前
Ubuntu配置nginx
linux·nginx·ubuntu
Dxy123931021612 小时前
Nginx中的worker_processes如何设置:从“盲目填数”到“精准调优”
运维·nginx
oMcLin12 小时前
如何在 Debian 11 上通过优化 Nginx 与 PHP‑FPM,提高大流量网站的稳定性与响应速度?
nginx·debian·php
liuhaikang13 小时前
鸿蒙高性能动画库——lottie-turbo
java·开发语言·nginx
一叶星殇14 小时前
ASP.NET Core 后端如何通过 Nginx 获取真实客户端 IP 完整指南
服务器·tcp/ip·nginx
fredricen14 小时前
Openwrt21.02实现文件列表展示并下载文件
lua·openwrt
Dxy12393102161 天前
Nginx原生配置介绍
运维·nginx