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

相关推荐
i***48611 小时前
Nginx中$http_host、$host、$proxy_host的区别
运维·nginx·http
o***74171 小时前
【Nginx 】Nginx 部署前端 vue 项目
前端·vue.js·nginx
z***39622 小时前
Nginx中如何配置WebSocket代理?
运维·websocket·nginx
忆林5202 小时前
请求服务任何资源(静态/动态)都返回主页数据?多nginx针对单域名的多服务配置
运维·nginx
q***4644 小时前
离线安装 Nginx
运维·数据库·nginx
Roottest12346 小时前
nginx+tomcat+mysql操作实验
运维·mysql·nginx
z***y8628 小时前
后端服务限流配置,Nginx与Spring Cloud Gateway
java·服务器·nginx
e***985714 小时前
Nginx搭建负载均衡
运维·nginx·负载均衡
q***74817 小时前
Nginx环境安装
运维·nginx
梁正雄1 天前
linux服务-nginx原理与安装-1
linux·运维·nginx