centos 7 OpenResty®(lua-nginx-module)搭建可扩展的Web平台

OpenResty®-英文官网地址:http://openresty.org/en/

OpenResty®-中文官网地址: http://openresty.org/cn/

 

OpenResty®> https://openresty.org/en/linux-packages.html
centos  yun-command
sudo yum install yum-utils
sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
sudo yum install openresty-resty //安装resty命令行实用程序
下载> https://openresty.org/en/download.html
源代码> https://openresty.org/download/openresty-1.15.8.2.tar.gz
# [root@localhost openresty-1.15.8.2]# yum install -y gcc gcc-c++ iptables-services perl pcre-devel openssl-devel zlib wget lrzsz ntpdate
# [root@localhost src]# wget https://openresty.org/download/openresty-1.15.8.2.tar.gz -P /usr/local/src
# [root@localhost src]# tar -zxvf openresty-1.15.8.2.tar.gz
# [root@localhost src]# cd openresty-1.15.8.2
# [root@localhost openresty-1.15.8.2]# ls
# bundle  configure  COPYRIGHT  patches  README.markdown  README-windows.txt  util
# [root@localhost openresty-1.15.8.2]# ./configure 
# Configuration summary
#   + using system PCRE library
#   + using system OpenSSL library
#   + using system zlib library
# 
#   nginx path prefix: "/usr/local/openresty/nginx"
#   nginx binary file: "/usr/local/openresty/nginx/sbin/nginx"
#   nginx modules path: "/usr/local/openresty/nginx/modules"
#   nginx configuration prefix: "/usr/local/openresty/nginx/conf"
#   nginx configuration file: "/usr/local/openresty/nginx/conf/nginx.conf"
#   nginx pid file: "/usr/local/openresty/nginx/logs/nginx.pid"
#   nginx error log file: "/usr/local/openresty/nginx/logs/error.log"
#   nginx http access log file: "/usr/local/openresty/nginx/logs/access.log"
#   nginx http client request body temporary files: "client_body_temp"
#   nginx http proxy temporary files: "proxy_temp"
#   nginx http fastcgi temporary files: "fastcgi_temp"
#   nginx http uwsgi temporary files: "uwsgi_temp"
#   nginx http scgi temporary files: "scgi_temp"
# 
# cd ../..
# Type the following commands to build and install:
#     gmake
#     gmake install
# [root@localhost openresty-1.15.8.2]# gmake && gmake install
# make[2]: Leaving directory `/usr/local/src/openresty-1.15.8.2/build/nginx-1.15.8'
# make[1]: Leaving directory `/usr/local/src/openresty-1.15.8.2/build/nginx-1.15.8'
# mkdir -p /usr/local/openresty/site/lualib /usr/local/openresty/site/pod /usr/local/openresty/site/manifest
# ln -sf /usr/local/openresty/nginx/sbin/nginx /usr/local/openresty/bin/openresty
# [root@localhost openresty-1.15.8.2]# cd /usr/local/openresty/
# [root@localhost openresty]# ls
# bin  COPYRIGHT  luajit  lualib  nginx  pod  resty.index  site
# [root@localhost openresty]# ll bin |grep openresty
# lrwxrwxrwx 1 root root    37 Oct  7 14:32 openresty -> /usr/local/openresty/nginx/sbin/nginx
[root@localhost sbin]# ln -sf `pwd`/nginx /usr/local/sbin/nginx
# !bin/bash
# echo '#!/bin/bash
# chkconfig:35 85 15
# nginx=/usr/local/openresty/nginx/sbin/nginx
# case "$1" in
#     start)
#         echo "Starting nginx daemon..."
#         $nginx && echo "Success"
#     ;;
#     stop)
#         echo "Stopping nginx daemon..."
#         $nginx -s stop && echo "Success"
#     ;;
#     reload)
#         echo "Reloading nginx daemon..."
#         $nginx -s reload && echo "Success"    
#     ;;
#     restart)
#         echo "Restarting nginx daemon..."
#         $nginx -s quit
#         $nginx && echo "Success"
#     ;;
#     reopen)
#         echo "Reopening nginx daemon..."
#         $nginx -s reopen && echo "Success"
#     ;;
#     *)
#         echo "Please useing nginx {start|stop|retart|reload|reopen}"
#         exit2
#     ;;
# esac' > /etc/init.d/nginx
# chmod 755 /etc/init.d/nginx
# chkconfig --add nginx
# chkconfig --level 123456 nginx on  //0-6运行级别可用
# [root@localhost sbin]# iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
# [root@localhost sbin]# service iptables save
# iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
# [root@localhost sbin]# systemctl restart iptables
# [root@localhost sbin]# iptables -L -n
# Chain INPUT (policy ACCEPT)
# target     prot opt source               destination         
# ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:80
# 
# Chain FORWARD (policy ACCEPT)
# target     prot opt source               destination         
# 
# Chain OUTPUT (policy ACCEPT)
# target     prot opt source               destination  
# [root@localhost sbin]# nginx 
# [root@localhost sbin]# nginx -s reload

