CentOS7安装Nginx并配置Lua

安装Nginx

下载对应当前系统版本的nginx包(package)

1
# wget  http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

建立nginx的yum仓库

1
# rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm

下载并安装nginx

1
# yum install nginx

启动nginx服务

1
systemctl start nginx

IMG_20160606_1

1
2
3
systemctl start nginx
systemctl status nginx
systemctl restart nginx

Nginx配置Lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';


log_format log_resp_body '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$request_time $bytes_sent $request_length "$request_body" "$resp_body"';

server {
listen 80;
server_name localhost;

#charset koi8-r;
access_log /var/log/nginx/log/access.html.log log_resp_body;

lua_need_request_body on;

set $resp_body "";

body_filter_by_lua '
local resp_body = string.sub(ngx.arg[1], 1, 1000)
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
if ngx.arg[2] then
ngx.var.resp_body = ngx.ctx.buffered
end
';


location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

nginx: [emerg] unknown directive “lua_need_request_body” in /etc/nginx/nginx.conf

配置失败,需要我们安装lua-nginx-module。也推荐使用OpenResty的方式。可以参考我的另一篇博客OpenResty安装、配置与使用

另一种方式安装Nginx

1.下载并解压:

1
2
3
4
wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar -xf nginx-1.8.0.tar.gz
cd nginx-1.8.0/
./configure

2../configure错误:

./configure: error: C compiler cc is not found

1
2
whereis gcc
yum install gcc gcc-c++ ncurses-devel perl

./configure: error: the HTTP rewrite module requires the PCRE library.

安装pcre-devel解决问题

1
yum -y install pcre-devel

./configure: error: the HTTP gzip module requires the zlib library.

安装zlib-devel

1
yum install -y zlib-devel

编译安装:

1
2
3
./configure
make
make install
八宝粥 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!