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

相关推荐
徐小黑ACG8 小时前
nginx配置文件
linux·服务器·nginx
考虑考虑10 小时前
nginx打印请求日志
运维·后端·nginx
盟道科技20 小时前
Redis+Lua 实现滑动窗口限流:从固定窗口踩坑到生产可用的完整方案
redis·junit·lua
Rain的Java大神之路1 天前
JavaWeb开发如何解决跨域问题
java·前端·后端·nginx·web安全·面试·运维开发
Lsetea2 天前
Nginx报SSL_CTX_use_PrivateKey failed:证书与私钥不匹配怎么排查
nginx·https·ssl证书·openssl·私钥
!chen2 天前
客户环境 Nginx 配置流式报表与超时排查
运维·nginx
大喵桑丶2 天前
AlmaLinux 9 上基于 PostgreSQL + Nginx 部署高可用轻量级 Gitea 服务
nginx·postgresql·gitea
我最爱吃鱼香茄子3 天前
【踩坑实录】若依Vue宝塔部署:验证码空白、反复提示登录状态已过期|完整排错
nginx·宝塔·若依·ruoyi‑vue·登录状态已过期
dazhong20123 天前
Docker 进阶篇(二) Docker Compose 部署 Nginx 实践
nginx·docker·容器
笑梦无境3 天前
nginx安装(5)
linux·运维·nginx