当前位置:   article > 正文

openresty 安装与使用_openresty使用

openresty使用

一、openresty的安装

  1. 下载源码

openresty download

  1. 安装依赖

  1. apt-get install libpcre3-dev \
  2. libssl-dev perl make build-essential curl
  1. 编译

  1. tar -xzvf openresty-VERSION.tar.gz
  2. # --without-http_redis2_module 将不能使用http_redis2模块
  3. ./configure --prefix=/usr/local/openresty \
  4. --with-luajit \
  5. --without-http_redis2_module \
  6. --with-http_iconv_module \
  7. --with-http_postgres_module
  8. make
  9. make install
  1. 启动

cd /usr/local/openresty/nginx/sbin

./nginx

访问http://ip

二、应用

  1. httpredis

httpredis只能读取redis

vim conf/nginx-httpredis.conf

  1. worker_processes 1;
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. include mime.types;
  7. default_type application/octet-stream;
  8. sendfile on;
  9. keepalive_timeout 65;
  10. server {
  11. listen 80;
  12. server_name www.cpf.com;
  13. root html;
  14. index index.html;
  15. location / {
  16. default_type text/plain;
  17. set $redis_key "m";
  18. redis_pass 127.0.0.1:6379;
  19. error_page 404 = @fetch;
  20. }
  21. location @fetch {
  22. root html;
  23. }
  24. }
  25. }

在html 目录下创建一个1.html文件,内容为:"i am 1.html"

在redis中无key "m"时:

在redis中有key "m"时:

  1. httpredis2module

httpredis2module可以读写redis

vim conf/nginx-httpredis2module.conf

  1. worker_processes 1;
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. include mime.types;
  7. default_type application/octet-stream;
  8. sendfile on;
  9. keepalive_timeout 65;
  10. server {
  11. listen 80;
  12. server_name www.cpf.com;
  13. root html;
  14. index index.html;
  15. location /get {
  16. set_unescape_uri $key 'n';
  17. redis2_query get $key;
  18. redis2_pass 127.0.0.1:6379;
  19. }
  20. location /set {
  21. set_unescape_uri $key 'n';
  22. redis2_query set $key 'nValue';
  23. redis2_pass 127.0.0.1:6379;
  24. }
  25. }
  26. }
  1. lua redis

  1. 设置获取redis中的key

vim conf/nginx-openresty-lua-redis.conf

  1. worker_processes 1;
  2. error_log logs/error.log;
  3. events {
  4. worker_connections 1024;
  5. }
  6. http {
  7. include mime.types;
  8. default_type application/octet-stream;
  9. sendfile on;
  10. keepalive_timeout 65;
  11. server {
  12. listen 8082;
  13. server_name localhost;
  14. location / {
  15. default_type text/html;
  16. content_by_lua_file /usr/local/openresty/nginx/lua/lua-openresty-redis.lua;
  17. }
  18. }
  19. }

vim lua/lua-openresty-redis.lua

  1. -- 引用resty的redis
  2. local redis = require "resty.redis";
  3. local red = redis:new();
  4. -- 连接redis
  5. local ok,err = red:connect("127.0.0.1",6379);
  6. if not ok then
  7. ngx.say("faild to connect",err);
  8. return
  9. end
  10. ok,err = red:set("dKey","dValue");
  11. if not ok then
  12. ngx.say("failed to set dKey",err);
  13. return
  14. end
  15. ok,err = red:get("dKey")
  16. if not ok then
  17. ngx.say("dKey is null")
  18. else
  19. ngx.say("dKey's value is :"..ok)
  20. end
  21. return
  1. lua获取查询参数

vim conf/nginx-param.c

  1. worker_processes 1;
  2. error_log logs/error.log;
  3. events {
  4. worker_connections 1024;
  5. }
  6. http {
  7. include mime.types;
  8. default_type application/octet-stream;
  9. server {
  10. listen 8081;
  11. location / {
  12. default_type text/html;
  13. content_by_lua_file /usr/local/openresty/nginx/lua/lua-http-param.lua;
  14. }
  15. }
  16. }

vim lua/lua-http-param.lua

  1. -- 获取get请求的参数
  2. local arg = ngx.req.get_uri_args();
  3. for k,v in pairs(arg)
  4. do
  5. ngx.say("key:",k," value:",v);
  6. end

./sbin/nginx -p ./ -c conf/nginx-param.conf

  1. lua获取请求头参数

vim conf/nginx-param.conf

  1. worker_processes 1;
  2. error_log logs/error.log;
  3. events {
  4. worker_connections 1024;
  5. }
  6. http {
  7. include mime.types;
  8. default_type application/octet-stream;
  9. server {
  10. listen 8081;
  11. location / {
  12. default_type text/html;
  13. content_by_lua_file /usr/local/openresty/nginx/lua/lua-header-param.lua;
  14. }
  15. }
  16. }

vim lua/lua-header-param.lua

  1. local headers = ngx.req.get_headers();
  2. for k,v in pairs(headers)
  3. do
  4. ngx.say("[header] key:",k," value:",v);
  5. end

./sbin/nginx -p ./ -c conf/nginx-param.conf

  1. lua获取请求表单

vim conf/nginx-param.conf

  1. worker_processes 1;
  2. error_log logs/error.log;
  3. events {
  4. worker_connections 1024;
  5. }
  6. http {
  7. include mime.types;
  8. default_type application/octet-stream;
  9. server {
  10. listen 8081;
  11. location / {
  12. default_type text/html;
  13. content_by_lua_file /usr/local/openresty/nginx/lua/lua-post-kv-param.lua;
  14. }
  15. }
  16. }

