openresty 概述
openresty 简介
openresty基于nginx、lua的web平台,整合了大量三方模块,可直接使用,不需要手动编译
# openresty 默认安装的模块
root@7bd203df440c:/usr/local/openresty/nginx# ./sbin/nginx -V
nginx version: openresty/1.19.9.1
built with OpenSSL 1.1.1l 24 Aug 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx \
--with-cc-opt='-O2 -DNGX_LUA_ABORT_AT_PANIC -I/usr/local/openresty/zlib/include -I/usr/local/openresty/pcre/include -I/usr/local/openresty/openssl111/include' \
--add-module=../ngx_devel_kit-0.3.1 \
--add-module=../echo-nginx-module-0.62 \
--add-module=../xss-nginx-module-0.06 \
--add-module=../ngx_coolkit-0.2 \
--add-module=../set-misc-nginx-module-0.32 \
--add-module=../form-input-nginx-module-0.12 \
--add-module=../encrypted-session-nginx-module-0.08 \
--add-module=../srcache-nginx-module-0.32 \
--add-module=../ngx_lua-0.10.20 \
--add-module=../ngx_lua_upstream-0.07 \
--add-module=../headers-more-nginx-module-0.33 \
--add-module=../array-var-nginx-module-0.05 \
--add-module=../memc-nginx-module-0.19 \
--add-module=../redis2-nginx-module-0.15 \
--add-module=../redis-nginx-module-0.3.7 \
--add-module=../ngx_stream_lua-0.0.10 \
--with-ld-opt='-Wl,-rpath,/usr/local/openresty/luajit/lib -L/usr/local/openresty/zlib/lib -L/usr/local/openresty/pcre/lib -L/usr/local/openresty/openssl111/lib -Wl,-rpath,/usr/local/openresty/zlib/lib:/usr/local/openresty/pcre/lib:/usr/local/openresty/openssl111/lib' \
--with-pcre-jit --with-stream \
--with-stream_ssl_module --with-stream_ssl_preread_module \
--with-http_v2_module --without-mail_pop3_module \
--without-mail_imap_module --without-mail_smtp_module \
--with-http_stub_status_module --with-http_realip_module \
--with-http_addition_module --with-http_auth_request_module \
--with-http_secure_link_module --with-http_random_index_module \
--with-http_gzip_static_module --with-http_sub_module \
--with-http_dav_module --with-http_flv_module \
--with-http_mp4_module --with-http_gunzip_module \
--with-threads --with-stream --with-http_ssl_module
配置说明
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;
#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/119
client_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;
include /etc/nginx/conf.d/*.conf;
# Don't reveal OpenResty version to clients.
# server_tokens off;
}
default.conf
# nginx.vh.default.conf -- docker-openresty
#
# This file is installed to:
# `/etc/nginx/conf.d/default.conf`
#
# It tracks the `server` section of the upstream OpenResty's `nginx.conf`.
#
# This config (and any other configs in `etc/nginx/conf.d/`) is loaded by
# default by the `include` directive in `/usr/local/openresty/nginx/conf/nginx.conf`.
#
# See https://github.com/openresty/docker-openresty/blob/master/README.md#nginx-config-files
#
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/local/openresty/nginx/html;
index index.html index.htm;
}
location /lua {
default_type "applictaion/json";
content_by_lua "ngx.say('hello gtlx')";
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/openresty/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root /usr/local/openresty/nginx/html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
main 指令
user *** *** # 定义运行ngin的用户、用户组
worker_processors 2 # nginx的进程数
worker_cpu_affinity auto # nginx的cpu亲缘性
worker_rlimit_nofile 65535 # worker进程最大可打开的文件数
worker_priority 0 # worker进程优先级
worker_shutdown_timeout 30 # 30s内无法平滑关闭,强制退出
pid /var/run/nginx.pid # 进程文件
error_log /path/error.log level # 错误日志路径、输出级别
events {
worker_connections 1000 # 单个进程最大连接数,
# nignx总最大连接数 = 单个进程最大连接数 * worker_processors
use epoll # 使用的io模型
}
http 指令
http {
include mime.types; # 文件拓展名、类型映射表
default_type application/octet-stream; # 默认文件类型
underscores_in_headers off; # 请求头中包含underscores是否标记为无效
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# 日志格式(名称为main)
access_log logs/access.log main; # 日志文件路径、使用的日志格式(上面定义的main格式)
client_body_temp_path /var/run/openresty/nginx-client-body; # 客户端请求临时文件保存路径
proxy_temp_path /var/run/openresty/nginx-proxy; # 代理请求缓存路径
gzip on; # 是否开启压缩
sendfile on; # 是否开启sendfile函数,默认开启
tcp_nopush on; # sendfile为on生效,提升网络传输效率
server_tokens off; # 屏蔽版本号,默认不屏蔽
keepalive_timeout 65; # tcp连接超时时间
include /etc/nginx/conf.d/*.conf; # 导入配置文件
}
server、location 指令
server {
listen 80; # 服务监听端口
server_name localhost; # 主机名
charset koi8-r; # 编码集
access_log /var/log/nginx/host.access.log main; # 日志输出路径、输出格式
location / { # 请求路径,支持正则表达式
root /usr/local/openresty/nginx/html; # 根目录
index index.html index.htm;
}
location /lua {
default_type "applictaion/json";
content_by_lua "ngx.say('hello gtlx')";
}
error_page 404 /404.html; # 客户端错误重定向页面
error_page 500 502 503 504 /50x.html; # 服务端错误重定向页面
location = /50x.html {
root /usr/local/openresty/nginx/html;
}
}
常用模块
内置模块:http://openresty.org/cn/components.html
三方模块搜索:
常用模块说明
LuaNginxModule:集成lua,在nginx中使用lua命令
LuaRestyUploadLibrary:http文件上传
HeadersMoreNginxModule:设置请求、响应头(添加、修改、删除)
lua-resty-mysql:操作mysql数据库
lua-resty-redis:操作redis数据库
lua-resty-lrucache:nginx缓存
lua-resty-mlcache:nginx缓存
# 动态负载均衡
ngx_http_dyups_module
nginx-upsync-module
lua-resty-balancer
ngx_http_auth_request_moddule:nginx鉴权