# 关于OpenResty目录结构的具体说明如下
# ●bin:存放二进制可执行文件.
# ●luajit:存放LuaJIT(Lua代码解释器)相关的文件.
# ●lualib:存放Lua库文件.
# ●nginx:OpenResty整合的Nginx存放在这个目录.
# ●pod:存放用于bin/restydoc程序读取的pod文档.
# ●resty.in ndex:存放pod文档的索引.
# ●site:OPM(OpenResty Package Manager)包的存放目录.

# OpenResty主要应用在一些对性能要求非常高的场合,例如WAF(Web Application Firewall,Web应用防火墙)、
# CDN(Content Delivery Network,内容分发网络)调度、广告系统、消息推送系统、RESTful API(一种服务器接口技术)等.
# 一些知名的互联网公司如 CloudFlare 、奇虎360、新浪、京东等都在使用OpenResty相关的技术.
# OpenResty平台离不开 Nginx 和 Lua 语言,读者可通过GitHub开源平台中的openresty-best practices.

# [root@localhost bin]# ./resty -e 'print("hello,my name is kings-dev.")'
# hello,my name is kings-dev.
# [root@localhost bin]# ./restydoc -s proxy_pass
# ngx_http_proxy_module(7)             nginx            ngx_http_proxy_module(7)
# 
# 
# 
#    proxy_pass
#        syntax: proxy_pass "URL"
# 
#        context: location
# 
#        context: if in location
# 
#        context: limit_except
# 
#        Sets the protocol and address of a proxied server and an optional URI
#        to which a location should be mapped.  As a protocol, “"http"” or
#        “"https"” can be specified.  The address can be specified as a domain
#        name or IP address, and an optional port:
# 
#            proxy_pass http://localhost:8000/uri/;
# 
#        or as a UNIX-domain socket path specified after the word “"unix"” and
#        enclosed in colons:
# 
#            proxy_pass http://unix:/tmp/backend.socket:/uri/;
# 
#        If a domain name resolves to several addresses, all of them will be
#        used in a round-robin fashion.  In addition, an address can be
#        specified as a server group.
# 
#        Parameter value can contain variables.  In this case, if an address is
#        specified as a domain name, the name is searched among the described
#        server groups, and, if not found, is determined using a
#        ngx_http_core_module.
# 
#        A request URI is passed to the server as follows:
# 
#        ·   If the "proxy_pass" directive is specified with a URI, then when a
#            request is passed to the server, the part of a normalized request
#            URI matching the location is replaced by a URI specified in the
#            directive:
# 
#                location /name/ {
#                    proxy_pass http://127.0.0.1/remote/;
#                }
# 
#        ·   If "proxy_pass" is specified without a URI, the request URI is
#            passed to the server in the same form as sent by a client when the
#            original request is processed, or the full normalized request URI
#            is passed when processing the changed URI:
# 
#                location /some/path/ {
#                    proxy_pass http://127.0.0.1;
#                }
# 
#            NOTE
# ngx_http_proxy_module(7)             nginx            ngx_http_proxy_module(7)
# 
# 
# 
#    proxy_pass
#        syntax: proxy_pass "URL"
# 
#        context: location
# 
#        context: if in location
# 
#        context: limit_except
# 
#        Sets the protocol and address of a proxied server and an optional URI
#        to which a location should be mapped.  As a protocol, “"http"” or
#        “"https"” can be specified.  The address can be specified as a domain
#        name or IP address, and an optional port:
# 
#            proxy_pass http://localhost:8000/uri/;
# 
#        or as a UNIX-domain socket path specified after the word “"unix"” and
#        enclosed in colons:
# 
#            proxy_pass http://unix:/tmp/backend.socket:/uri/;
# 
#        If a domain name resolves to several addresses, all of them will be
#        used in a round-robin fashion.  In addition, an address can be
#        specified as a server group.
# 
#        Parameter value can contain variables.  In this case, if an address is
#        specified as a domain name, the name is searched among the described
#        server groups, and, if not found, is determined using a
#        ngx_http_core_module.
# 
#        A request URI is passed to the server as follows:
# 
#        ·   If the "proxy_pass" directive is specified with a URI, then when a
#            request is passed to the server, the part of a normalized request
#            URI matching the location is replaced by a URI specified in the
#            directive:
# 
#                location /name/ {
#                    proxy_pass http://127.0.0.1/remote/;
#                }
# 
#        ·   If "proxy_pass" is specified without a URI, the request URI is
#            passed to the server in the same form as sent by a client when the
#            original request is processed, or the full normalized request URI
#            is passed when processing the changed URI:
# 
#                location /some/path/ {
#                    proxy_pass http://127.0.0.1;
#                }
# 
#            NOTE
# 
#            Before version 1.1.12, if "proxy_pass" is specified without a URI,
#            the original request URI might be passed instead of the changed URI
#            in some cases.
# 
#        In some cases, the part of a request URI to be replaced cannot be
#        determined:
# 
#        ·   When location is specified using a regular expression, and also
#            inside named locations.
# 
#            In these cases, "proxy_pass" should be specified without a URI.
# 
#        ·   When the URI is changed inside a proxied location using the
#            ngx_http_rewrite_module directive, and this same configuration will
#            be used to process a request ("break"):
# 
#                location /name/ {
#                    rewrite    /name/([^/]+) /users?name=$1 break;
#                    proxy_pass http://127.0.0.1;
#                }
# 
#            In this case, the URI specified in the directive is ignored and the
#            full changed request URI is passed to the server.
# 
#        ·   When variables are used in "proxy_pass":
# 
#                location /name/ {
#                    proxy_pass http://127.0.0.1$request_uri;
#                }
# 
#            In this case, if URI is specified in the directive, it is passed to
#            the server as is, replacing the original request URI.
# 
#        WebSocket proxying requires special configuration and is supported
#        since version 1.3.13.
# 
# 
# 
# OpenResty                         2019-10-07          ngx_http_proxy_module(7)

