nginx使用方法 - artinfo1982/demos GitHub Wiki
nginx的编译安装(不添加任何第三方模块)
nginx编译时,依赖zlib、pcre、openssl(如果需要支持HTTPS的话),可以分别下载上述软件包和nginx一起交叉编译,也可以预先编译好上述软件再编译nginx。
下载nginx源代码后,解压,修改源代码路径/auto/cc/gcc文件,将下面这行注释(将debug日志关闭):
# debug
#CFLAGS="$CFLAGS -g"
如果是在Windows上编译(比如cygwin),需要将nginx源代码路径/src/os/unix/ngx_user.c中的这段中的value那行注释,改成下面的样子:
ngx_int_t
ngx_libc_crypt(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted)
{
char *value;
size_t len;
ngx_err_t err;
//value = crypt((char *) key, (char *) salt);
value = (char *)salt;
... ...
}
nginx的编译步骤,都是./configrue XXX && make && make install,其中XXX表示配置参数,根据不同的选择会不同。
已经安装zlib、pcre、openssl的系统,configure后的参数如下:
./configure --prefix=/opt/nginx --sbin-path=/opt/nginx/bin/nginx --conf-path=/opt/nginx/conf/nginx.conf --error-log-path=/opt/nginx/log/error.log --pid-path=/opt/nginx/pid/nginx.pid --lock-path=/opt/nginx/lock/nginx.lock --with-http_ssl_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-pcre
没有安装过zlib、pcre、openssl的系统,configure后的参数如下(需要分别指出zlib、pcre、openssl的源代码解压路径):
./configure --prefix=/opt/nginx --sbin-path=/opt/nginx/bin/nginx --conf-path=/opt/nginx/conf/nginx.conf --error-log-path=/opt/nginx/log/error.log --pid-path=/opt/nginx/pid/nginx.pid --lock-path=/opt/nginx/lock/nginx.lock --with-http_ssl_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-pcre=/pcrePath --with-zlib=/zlibPath --with-openssl=/opensslPath
configure完成之后,make && make install即可。
一个典型的nginx.conf如下(windows下worker_connections最大为64):
worker_processes 1;
events {
worker_connections 64;
}
http {
include mime.types;
default_type application/octet-stream;
access_log off;
sendfile off;
keepalive_timeout 60;
gzip on;
server {
listen 50000;
server_name localhost;
charset utf-8;
access_log off;
location / {
alias /opt/nginx/html/index.html;
}
}
}
nginx添加echo模块
echo模块,可以实现当请求来临时,返回任何你想返回的字符串。
下载echo-nginx-module,下载路径:
https://github.com/openresty/echo-nginx-module/releases
将下载的源代码包上传到linux,解压后,重新按照第一章节中的方法编译安装nginx,只是在configure的最后,添加--add-module=/echo模块源代码路径(方便起见,以已经安装了zlib、pcre、openssl的环境为例,下同):
./configure --prefix=/opt/nginx --sbin-path=/opt/nginx/bin/nginx --conf-path=/opt/nginx/conf/nginx.conf --error-log-path=/opt/nginx/log/error.log --pid-path=/opt/nginx/pid/nginx.pid --lock-path=/opt/nginx/lock/nginx.lock --with-http_ssl_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-pcre --add-module=/opt/echo-nginx-module-0.61
使用echo模块的功能可以在nginx.conf中这样写:
location ~ ^/api/test {
echo "{\"a\":\"1\"}";
}
nginx添加headers_more模块
headers_more模块,可以实现添加、修改、清除任何响应中的HTTP Header,还可以自定义当某些HTTP Status时添加某些特定的Header。
下载headers-more-nginx-module,下载路径:
https://github.com/openresty/headers-more-nginx-module/releases
将下载的源代码包上传到linux,解压后,重新按照第一章节中的方法编译安装nginx,只是在configure的最后,添加--add-module=/headers_more模块源代码路径(合并echo模块的配置):
./configure --prefix=/opt/nginx --sbin-path=/opt/nginx/bin/nginx --conf-path=/opt/nginx/conf/nginx.conf --error-log-path=/opt/nginx/log/error.log --pid-path=/opt/nginx/pid/nginx.pid --lock-path=/opt/nginx/lock/nginx.lock --with-http_ssl_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-pcre --add-module=/opt/echo-nginx-module-0.61 --add-module=/opt/headers-more-nginx-module-0.32
headers_more的使用方法示例,在nginx.conf中添加如下的内容:
location ~ ^/api/headers_more {
#清除header原来的值
more_clear_headers 'Content-Type';
#设置header的新值
more_set_headers 'Content-Type: application/json';
#添加自定义的header
more_set_headers 'Header_Test: hello';
#添加在某些HTTP状态码时使用的header
more_set_headers -s '400 404' 'Header_Err: haha';
}
nginx添加lua模块
lua模块,可以在nginx.conf中实现编程,比如可以实现当请求来临时,解析请求的URL、body,根据不同的参数选择不同的响应。
下载ngx_devel_kit,下载路径:
https://github.com/simpl/ngx_devel_kit/releases
下载lua-nginx-module,下载路径:
https://github.com/openresty/lua-nginx-module/releases
将下载的上述两个源代码包上传到linux,解压后,重新按照第一章节中的方法编译安装nginx,只是在configure的最后,依次添加--add-module=/模块源代码路径(合并echo、headers_more模块的配置):
./configure --prefix=/opt/nginx --sbin-path=/opt/nginx/bin/nginx --conf-path=/opt/nginx/conf/nginx.conf --error-log-path=/opt/nginx/log/error.log --pid-path=/opt/nginx/pid/nginx.pid --lock-path=/opt/nginx/lock/nginx.lock --with-http_ssl_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-pcre --add-module=/opt/echo-nginx-module-0.61 --add-module=/opt/headers-more-nginx-module-0.32 --add-module=/opt/ngx_devel_kit-0.3.0 --add-module=/opt/lua-nginx-module-0.10.11rc3
此外,还需要预安装luajit。
下面以解析请求消息体中的字段为例,讲解如何使用lua。假设请求的POST消息体是json的,比如{"test":"xxx"},xxx为可变内容,其取值可能是dog、cat,需要根据其具体的值,决定响应的形式。
解析json需要用到cjson或者dkjson,由于在windows平台编译cjson会出现很多错误,所以选择dkjson来替代。dkjson的下载地址:
http://dkolf.de/src/dkjson-lua.fsl/home
将下载下来的dkjson.lua文件上传到/usr/lib/lua/5.2下。
下面是使用lua编写conf文件的例子:
location ~ ^/api/lua {
content_by_lua_block {
package.path = "/usr/lib/lua/5.2/dkjson.lua"
local json = require("dkjson")
ngx.req.read_body()
local data = ngx.req.get_body_data()
local obj, pos, err = json.decode(data, 1, nil)
if err then
ngx.say(err)
else
if obj.test == "dog" then
ngx.say("{\"msg\":\"hello dog\"}")
elseif obj.test == "cat" then
ngx.say("{\"msg\":\"hello cat\"}")
end
end
}
}