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

相关推荐
Hello-Mr.Wang2 小时前
nginx与openSSL版本不兼容问题
linux·服务器·nginx
一直在学习的小白~7 小时前
Nginx 服务器,Apache 服务器,IIS 服务器的区别
服务器·nginx·apache
他不爱吃香菜7 小时前
Nginx正向代理HTTPS配置指南(仅供参考)
网络·网络协议·tcp/ip·nginx·http·https·信息与通信
羑悻的小杀马特7 小时前
蓝耘携手通义万象 2.1 图生视频:开启创意无限的共享新时代
服务器·nginx·音视频·ai大模型·蓝耘·通义万象 2.1
若云止水7 小时前
Ubuntu 下 nginx-1.24.0 源码分析 - ngx_core_module
数据库·nginx·ubuntu
若云止水18 小时前
ngx_openssl_create_conf
nginx
m0_7482404419 小时前
Nginx 配置 SSL(HTTPS)详解
nginx·https·ssl
Jiude19 小时前
💻 从买服务器到搞定一切:帮朋友搭建企业技术基建!(二)🚀
后端·nginx·架构
国家级著名CV工程师20 小时前
从零开始-DevOps自动化部署教程
java·运维·服务器·nginx·自动化·devops
川石课堂软件测试20 小时前
涨薪技术|Kubernetes(k8s)之Service服务类型
mysql·nginx·docker·容器·贪心算法·kubernetes·tomcat