|
38 | 38 | + [ngx Lua APi 介绍使用](#Openresty_ngx_api_used)
|
39 | 39 | + [连接数据库](#Openresty_connent_redis)
|
40 | 40 | + [OpenResty缓存](#Openresty_connent_cache)
|
| 41 | + + [lua-resty-upstream-healthcheck 使用](#Openresty_lua-resty-upstream-healthcheck) |
41 | 42 | + [luajit 执行文件默认安装路径](#Nginx_base_knowledge)
|
42 | 43 | + [Redis执行Lua脚本基本用法](#Redis_Run_Lua)
|
43 | 44 | + [Ngx_lua 写入Redis数据,通过CURL请求](#Ngx_lua_write_Redis)
|
|
689 | 690 | + 纯内存的操作,多个worker之间共享的(比如nginx开启10个Worker,则每个worker之间是共享该内存的)
|
690 | 691 | + 同一份数据在多个worker之间是共享的,只要存储一份数据就可以了
|
691 | 692 | + 锁的竞争(数据原子性)
|
| 693 | +#### <a name="Openresty_lua-resty-upstream-healthcheck"/> lua-resty-upstream-healthcheck 使用 |
| 694 | ++ health.txt 在每个upstream 服务器组的root 目录下创建这个文件,目录结构如下所示 |
| 695 | + ```Lua |
| 696 | + ├── html |
| 697 | + │ ├── 50x.html |
| 698 | + │ ├── index.html |
| 699 | + │ ├── websocket001.html |
| 700 | + │ └── websocket02.html |
| 701 | + ├── html81 |
| 702 | + │ ├── 50x.html |
| 703 | + │ ├── health.txt |
| 704 | + │ └── index.html |
| 705 | + ├── html82 |
| 706 | + │ ├── 50x.html |
| 707 | + │ ├── health.txt |
| 708 | + │ └── index.html |
| 709 | + ├── html83 |
| 710 | + │ ├── 50x.html |
| 711 | + │ ├── health.txt |
| 712 | + │ └── index.html |
| 713 | + ├── html84 |
| 714 | + │ ├── 50x.html |
| 715 | + │ └── index.html |
| 716 | + ``` |
| 717 | ++ nginx.conf |
| 718 | + ```Lua |
| 719 | + worker_processes 8; |
| 720 | + |
| 721 | + error_log logs/error.log; |
| 722 | + pid logs/nginx.pid; |
| 723 | + |
| 724 | + events { |
| 725 | + use epoll; |
| 726 | + worker_connections 1024; |
| 727 | + } |
| 728 | + |
| 729 | + http { |
| 730 | + include mime.types; |
| 731 | + default_type text/html; |
| 732 | + #lua模块路径,其中”;;”表示默认搜索路径,默认到/usr/servers/nginx下找 |
| 733 | + lua_package_path "/home/tinywan/Openresty_Protect/First_Protect/lualib/?.lua;;"; #lua 模块 |
| 734 | + lua_package_cpath "/home/tinywan/Openresty_Protect/First_Protect/lualib/?.so;;"; #c模块 |
| 735 | + include /home/tinywan/Openresty_Protect/First_Protect/nginx_first.conf; |
| 736 | + } |
| 737 | + |
| 738 | + ``` |
| 739 | ++ nginx_first.conf |
| 740 | + ```Lua |
| 741 | + upstream tomcat { |
| 742 | + server 127.0.0.1:8081; |
| 743 | + server 127.0.0.1:8082; |
| 744 | + server 127.0.0.1:8083; |
| 745 | + server 127.0.0.1:8084 backup; |
| 746 | + } |
| 747 | + |
| 748 | + lua_shared_dict healthcheck 1m; |
| 749 | + |
| 750 | + lua_socket_log_errors off; |
| 751 | + |
| 752 | + init_worker_by_lua_block { |
| 753 | + local hc = require "resty.upstream.healthcheck" |
| 754 | + local ok, err = hc.spawn_checker{ |
| 755 | + shm = "healthcheck", -- defined by "lua_shared_dict" |
| 756 | + upstream = "tomcat", -- defined by "upstream" |
| 757 | + type = "http", |
| 758 | + |
| 759 | + http_req = "GET /health.txt HTTP/1.0\r\nHost: tomcat\r\n\r\n", |
| 760 | + -- raw HTTP request for checking |
| 761 | + |
| 762 | + interval = 2000, -- run the check cycle every 2 sec |
| 763 | + timeout = 1000, -- 1 sec is the timeout for network operations |
| 764 | + fall = 3, -- # of successive failures before turning a peer down |
| 765 | + rise = 2, -- # of successive successes before turning a peer up |
| 766 | + valid_statuses = {200, 302}, -- a list valid HTTP status code |
| 767 | + concurrency = 10, -- concurrency level for test requests |
| 768 | + } |
| 769 | + } |
| 770 | + |
| 771 | + server { |
| 772 | + listen 80; |
| 773 | + server_name localhost; |
| 774 | + |
| 775 | + location / { |
| 776 | + proxy_pass http://tomcat; |
| 777 | + } |
| 778 | + |
| 779 | + # status page for all the peers: |
| 780 | + location /server/status { |
| 781 | + access_log off; |
| 782 | + allow 127.0.0.1; |
| 783 | + |
| 784 | + default_type text/plain; |
| 785 | + content_by_lua_block { |
| 786 | + local hc = require "resty.upstream.healthcheck" |
| 787 | + ngx.say("Nginx Worker PID: ", ngx.worker.pid()) |
| 788 | + ngx.print(hc.status_page()) |
| 789 | + } |
| 790 | + } |
| 791 | + |
| 792 | + # status page for all the peers: |
| 793 | + location /lua { |
| 794 | + default_type 'text/html'; |
| 795 | + lua_code_cache off; |
| 796 | + content_by_lua_file /home/tinywan/Openresty_Protect/First_Protect/lua/test.lua; |
| 797 | + } |
| 798 | + |
| 799 | + location /lua_get_redis { |
| 800 | + default_type 'text/html'; |
| 801 | + lua_code_cache off; |
| 802 | + content_by_lua_file /home/tinywan/Openresty_Protect/First_Protect/lua/get_redis.lua; |
| 803 | + } |
| 804 | + |
| 805 | + location /get_redis_iresty { |
| 806 | + default_type 'text/html'; |
| 807 | + lua_code_cache off; |
| 808 | + content_by_lua_file /home/tinywan/Openresty_Protect/First_Protect/lua/get_redis_iresty.lua; |
| 809 | + } |
| 810 | + |
| 811 | + location /get_main { |
| 812 | + default_type 'text/html'; |
| 813 | + lua_code_cache off; |
| 814 | + content_by_lua_file /home/tinywan/Openresty_Protect/First_Protect/lua/main.lua; |
| 815 | + } |
| 816 | + |
| 817 | + } |
| 818 | + |
| 819 | + server { |
| 820 | + listen 8081; |
| 821 | + server_name localhost; |
| 822 | + |
| 823 | + location / { |
| 824 | + root html81; |
| 825 | + index index.html index.htm; |
| 826 | + } |
| 827 | + |
| 828 | + location /health_status { |
| 829 | + |
| 830 | + } |
| 831 | + } |
| 832 | + |
| 833 | + server { |
| 834 | + listen 8082; |
| 835 | + server_name localhost; |
| 836 | + |
| 837 | + location / { |
| 838 | + root html82; |
| 839 | + index index.html index.htm; |
| 840 | + } |
| 841 | + |
| 842 | + location /health_status { |
| 843 | + |
| 844 | + } |
| 845 | + } |
| 846 | + |
| 847 | + server { |
| 848 | + listen 8083; |
| 849 | + server_name localhost; |
| 850 | + |
| 851 | + location / { |
| 852 | + root html83; |
| 853 | + index index.html index.htm; |
| 854 | + } |
| 855 | + |
| 856 | + location /health_status { |
| 857 | + |
| 858 | + } |
| 859 | + } |
| 860 | + |
| 861 | + server { |
| 862 | + listen 8084; |
| 863 | + server_name localhost; |
| 864 | + |
| 865 | + location / { |
| 866 | + root html84; |
| 867 | + index index.html index.htm; |
| 868 | + } |
| 869 | + } |
| 870 | + |
| 871 | + ``` |
692 | 872 | ### Redis、Lua、Nginx一起工作事迹
|
693 | 873 | + 解决一个set_by_lua $sum 命令受上下文限制的解决思路,已完美解决
|
694 | 874 | + - [x] [API disabled in the context of set_by_lua](https://github.com/openresty/lua-nginx-module/issues/275)
|
|
0 commit comments