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

相关推荐
w_t_y_y1 小时前
Nginx Plus
运维·数据库·nginx
老姚---老姚9 小时前
Nginx Location 匹配优先级详解
nginx
露临霜9 小时前
Docker安装nginx
nginx·docker·容器
AlianNiew9 小时前
Nginx 反向代理 403 问题复盘
nginx
早川91915 小时前
Nginx反向代理
nginx
IMPYLH17 小时前
Lua 的 xpcall 函数
开发语言·笔记·后端·游戏引擎·lua
云和数据.ChenGuang17 小时前
运维工程师技术之nginx搭配php
运维·nginx·php·运维技术·数据库运维工程师·运维教程
我要精通C++17 小时前
从源码看nginx的缓存功能
运维·nginx·缓存
一帘忧梦1 天前
linux 系统rcs脚本启动
linux·运维·lua
阿海5741 天前
卸载nginx的shell脚本
linux·nginx