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

相关推荐
智购科技自动售卖机厂家6 小时前
从STM32到RK3588——自动售货机嵌入式主控方案的架构演进~YH
stm32·嵌入式硬件·物联网·架构·lua·零售·symfony
是良辰9 小时前
Docker Nginx HTTPS 自签证书部署完整操作文档
nginx·docker·https
難釋懷13 小时前
Nginx外置缓存-redis2-nginx-module
nginx·缓存·junit
凡尘——雨落凡尘1 天前
PHP 线上十大隐形故障复盘:90% 的网站卡顿、502、雪崩,都是这些细节导致的
redis·nginx·php·session
Deryck_德瑞克1 天前
【Nginx】配置差异分析
服务器·前端·nginx
学者猫头鹰2 天前
Nginx零基础学习手册
nginx
qq_370773092 天前
Unity游戏xLua逆向实战:从SO版本识别到Lua脚本热替换全链路
游戏·unity·lua
難釋懷2 天前
Nginx外置缓存-nginx + memcached
nginx·缓存·memcached
難釋懷3 天前
Nginx外置缓存-nginx + redis
redis·nginx·缓存
☆凡尘清心☆3 天前
CentOS Stream 9 专用 LNMP 全自动一键脚本
linux·运维·mysql·nginx·lnmp·centos stream 9