日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

openresty配置部署

發布時間:2024/1/23 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 openresty配置部署 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

環境:centos

yum install -y readline-devel pcre-devel openssl-devel gcc yum install -y g++ make automake autoconf./configure --prefix=/usr/local/openresty \--with-luajit--with-http_iconv_module \--with-http_stub_status_modulegmake ; gmake installgroupadd -f www useradd -r -s /sbin/nologin -g www www

openresty配置

反向代理配置實例

user www www; worker_processes 1;error_log logs/error.log info; pid logs/nginx.pid;events {worker_connections 3000; }http {include mime.types;sendfile on;keepalive_timeout 65;gzip on;server {listen 80;location / {proxy_pass https://github.com;proxy_redirect off;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}location /README.md {proxy_pass https://github.com/moonbingbing/openresty-best-practices/blob/master/README.mdproxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwared_for;}} }

/lb_health_check配置

location = /lb_health_check {add_header Content-Type "text/plain; charset=utf-8";return 200;access_log off; }

favicon.ico配置

location = /favicon.ico {log_not_found on;access_log off; }

重啟

nginx -s reload nginx -t -c /path/to/nginx.conf nginx -s stop

壓力測試 yum -y install httpd-tools

lua擴展

獲取POST請求配置

# vim conf/nginx.conf log_format main '$remote_addr | $remote_user | [$time_local] | "$request" | ''$status | $body_bytes_sent | "$http_referer" | ''"$http_user_agent" | "$http_x_forwarded_for" | "$request_body" | "$resp_body"';# vim vhost/http.conf server {listen 80;set $resp_body "";include vhost/loc/yun_location.conf; }# vim vhost/loc/yun_location.conf location = /lb_health_check {add_header Content-Type "text/plain; charset=utf-8";return 200;access_log off; }location = /favicon.ico {log_not_found on;access_log off; }location /api {lua_need_request_body on;body_filter_by_lua 'local resp_body = string.sub(ngx.arg[1], 1, 1000)ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_bodyif ngx.arg[2] thenngx.var.resp_body = ngx.ctx.bufferedend';proxy_pass http: //yun_backend; }

接收請求配置

# nginx.conf location ~ /lua_request/(\d+)/(\d+) {set $a $1;set $b $host;default_type "application/json";content_by_lua_file conf/lua/lua_request.luaecho_after_body "ngx.var.b $b"; }# 獲取變量 local variable = ngx.var; ngx.say("ngx.var.a: ", variable.a); -- set $a $1; ngx.say("ngx.var.b: ", variable.b); -- set $b $host;ngx.say("ngx.var[1]: ", variable[1]); ngx.say("ngx.var[2]: ", variable[2]);# 獲取頭 local headers = ngx.req.get_headers(); ngx.say("Host: ", headers["Host"]); ngx.say("user-agent: ", headers["user-agent"]); ngx.say("user-agent: ", headers.user_agent);ngx.say("ngx.req.http_version: ", ngx.req.http_version()); ngx.say("ngx.req.get_method: ", ngx.req.get_method()); ngx.say("ngx.req.raw_header: ", ngx.req.raw_header()); -- 未解析的請求頭字符串for k, v in pairs(headers) dongx.say(k, ": ", v); end# 獲取get請求 local get_argument = ngx.req.get_uri_args(); for k, v in pairs(get_argument) dongx.say(k, ": ", v); end# 獲取post請求 ngx.req.read_body(); local post_argument = ngx.req.get_post_args(); for k, v in pairs(post_argument) dongx.say(k, ": ", v); endngx.say("ngx.req.get_body_data(): ", ngx.req.get_body_data()); -- 解析的請求body體內容字符串

輸出響應

# nginx.conf location ~ /lua_response {default_type "application/json";content_by_lua_file conf/lua/lua_response.lua; }# 響應設置 ngx.header.a = "1" -- 寫響應頭 ngx.header.b = {"2", "3"} -- 寫多個響應頭 ngx.say("") -- 響應內容體 ngx.print("") -- 同上, 多輸出換行符 ngx.exit(200) -- 指定狀態碼退出ngx.status = 200 ngx.resp.get_headers() ngx.send_headers()# 編碼解碼 local request_uri = ngx.var.request_uri; -- 未經解碼的請求uri ngx.say("unescape_uri: ", ngx.unescape_uri(request_uri)); ngx.say("md5: ", ngx.md5("hello, world")); ngx.say("time: ", ngx.http_time(ngx.time()));ngx.escape_uri/ngx.unescape_uri -- uri編碼解碼 ngx.encode_args/ngx.decode_args -- 參數編碼解碼 ngx.encode_base64/ngx.decode_base64 -- base64編碼解碼 ngx.re.match -- 正則表達式匹配## 全局內存 # nginx.conf lua_shared_dict shared_data 1m;location /lua_shared_dict {default_type "application/json";content_by_lua_file conf/lua/lua_shared_dict.lua; }# 獲取全局共享的內存變量 local shared_data = ngx.shared.shared_data;local i = shared_data:get("i") if not i theni = 100;shared_data:set("i", i); endi = shared_data:incr("i", 1); ngx.say("i = ", i);## 處理流程 init_by_lua, init_by_lua_file init_worker_by_lua, init_worker_by_lua_file set_by_lua, set_by_lua_file rewrite_by_lua, rewrite_by_lua_file access_by_lua, access_by_lua_file content_by_lua, content_by_lua_file header_filter_by_lua, header_filter_by_lua_file body_filter_by_lua, body_filter_by_lua_file log_by_lua, log_by_lua_file## 引入模塊 # nginx.conf lua_package_path "/usr/local/openresty/lualib/?.lua;;"; -- lua模塊, ”;;”表示默認搜索路徑 lua_package_cpath "/usr/local/openresty/lualib/?.so;;"; -- c模塊local cjson = require(“cjson”) local redis = require(“resty.redis”)

?

總結

以上是生活随笔為你收集整理的openresty配置部署的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。