目錄
一、什么是OpenResty
二、OpenResty安裝
傳統方式安裝
Docker方式安裝
三、結合lua簡單使用
1、輸出helloword
2、限流
OpenResty是一個基于Nginx與 Lua 的高性能 Web 平臺,其內部集成了大量精良的 Lua 庫、第三方模塊以及大多數的依賴項。用于方便地搭建能夠處理超高并發、擴展性極高的動態 Web 應用、Web 服務和動態網關。OpenResty的目標是讓你的Web服務直接跑在Nginx服務內部,充分利用Nginx的非阻塞 I/O 模型,不僅僅對 HTTP 客戶端請求,甚至于對遠程后端諸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都進行一致的高性能響應。由于OpenResty自身內部有nginx,也可以理解為加強后的nginx。
centos7安裝php環境,1、獲取預編譯包
wget https://openresty.org/package/centos/openresty.repo
?如果安裝過程中報如下錯-bash: wget: command not found 先安裝wget
yum -y install wget
?2、移動編譯包到etc目錄
sudo mv openresty.repo /etc/yum.repos.d/
Centos 7。?如果需要更新資源包執行如下命令進行更新
sudo yum check-update
?列出所有openresty倉庫里頭的軟件包
sudo yum --disablerepo="*" --enablerepo="openresty" list available
3、安裝軟件包,默認安裝在/usr/local/openresty目錄下
sudo yum install -y openresty
mc整合包怎么安裝?4、?啟動nginx
#啟動
/usr/local/openresty/nginx/sbin/nginx
#重啟
/usr/local/openresty/nginx/sbin/nginx -s reload
#停止
/usr/local/openresty/nginx/sbin/nginx -s stop
?5、如果看到如下界面安裝成功
?6、查看版本信息
openresty -v
1、首先需要有docker環境,docker環境安裝參考Docker的入門以及簡單應用的安裝_熟透的蝸牛的博客-CSDN博客
2、獲取鏡像文件
docker pull openresty/openresty
?3、創建容器并運行
docker run -id --name openresty -p 80:80 openresty/openresty
4、以宿主機掛在方式運行
?a、創建掛載目錄
mkdir -p /data/openresty
?b、將容器中的配置復制到宿主機
docker cp openresty:/usr/local/openresty /data
c、停止并刪除openresty
容器?
docker stop openresty #停止
docker rm openresty #刪除
d、重新啟動容器
docker run --name openresty \
--restart always \
--privileged=true \
-d -p 80:80 \
-v /data/openresty/nginx/conf/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf \
-v /data/openresty/nginx/logs:/usr/local/openresty/nginx/logs \
-v /etc/localtime:/etc/localtime \
openresty/openresty
a、在宿主機編輯文件 hello.lua
ngx.say("hello world mayikt");
b、將文件拷貝到openresty容器
進入容器創建文件
docker exec -it openresty /bin/bash #進入容器mkdir -p /root/lua #創建文件sudo docker cp /data/openresty/lua/hello.lua openresty:/root/lua/hello.lua #拷貝文件到openresty容器
c、修改nginx配置文件如下
# nginx.conf -- docker-openresty
#
# This file is installed to:
# `/usr/local/openresty/nginx/conf/nginx.conf`
# and is the file loaded by nginx at startup,
# unless the user specifies otherwise.
#
# It tracks the upstream OpenResty's `nginx.conf`, but removes the `server`
# section and adds this directive:
# `include /etc/nginx/conf.d/*.conf;`
#
# The `docker-openresty` file `nginx.vh.default.conf` is copied to
# `/etc/nginx/conf.d/default.conf`. It contains the `server section
# of the upstream `nginx.conf`.
#
# See https://github.com/openresty/docker-openresty/blob/master/README.md#nginx-config-files
##user nobody;
user root root; #建議使用非root用戶
worker_processes 1;# Enables the use of JIT for regular expressions to speed-up their processing.
pcre_jit on;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;# Enables or disables the use of underscores in client request header fields.# When the use of underscores is disabled, request header fields whose names contain underscores are marked as invalid and become subject to the ignore_invalid_headers directive.# underscores_in_headers off;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;# Log in JSON Format# log_format nginxlog_json escape=json '{ "timestamp": "$time_iso8601", '# '"remote_addr": "$remote_addr", '# '"body_bytes_sent": $body_bytes_sent, '# '"request_time": $request_time, '# '"response_status": $status, '# '"request": "$request", '# '"request_method": "$request_method", '# '"host": "$host",'# '"upstream_addr": "$upstream_addr",'# '"http_x_forwarded_for": "$http_x_forwarded_for",'# '"http_referrer": "$http_referer", '# '"http_user_agent": "$http_user_agent", '# '"http_version": "$server_protocol", '# '"nginx_access": true }';# access_log /dev/stdout nginxlog_json;# See Move default writable paths to a dedicated directory (#119)# https://github.com/openresty/docker-openresty/issues/119client_body_temp_path /var/run/openresty/nginx-client-body;proxy_temp_path /var/run/openresty/nginx-proxy;fastcgi_temp_path /var/run/openresty/nginx-fastcgi;uwsgi_temp_path /var/run/openresty/nginx-uwsgi;scgi_temp_path /var/run/openresty/nginx-scgi;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;server {listen 80;server_name localhost;charset utf-8;set $template_root /root/html;#access_log logs/host.access.log main;# 添加location /lua {default_type 'text/html';content_by_lua_file /root/lua/hello.lua; #當訪問/lua路徑時就會訪問到lua腳本}# redirect server error pages to the static page /50x.htmlerror_page 500 502 503 504 /50x.html;location = /50x.html {root html;} }#include /etc/nginx/conf.d/*.conf; #注釋掉這句,不然默認加載的是容器內 /etc/nginx/conf.d/*.conf的配置文件# Don't reveal OpenResty version to clients.# server_tokens off;
}
d、訪問? ? http://ip/lua
?思考:利用上面的特性就可以將我們有些不需要變化的數據預熱到服務器中,而減少數據庫訪問的壓力,降低服務器帶寬的占用,然后利用lua腳本去維護少量變化的數據。例如商品詳情頁中的圖片,文字描述,頁面上的廣告位,輪播圖等。
a、修改nginx.conf如下
# nginx.conf -- docker-openresty
#
# This file is installed to:
# `/usr/local/openresty/nginx/conf/nginx.conf`
# and is the file loaded by nginx at startup,
# unless the user specifies otherwise.
#
# It tracks the upstream OpenResty's `nginx.conf`, but removes the `server`
# section and adds this directive:
# `include /etc/nginx/conf.d/*.conf;`
#
# The `docker-openresty` file `nginx.vh.default.conf` is copied to
# `/etc/nginx/conf.d/default.conf`. It contains the `server section
# of the upstream `nginx.conf`.
#
# See https://github.com/openresty/docker-openresty/blob/master/README.md#nginx-config-files
##user nobody;
user root root; #建議使用非root用戶
worker_processes 1;# Enables the use of JIT for regular expressions to speed-up their processing.
pcre_jit on;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;# Enables or disables the use of underscores in client request header fields.# When the use of underscores is disabled, request header fields whose names contain underscores are marked as invalid and become subject to the ignore_invalid_headers directive.# underscores_in_headers off;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;# Log in JSON Format# log_format nginxlog_json escape=json '{ "timestamp": "$time_iso8601", '# '"remote_addr": "$remote_addr", '# '"body_bytes_sent": $body_bytes_sent, '# '"request_time": $request_time, '# '"response_status": $status, '# '"request": "$request", '# '"request_method": "$request_method", '# '"host": "$host",'# '"upstream_addr": "$upstream_addr",'# '"http_x_forwarded_for": "$http_x_forwarded_for",'# '"http_referrer": "$http_referer", '# '"http_user_agent": "$http_user_agent", '# '"http_version": "$server_protocol", '# '"nginx_access": true }';# access_log /dev/stdout nginxlog_json;# See Move default writable paths to a dedicated directory (#119)# https://github.com/openresty/docker-openresty/issues/119client_body_temp_path /var/run/openresty/nginx-client-body;proxy_temp_path /var/run/openresty/nginx-proxy;fastcgi_temp_path /var/run/openresty/nginx-fastcgi;uwsgi_temp_path /var/run/openresty/nginx-uwsgi;scgi_temp_path /var/run/openresty/nginx-scgi;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#限流設置 允許每秒有2個請求,超過將會限流limit_req_zone $binary_remote_addr zone=contentRateLimit:1m rate=2r/s;#gzip on;server {listen 80;server_name localhost;charset utf-8;set $template_root /root/html;#access_log logs/host.access.log main;# 添加location /lua {default_type 'text/html';content_by_lua_file /root/lua/hello.lua; #當訪問/lua路徑時就會訪問到lua腳本}location /rate {#使用限流配置default_type 'text/html';limit_req zone=contentRateLimit;content_by_lua_file /root/lua/hello.lua;}# redirect server error pages to the static page /50x.htmlerror_page 500 502 503 504 /50x.html;location = /50x.html {root html;} }#include /etc/nginx/conf.d/*.conf; #注釋掉這句,不然默認加載的是容器內 /etc/nginx/conf.d/*.conf的配置文件# Don't reveal OpenResty version to clients.# server_tokens off;
}
b、重啟openresty容器,訪問,當快速訪問時頁面報錯,錯誤碼為503
?c、處理突發流量控制
# nginx.conf -- docker-openresty
#
# This file is installed to:
# `/usr/local/openresty/nginx/conf/nginx.conf`
# and is the file loaded by nginx at startup,
# unless the user specifies otherwise.
#
# It tracks the upstream OpenResty's `nginx.conf`, but removes the `server`
# section and adds this directive:
# `include /etc/nginx/conf.d/*.conf;`
#
# The `docker-openresty` file `nginx.vh.default.conf` is copied to
# `/etc/nginx/conf.d/default.conf`. It contains the `server section
# of the upstream `nginx.conf`.
#
# See https://github.com/openresty/docker-openresty/blob/master/README.md#nginx-config-files
##user nobody;
user root root; #建議使用非root用戶
worker_processes 1;# Enables the use of JIT for regular expressions to speed-up their processing.
pcre_jit on;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;# Enables or disables the use of underscores in client request header fields.# When the use of underscores is disabled, request header fields whose names contain underscores are marked as invalid and become subject to the ignore_invalid_headers directive.# underscores_in_headers off;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;# Log in JSON Format# log_format nginxlog_json escape=json '{ "timestamp": "$time_iso8601", '# '"remote_addr": "$remote_addr", '# '"body_bytes_sent": $body_bytes_sent, '# '"request_time": $request_time, '# '"response_status": $status, '# '"request": "$request", '# '"request_method": "$request_method", '# '"host": "$host",'# '"upstream_addr": "$upstream_addr",'# '"http_x_forwarded_for": "$http_x_forwarded_for",'# '"http_referrer": "$http_referer", '# '"http_user_agent": "$http_user_agent", '# '"http_version": "$server_protocol", '# '"nginx_access": true }';# access_log /dev/stdout nginxlog_json;# See Move default writable paths to a dedicated directory (#119)# https://github.com/openresty/docker-openresty/issues/119client_body_temp_path /var/run/openresty/nginx-client-body;proxy_temp_path /var/run/openresty/nginx-proxy;fastcgi_temp_path /var/run/openresty/nginx-fastcgi;uwsgi_temp_path /var/run/openresty/nginx-uwsgi;scgi_temp_path /var/run/openresty/nginx-scgi;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#限流設置 允許每秒有2個請求,超過將會限流limit_req_zone $binary_remote_addr zone=contentRateLimit:1m rate=2r/s;#gzip on;server {listen 80;server_name localhost;charset utf-8;set $template_root /root/html;#access_log logs/host.access.log main;# 添加location /lua {default_type 'text/html';content_by_lua_file /root/lua/hello.lua; #當訪問/lua路徑時就會訪問到lua腳本}location /rate {#使用限流配置default_type 'text/html';#平均每秒允許不超過2個請求,突發不超過5個請求,并且處理突發5個請求的時候,沒有延遲,等到完成之后,按照正常的速率處理limit_req zone=contentRateLimit burst=5 nodelay;content_by_lua_file /root/lua/hello.lua;}# redirect server error pages to the static page /50x.htmlerror_page 500 502 503 504 /50x.html;location = /50x.html {root html;} }#include /etc/nginx/conf.d/*.conf; #注釋掉這句,不然默認加載的是容器內 /etc/nginx/conf.d/*.conf的配置文件# Don't reveal OpenResty version to clients.# server_tokens off;
}
d、控制并發連接數
# nginx.conf -- docker-openresty
#
# This file is installed to:
# `/usr/local/openresty/nginx/conf/nginx.conf`
# and is the file loaded by nginx at startup,
# unless the user specifies otherwise.
#
# It tracks the upstream OpenResty's `nginx.conf`, but removes the `server`
# section and adds this directive:
# `include /etc/nginx/conf.d/*.conf;`
#
# The `docker-openresty` file `nginx.vh.default.conf` is copied to
# `/etc/nginx/conf.d/default.conf`. It contains the `server section
# of the upstream `nginx.conf`.
#
# See https://github.com/openresty/docker-openresty/blob/master/README.md#nginx-config-files
##user nobody;
user root root; #建議使用非root用戶
worker_processes 1;# Enables the use of JIT for regular expressions to speed-up their processing.
pcre_jit on;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;# Enables or disables the use of underscores in client request header fields.# When the use of underscores is disabled, request header fields whose names contain underscores are marked as invalid and become subject to the ignore_invalid_headers directive.# underscores_in_headers off;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;# Log in JSON Format# log_format nginxlog_json escape=json '{ "timestamp": "$time_iso8601", '# '"remote_addr": "$remote_addr", '# '"body_bytes_sent": $body_bytes_sent, '# '"request_time": $request_time, '# '"response_status": $status, '# '"request": "$request", '# '"request_method": "$request_method", '# '"host": "$host",'# '"upstream_addr": "$upstream_addr",'# '"http_x_forwarded_for": "$http_x_forwarded_for",'# '"http_referrer": "$http_referer", '# '"http_user_agent": "$http_user_agent", '# '"http_version": "$server_protocol", '# '"nginx_access": true }';# access_log /dev/stdout nginxlog_json;# See Move default writable paths to a dedicated directory (#119)# https://github.com/openresty/docker-openresty/issues/119client_body_temp_path /var/run/openresty/nginx-client-body;proxy_temp_path /var/run/openresty/nginx-proxy;fastcgi_temp_path /var/run/openresty/nginx-fastcgi;uwsgi_temp_path /var/run/openresty/nginx-uwsgi;scgi_temp_path /var/run/openresty/nginx-scgi;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#限流設置 允許每秒有2個請求,超過將會限流limit_req_zone $binary_remote_addr zone=contentRateLimit:1m rate=2r/s;#根據IP地址來限制,存儲內存大小10Mlimit_conn_zone $binary_remote_addr zone=perip:10m;limit_conn_zone $server_name zone=perserver:10m;#gzip on;server {listen 80;server_name localhost;charset utf-8;set $template_root /root/html;#access_log logs/host.access.log main;# 添加location /lua {default_type 'text/html';content_by_lua_file /root/lua/hello.lua; #當訪問/lua路徑時就會訪問到lua腳本}location /rate {#使用限流配置default_type 'text/html';#平均每秒允許不超過2個請求,突發不超過5個請求,并且處理突發5個請求的時候,沒有延遲,等到完成之后,按照正常的速率處理limit_req zone=contentRateLimit burst=5 nodelay;content_by_lua_file /root/lua/hello.lua;}location /rate1{limit_conn perip 0;#單個客戶端ip與服務器的連接數 ,設置為0然后連接不上limit_conn perserver 10; #限制與服務器的總連接數default_type 'text/html';content_by_lua_file /root/lua/hello.lua;}# redirect server error pages to the static page /50x.htmlerror_page 500 502 503 504 /50x.html;location = /50x.html {root html;} }#include /etc/nginx/conf.d/*.conf; #注釋掉這句,不然默認加載的是容器內 /etc/nginx/conf.d/*.conf的配置文件# Don't reveal OpenResty version to clients.# server_tokens off;
}
?
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态