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

相关推荐
YJQ99672 小时前
Nginx防盗链及HTTPS:保护网站内容安全与加密传输
nginx·安全·https
猿java5 小时前
什么是Nginx?它有哪些应用场景?
java·nginx·面试
绝顶少年5 小时前
反向代理、负载均衡与镜像流量:原理剖析、区别对比及 Nginx 配置实践
运维·nginx·负载均衡
天下·第二19 小时前
【Nginx】负载均衡配置详解
运维·nginx·负载均衡
Hello.Reader21 小时前
洞悉 NGINX ngx_http_access_module基于 IP 的访问控制实战指南
tcp/ip·nginx·http
PWRJOY1 天前
在 Ubuntu 24.04 系统上安装和管理 Nginx
linux·nginx·ubuntu
时光话1 天前
Lua 第12部分 日期和时间
开发语言·lua
菠萝崽.1 天前
安装docker,在docker上安装mysql,docker上安装nginx
java·mysql·nginx·docker·软件工程·springboot·开发
java1234_小锋2 天前
什么是WebSocket?NGINX如何支持WebSocket协议?
websocket·网络协议·nginx
Hello.Reader2 天前
基于 Nginx 的 WebSocket 反向代理实践
运维·websocket·nginx