Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。
特点:支持高并发连接,官方5w,实际生产中2~4w并发连接;
内存消耗少;开源免费;配置文件简单;支持Rewire重写规则;内置的健康检查功能;节省带宽;稳定性高;支持热部署;
一、安装
软件版本:Centos 6.3 x64 + nginx 1.8.0 源码编译安装
1、安装缺少的依赖包
yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
2、从官网下载稳定版本(目前最新稳定版1.8.0)http://nginx.org/en/download.html
pwd /usr/local/nginx-1.8.0 ./configure make && make install
3、启动关闭
## 检查配置文件是否正确
# /usr/local/nginx-1.6/sbin/nginx -t
# ./sbin/nginx -V # 可以看到编译选项
## 启动、关闭
# ./sbin/nginx # 默认配置文件 conf/nginx.conf,-c 指定
# ./sbin/nginx -s stop
或 pkill nginx
## 重启,不会改变启动时指定的配置文件
# ./sbin/nginx -s reload
或 kill -HUP `cat /usr/local/nginx-1.6/logs/nginx.pid`
4、Nginx配置404页面的方法
第一种nginx报404:
error_page 404 /404.html;
location = /404.html {
root html;
}
第二种nginx做反向代理,后端tomcat报404
location / {
#root html;
#index index.html index.htm;
proxy_intercept_errors on; // 开启此配置时关键,拦截后端错误返回错误页面
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://localhost:8080;
}
error_page 404 /404.html;
location = /404.html {
root html;
}
第三种:在http配置块中开启fastcgi_intercept_errors on
参考链接:[http://seanlook.com/2015/05/17/nginx-install-and-config/]
-------------------------
Lua Nginx配置
git clone https://github.com/simpl/ngx_devel_kit.git git clone https://github.com/chaoslawful/lua-nginx-module.git git clone https://github.com/agentzh/redis2-nginx-module.git git clone https://github.com/agentzh/set-misc-nginx-module.git git clone https://github.com/agentzh/echo-nginx-module.git yum -y install pcre pcre-dev* wget http://nginx.org/download/nginx-1.3.14.tar.gz tar zxvf nginx-1.3.14.tar.gz cd nginx-1.3.14 ./configure --prefix=/usr/local/nginx --add-module=../ngx_devel_kit/ --add-module=../lua-nginx-module --add-module=../redis2-nginx-module --add-module=../set-misc-nginx-module --add-module=../echo-nginx-module make && make install
注:在 server 段里,加入代码,如果不加此代码或者设置为 on 时,则需要重启 Nginx。
lua_code_cache off;
下面是 Nginx.conf 的配置
server{
listen 80;
server_name test.lua.com;
#http://test.lua.com/lua
location /hello {
default_type "text/plain";
content_by_lua 'ngx.say("Nginx Lua Hello!")';
}
#GET http://test.lua.com/get?key=key
location /get {
set_unescape_uri $key $arg_key;
redis2_query get $key;
redis2_pass 127.0.0.1:6379; #配置redis访问
}
#SET http://test.lua.com/set?key=key&val=value
location /set {
set_unescape_uri $key $arg_key;
set_unescape_uri $val $arg_val;
redis2_query set $key $val;
redis2_pass 127.0.0.1:6379;
}
}
重启Nginx
/etc/init.d/nginx restart
也可以直接用lua调用redis的接口
local ckid = redis.pcall('get',KEYS[1])
local meta
if ckid ~= nil then
meta = redis.call('hgetall', ckid)
else
meta = 'none'
ckid = 'none'
end
return {ckid, meta}
参考链接:
http://xiaorui.cc/2015/01/27/%E4%BD%BF%E7%94%A8nginx-lua%E5%AE%9E%E7%8E%B0redis%E9%AB%98%E6%80%A7%E8%83%BDhttp%E6%8E%A5%E5%8F%A3/
---
Copyright © 2015 - 2016 DISPACE.NET | 使用帮助 | 关于我们 | 投诉建议