文章目录
Engine X
由俄罗斯程序员 Igor Sysoev
于 2004
发布
可以作为 web服务器、反向代理服务器、邮件服务器、负载均衡服务器
install
安装最新版,包含stream模块!
dnf install -y dnf-utils
vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
yum-config-manager --enable nginx-mainline
# centos7才需要
wget https://nginx.org/keys/nginx_signing.key --no-check-certificate
rpm --import nginx_signing.key
dnf install -y nginx
systemctl start nginx
# --with-file-aio --with-http_ssl_module --with-http_v2_module
# --with-http_realip_module --with-http_sub_module
# --with-http_gunzip_module --with-stream --with-stream_realip_module
yum install gd-devel
groupadd www
useradd www -s /sbin/nologin -g www -M # M:no create home
chown -R www:www /home/www
nginx -t # 检查配置文件的正确性
nginx -s reload # 重新载入配置文件+lua模块
nginx -s reopen # 重启
nginx -s stop # 停止
nginx.conf
key | value |
---|---|
= | 普通的精确匹配 |
~ | 正则匹配,区分大小写 |
~* | 不区分大小写 |
$is_arg | 如果有$args,则为’?‘否则’' |
$args | foo=123&bar=456 == $query_string. $arg_foo $arg_bar.. |
$uri | /to/index.php |
$request_uri | /to/index.php?foo=123&bar=456 |
$request_method | GET POST |
$server_protocol | HTTP/1.1 |
$remote_user | 启用basicauthen认证后的用户 |
$remote_addr/$remote_port | 注意:这只是当前tcp的连接地址!!可能是中间代理的地址! 如 proxy3 |
$host | 不带端口.客户请求的ip.若请求头无Host则使用’server_name’的第一项. |
$http_host | 带端口(80时仅显示ip).请求头无Host时为空 |
$proxy_host | 取自’proxy_pass’参数(被代理服务的地址)!若是80端口也仅显示ip! |
X-Real-IP | ~$remote_addr;如proxy3 |
X-Forwarded-For | client, proxy1, proxy2; client -> proxy1 -> proxy2 -> proxy3, proxy3中的头 |
nginx_lua
local hdr = ngx.req.get_headers(); -- ngx.req.raw_header()
ngx.say("Host: ", hdr["Host"]); -- ngx.print()会多输出换行符
ngx.say("user-agent: ", hdr.user_agent);
ngx.req.http_version();
ngx.req.get_method();
ngx.req.get_uri_args()["userid"];
ngx.req.get_post_args();
ngx.req.get_body_data();
ngx.var.request_uri; -- 未解码的uri,需要ngx.unescape_uri()
ngx.arg[1]; -- url?后面的第一个参数的值
ngx.exit(200);
ngx.status = 200;
ngx.header.a = "1";
ngx.header.b = {"a", "b"};
ngx.resp.get_headers();
ngx.send_headers();
ngx.exec('/redirect');
local res = ngx.location.capture('/subreq');
if res.status == 200 then
ngx.print(res.body);
end
-- 可并发多个uri子请求,全部完成时返回,最后的时间是耗时最长的那个子请求!
res1, res2 = ngx.location.capture_multi({{uri, options?}, {uri,options?}, ...})
ngx.md5("123");
ngx.escape_uri/unescape_uri();
ngx.encode_args/decode_args();
ngx.encode_base64/decode_base64();
ngx.hmac_sha1(secret, str)
ngx.re.match()
openresty
auto-include-luaJit
https://github.com/bungle/awesome-resty
https://openresty.org/en/
https://github.com/alibaba/tengine
wget -P /etc/yum.repos.d/ https://openresty.org/package/centos/openresty.repo
yum install openresty
vim /etc/profile
export LUAJIT_LIB=/usr/local/openresty/luajit/lib
export LUAJIT_INC=/usr/local/openresty/luajit/include/luajit-2.0