Nginx学习笔记(一)

一、nginx跳转

1、echo_exec

2、rewrite

二、子请求

1、echo_location

2、auth_request

三、nginx变量

1、内建变量作用于全局的有

$request_method

四、nginx配置指令的执行顺序

1、设计到的模块有ngx_rewite,ngx_access,ngx_echo

rewrite>access>content

2、ngx_lua提供对应模块的方法

rewrite_by_lua;运行在rewrite末尾

access_by_lua;运行在access末尾

 content_by_lua

access_by_lua例子:

location /hello {
        access_by_lua '
            if ngx.var.remote_addr == "127.0.0.1" then
                return
            end

            ngx.exit(403)
        ';

        echo "hello world";
}

 3、在 rewrite 和 access 这两个阶段,多个模块的配置指令可以同时使用,content模块只能有一个"内容处理程序"

例子:

location /test {
     echo hello;
     content_by_lua 'ngx.say("world")';
}
$ curl 'http://localhost:8080/test'
world

 4、content 阶段的运行顺序,依次是 "内容处理程序",ngx_index 模块, ngx_autoindex 模块,以及 ngx_static 模块

ngx_index和ngx_autoindex如果请求的uri不以/结尾,直接放弃解析,调到ngx_static模块

5、Nginx 处理请求的过程一共划分为 11 个阶段,按照执行顺序依次是 post-readserver-rewritefind-configrewritepost-rewritepreaccessaccesspost-accesstry-filescontent 以及 log

(1)post-read

ngx_realip 模块提供的 set_real_ip_from 和 real_ip_header,使用的原因:一般我们会在 Nginx 之前的代理服务器中把请求的原始来源地址编码进某个特殊的 HTTP 请求头中(例如上例中的 X-My-IP 请求头),然后再在 Nginx 一侧把这个请求头中编码的地址恢复出来。这样 Nginx 中的后续处理阶段(包括 Nginx 背后的各种后端应用)就会认为这些请求直接来自那些原始的地址,代理服务器就仿佛不存在一样。

set_real_ip_from 指令规定了来源地址的改写操作只对那些来自 *.*.*.* 的请求生效,可以配置多个

real_ip_header 配置读取ip的header key

(2)server-rewrite 写在server节点下的rewrite函数在这个处理过程执行

(3)find-config   这个阶段并不支持 Nginx 模块注册处理程序,完成当前请求与 location 的配对

(4)rewrite

(5)post-rewrite 完成rewrite 阶段所要求的“内部跳转”操作

(6)preaccess 标准模块 ngx_limit_req 和 ngx_limit_zone 就运行在此阶段,前者可以控制请求的访问频度,而后者可以限制访问的并发度

(7)access

(8)post-access 主要用于配合 access 阶段实现标准 ngx_http_core 模块提供的配置指令 satisfy 的功能

(9)try-files

五、nginx配置记录

export LUAJIT_LIB=/usr/local/opt/luajit/lib
export LUAJIT_INC=/usr/local/opt/luajit/include/luajit-2.0
./configure \
--user=www --group=www \
--prefix=/usr/local/opt/nginx-self \
--with-http_realip_module \
--with-http_image_filter_module \
--with-openssl=/usr/local/source/openssl-1.0.1u \
--with-cc-opt="-I/usr/local/opt/pcre/include" \
--with-ld-opt="-L/usr/local/opt/pcre/lib" \
--add-module=/usr/local/source/ngx_devel_kit-0.3.0 \
--add-module=/usr/local/source/lua-nginx-module-0.10.6 \
--add-module=/usr/local/source/nginx-http-footer-filter \
--add-module=/usr/local/source/nginx-1.10.1/module/echo-nginx-module-0.60

六、header中不支持下划线"_"

nginx对header name的字符做了限制,默认 underscores_in_headers 为off,表示如果header name中包含下划线,则忽略掉。
处理办法有两种:
第一种:配置中http部分 增加underscores_in_headers on; 配置
第二种:用减号-替代下划线符号_,避免这种变态问题。nginx默认忽略掉下划线可能有些原因

 

posted @ 2016-09-23 17:16  hi_felix  阅读(1128)  评论(0编辑  收藏  举报