# [root@localhost bin]# cd 
# [root@localhost ~]# mkdir openresty_work
# [root@localhost ~]# cd openresty_work/
# [root@localhost openresty_work]# mkdir logs conf
# [root@localhost openresty_work]# ls -lF
# total 0
# drwxr-xr-x 2 root root 6 Oct  7 15:04 conf/
# drwxr-xr-x 2 root root 6 Oct  7 15:04 logs/

# [root@localhost openresty_work]# vi openresty_nginx_conf.sh

#!/bin/bash
echo "worker_processes 1;
error_log logs/error.log;
events {
        worker_connections 1024;
        }
    http {
        server {
            listen 8080; 
            location /{
                default_type text/html;
                content_by_lua_block {
                    ngx.say('<p>Hello World</p>')
                                    }
                        }
                }
        }" > /root/openresty_work/conf/nginx.conf

# [root@localhost openresty_work]# sh openresty_nginx_conf.sh   
# [root@localhost openresty_work]# ls -lF conf/nginx.conf 
# -rw-r--r-- 1 root root 399 Oct  7 15:14 conf/nginx.conf
# [root@localhost openresty_work]# nginx -s stop
# [root@localhost openresty_work]# nginx -p 
# [root@localhost openresty_work]# nginx -p .
# [root@localhost openresty_work]# curl localhost:8080  
# <p>Hello World</p>
# 在上述配置中, content_by_lua_file 指令用于引入外部文件, lua_code_cache 用于开启或关闭 lua 文件缓存.
# 在默认情况下会对lua文件进行缓存以提高效率,而在开发环境可以临时关闭缓存以方便测试.
# [root@localhost openresty_work]# vi test_lua_nginx.sh
# [root@localhost openresty_work]# sh test_lua_nginx.sh

#!/bin/bash
echo 'worker_processes 1;
error_log logs/error.log;
events {
        worker_connections 1024;
        }
    http {
        server {
            listen 8080;
                location /test.lua{
                lua_code_cache off;
                content_by_lua_file test.lua;
                                    }
                }
            }' > /root/openresty_work/conf/nginx.conf

# [root@localhost openresty_work]# echo "ngx.say('<h1>hi,my name is kings-dev.</h1>')" > test.lua
# [root@localhost openresty_work]# cat test.lua 
# ngx.say('<h1>hi,my name is kings-dev.</h1>')
# [root@localhost openresty_work]# nginx -p . -s reload
# nginx: [alert] lua_code_cache is off; this will hurt performance in ./conf/nginx.conf:10
# [root@localhost openresty_work]# curl localhost:8080/test.lua
# <h1>hi,my name is kings-dev.</h1>

 

 

 

 

posted @ 2019-10-07 16:09  wwweee000  阅读(488)  评论(0编辑  收藏  举报