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

相关推荐
q***65694 小时前
Nginx反向代理出现502 Bad Gateway问题的解决方案
运维·nginx·gateway
NGINX开源社区6 小时前
将部署从 NGINX Ingress Controller 迁移至 NGINX Gateway Fabric
nginx·gateway·fabric
w***15319 小时前
若依部署Nginx和Tomcat
运维·nginx·tomcat
q***040514 小时前
Nginx 缓存清理
运维·nginx·缓存
这人很懒没留下什么21 小时前
SpringBoot2.7.4整合Oauth2
开发语言·lua
苹果醋31 天前
VueX(Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式)
java·运维·spring boot·mysql·nginx
q***3751 天前
Nginx如何实现 TCP和UDP代理?
tcp/ip·nginx·udp
q***96581 天前
Nginx作用以及应用场景
运维·nginx
q***31831 天前
Nginx搭建负载均衡
运维·nginx·负载均衡
q***82911 天前
Nginx中$http_host、$host、$proxy_host的区别
运维·nginx·http