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

相关推荐
就叫飞六吧9 天前
基于keepalived、vip实现高可用nginx (centos)
python·nginx·centos
WIN赢9 天前
PostMan使用
测试工具·lua·postman
小生云木9 天前
Linux离线编译安装nginx
linux·运维·nginx
Cat God 00710 天前
项目上线(若依前后分离版)
java·nginx
多多*10 天前
计算机网络期末 网络基础概述
运维·服务器·网络·数据库·计算机网络·oracle·lua
10 天前
Lua复习之何为闭包
开发语言·unity·游戏引擎·lua·交互
码上库利南10 天前
详解Redis的LUA脚本、管道 (Pipelining)、事务事务 (Transactions)
数据库·redis·lua
婷儿z10 天前
LVS负载均衡群集:Nginx+Tomcat负载均衡群集
nginx·负载均衡·lvs
愿做无知一猿10 天前
【Docker】docker-compose中的nginx为何突然访问不到服务了?
nginx·docker·容器
当归102410 天前
Nginx与Tomcat:谁更适合你的服务器?
服务器·nginx·tomcat