Chaya大数据架构综合实践课程学习 ——— Openresty安装

 使用环境:VMWare 15.5 Centos 7 虚拟机
                   MobaXterm_Portable_v22.0

一、下载Openresty相关依赖

##下载Openresty依赖
yum install readline-devel pcre-devel openssl-devel gcc

##下载Openresty仓库yum源
wget https://openresty.org/package/centos/openresty.repo

##移动到yum源
mv openresty.repo /etc/yum.repos.d/

##更新yum源
yum check-update

##查看软件可用版本
yum list openresty --showduplicates

##安装Openresty源
yum install -y openresty

##测试安装正确
openresty -v

##默认安装位置
##/usr/local/openresty

二、Nginx相关配置

编写nginx.conf配置文件 位置:/opt/openresty/conf/nginx.conf(需要手动创建)

ps: 可直接使用下方代码一步创建,此截图看起来比较繁琐但是是谨慎的看看当前文件夹是否有其他文件避免后面步骤出线错误

 手动创建nginx.conf代码

touch /opt/openresty/conf/nginx.conf

 nginx.conf内容如下:

#nginx.conf
#nginx
user    root root;

#work进程数,
worker_processes 4;

# 错误日志路径,和日志级别
#需要先手动创建error.log文件,不然后面执行命令时会报错
error_log /opt/openresty/logs/nginx_error.log error; 

# nginx pid文件
pid    /opt/openresty/logs/nginx.pid;

# 单个worker最大打开的文件描述符个数
worker_rlimit_nofile 65535;

events
{
	#使用epoll模型
	use epoll;

	#单个worker进程允许的最多连接数
	worker_connections 65535;
}


http
{
    #mime.types 和当前文件在同一个目录下,要先提前创建,不然报错显示没有此文件
	include mime.types;
	default_type application/octet-stream;
	gzip on;
	gzip_min_length 1k;
	gzip_buffers 4 16k;
	gzip_http_version 1.0;
	gzip_comp_level 2;
	gzip_types text/plain application/x-javascript text/css application/xml;
	gzip_vary on;
	underscores_in_headers on;
		
	log_format main escape=json '{"timestamp": "$time_local",'
        '"remote_addr": "$remote_addr",'
        '"referer": "$http_referer",'
        '"request": "$request",'
        '"statu": "$status",'
        '"byte": "$body_bytes_sent",'
        '"agen": "$http_user_agent",'
        '"x_forwarde": "$http_x_forwarded_for",'
        '"up_addr": "$upstream_addr",'
        '"up_host": "$upstream_http_host",'
        '"up_resp_time": "$upstream_response_time",'
        '"request_time": "$request_time"}';	
	
	
	
	#定义我们数据采集的日志格式 
	log_format format_userlogs '$logdata';
	
	open_log_file_cache max=1000 inactive=60s;
	keepalive_timeout 0;
	client_max_body_size 20m;
	include /opt/openresty/conf/vhost/*.conf;
}

记得要先创建此文件中的error.log和mime.types以及nginx.pid文件,不然后面执行命令时会报错显示没有此文件

touch /opt/openresty/logs/nginx_error.log
touch /opt/openresty/conf/mime.types
touch /opt/openresty/logs/nginx.pid

如果后面重启openresty出现了问题,请记得子nginx.pid中输入nginx的端口号,这里设置为8802(也就是仅仅在nginx.pid中输入:8802)

三、服务应用配置

用户自定义配置文件userlogs.conf 位置:/opt/openresty/conf/vhost/userlogs.conf(也是需要自己去创建此文件)

touch /opt/openresty/conf/vhost/userlogs.conf

编写userlogs.conf内容:

#userlogs.conf
server {
      listen  8802 default_server;

      lua_need_request_body on;
      client_max_body_size 5M;
      client_body_buffer_size 5M;
      location /data/userlogs {
          set $logdata '';
    			content_by_lua_block {
              -- cjson模块
              local cjson = require "cjson"
              -- 读取请求体信息
              ngx.req.read_body()
              -- 请求体信息存放到 body_data变量中
              local body_data = ngx.req.get_body_data()
              -- 如果请求体为空,返回错误
              if body_data == nil  then
                ngx.say([[{"code":500,"msg":"req body nil"}]])
                return 
              end
              -- 定义当前时间
              local current_time = ngx.now()*1000
              
              -- 定义一个字典(存放请求参数)
              local data={}
              data["ctime"] = current_time
              
              -- 将增加的信息编码为json
              local meta = cjson.encode(data)
			  
              -- 将body_data数据进行编码处理(若需要使用节点信息则增加meta数据)
              --local req = ngx.encode_base64(meta) .. "-" .. ngx.unescape_uri(body_data)
              local req = ngx.unescape_uri(body_data)

              -- 将数据赋值给我们定义的nginx变量logdata(定义的log_format就使用)
              ngx.var.logdata = req 
              ngx.say([[{"code":200,"msg":"ok"}]])
   			}
          access_log  logs/userlogs.log  format_userlogs;
      }
}

四、Nginx服务启动

##测试配置格式正确 -p 指定openresty的配置目录
openresty -p /opt/openresty/ -t

##服务启动 -p 指定openresty的配置目录
openresty -p /opt/openresty/

##服务启动测试
##端口查看
netstat -antp |grep 8802
或者
lsof -i:8802

##进程查看
ps aux | grep nginx | grep -v grep

##服务停止
openresty -p /opt/openresty/ -s stop

##在服务未停止时重启  
openresty -p /opt/openresty/ -s reload

我的效果图:

 

五、Hello World界面尝试

如果已经完成了上面的安装步骤,可以直接进行下方链接的 Hello World实例 进行练习

OpenResty 使用介绍 | 菜鸟教程 (runoob.com)https://www.runoob.com/w3cnote/openresty-intro.html注意: 由于我使用的是虚拟机,所以不能直接通过本地win系统的浏览器输入http://localhost:9000/ 查看虚拟机的的内容。如果想要通过本机浏览器查看虚拟机OpenResty的页面内容,在本地浏览器中输入:http://[虚拟机的IP]:9000/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值