vim lua/lua-post-kv-param.lua

  1. -- 获取post body kv参数
  2. -- 重要:读取body
  3. ngx.req.read_body();
  4. local postArgs = ngx.req.get_post_args();
  5. for k,v in pairs(postArgs)
  6. do
  7. ngx.say("[post] key:",k," value:",v);
  8. end

./sbin/nginx -p ./ -c conf/nginx-param.

  1. 读取全部body

vim conf/nginx-param.conf

  1. worker_processes 1;
  2. error_log logs/error.log;
  3. events {
  4. worker_connections 1024;
  5. }
  6. http {
  7. include mime.types;
  8. default_type application/octet-stream;
  9. server {
  10. listen 8081;
  11. location / {
  12. default_type text/html;
  13. content_by_lua_file /usr/local/openresty/nginx/lua/lua-post-body-param.lua;
  14. }
  15. }
  16. }

vim lua-post-body-param.lua

  1. -- 获取body体参数
  2. -- 所有获取body的操作,这个很重要
  3. ngx.req.read_body();
  4. local body = ngx.req.get_body_data();
  5. ngx.say(body);

./sbin/nginx -p ./ -c conf/nginx-param.

  1. nginx+lua+redis 限流

vim lua/ip-limit-access.lua

  1. ngx.log(ngx.INFO,"ip limit access");
  2. local redis = require "resty.redis";
  3. local red = redis:new();
  4. --链接redis
  5. red:connect("127.0.0.1",6379);
  6. -- 需要写链接成功的判断。
  7. --判断是否限流
  8. limit = red:get("limit");
  9. if limit == '1' then
  10. return ngx.exit(503);
  11. end
  12. inc = red:incr("testLimit");
  13. if inc <= 2 then
  14. red:expire("testLimit",1);
  15. else
  16. red:set("limit",1);
  17. red:expire("limit",10);
  18. end

vim conf/nginx-ip-limit.conf

  1. worker_processes 1;
  2. error_log logs/error.log debug;
  3. events {
  4. worker_connections 1024;
  5. }
  6. http {
  7. include mime.types;
  8. default_type application/octet-stream;
  9. server {
  10. listen 8083;
  11. location / {
  12. default_type text/html;
  13. access_by_lua_file /usr/local/openresty/nginx/lua/ip-limit-access.lua;
  14. log_by_lua_file /usr/local/openresty/nginx/lua/ip-limit-log.lua;
  15. proxy_pass http://localhost:8080/;
  16. }
  17. }
  18. }
  19. ~

启动上游服务器go run main.go

  1. package main
  2. import (
  3. "time"
  4. "github.com/gin-gonic/gin"
  5. )
  6. func main() {
  7. e := gin.New()
  8. e.GET("/", func(ctx *gin.Context) {
  9. ctx.String(200, time.Now().String())
  10. })
  11. e.Run()
  12. }

启动nginx: ./sbin/nginx -p ./ -c conf/nginx-ip-limit.conf

  1. nginx+lua +redis 防爬虫(ip黑名单)

爬虫种类:

  1. 善意的:baidu、google

  1. 恶意的:恶意窃取网站内容

防爬虫的方法:

  1. 限制user-agent: 非浏览器会带上这个头。如postman

  1. 限制ip

  1. 添加验证码

  1. 限制cookie

本次我们使用限制ip的方式:

在lua中有一个黑名单缓存,这个缓存定时去redis更新。nginx的访问ip在这个缓存中查询,如果查询到,则拒绝访问。

在redis中添加黑名单

vim lua/black-list-access.lua

  1. ngx.log(ngx.INFO,"black list");
  2. -- 获取nginx中的ip_black_list
  3. local ip_black_list = ngx.shared.ip_black_list;
  4. local last_update_time=ip_black_list:get("last_update_time");
  5. if last_update_time == nil or last_update_time < (ngx.now()-2) then
  6. local redis = require "resty.redis";
  7. local red = redis:new();
  8. local ok,err = red:connect("127.0.0.1",6379);
  9. if not ok then
  10. ngx.log(ngx.INFO,"connect error");
  11. else
  12. local local_black_list,err = red:smembers("ip_black_list");
  13. ip_black_list:flush_all();
  14. for k,v in pairs(local_black_list)
  15. do
  16. ip_black_list:set(v,true);
  17. end
  18. ip_black_list:set("last_update_time",ngx.now());
  19. end
  20. end
  21. local ip=ngx.var.remote_addr;
  22. ngx.log(ngx.INFO,"request ip is "..ip);
  23. -- 判断是否在黑名单
  24. if ip_black_list:get(ip) then
  25. return ngx.exit(503);
  26. end

vim conf/nginx-black-list.conf

  1. worker_processes 1;
  2. error_log logs/error.log debug;
  3. events {
  4. worker_connections 1024;
  5. }
  6. http {
  7. ## 定义共享空间
  8. lua_shared_dict ip_black_list 1m;
  9. include mime.types;
  10. default_type application/octet-stream;
  11. server {
  12. listen 8083;
  13. location / {
  14. default_type text/html;
  15. access_by_lua_file /usr/local/openresty/nginx/lua/black-list-access.lua;
  16. proxy_pass http://localhost:8080/;
  17. }
  18. }
  19. }

./sbin/nginx -p ./ -c conf/nginx-black-list.conf

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/224864
推荐阅读
相关标签
  

闽ICP备14008679号