目标
1. 合并js、css文件
2. 后端服务器健康检查 http://{ip_addr}/status,查看后端服务器的状态
QA
1.安装PCRE库里,会提示找不到c++编译库
安装c++
yum install -y gcc gcc-c++
2. 安装nginx完成后,运行时会报错
[error while loading shared libraries: libpcre.so.1](https://www.oschina.net/question/12_78952)
定位错误原因:
[root@localhost nginx-1.8.0]# ldd $(which /usr/local/nginx/sbin/nginx)
linux-vdso.so.1 => (0x00007fffe61ff000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003c29c00000)
libcrypt.so.1 => /lib64/libcrypt.so.1 (0x0000003c35400000)
libpcre.so.1 => not found
libz.so.1 => /lib64/libz.so.1 (0x0000003c2ac00000)
libc.so.6 => /lib64/libc.so.6 (0x0000003c29800000)
/lib64/ld-linux-x86-64.so.2 (0x0000003c29000000)
libfreebl3.so => /lib64/libfreebl3.so (0x0000003c34800000)
libdl.so.2 => /lib64/libdl.so.2 (0x0000003c29400000)
解决办法:
[root@localhost lib64]# cd /lib64
[root@localhost lib64]# ln -s libpcre.so.0.0.1 libpcre.so.1
参考链接:http://www.oschina.net/question/12_78952
3. 安装报没有Perl 5
yum install perl
4. 启用concat模块(使用shard模式编译,需要手动开启concat模块)
dso {
load ngx_http_concat_module.so;
}
安装
在Centos下,yum源不提供nginx的安装,可以通过切换yum源的方法获取安装。也可以通过直接下载安装包的方法,以下命令均需root权限执行:
首先安装必要的库(nginx 中gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库)。选定/usr/local为安装目录,以下具体版本号根据实际改变。
1.安装PCRE库
$ cd /usr/local/
$ wget http://nchc.dl.sourceforge.net/project/pcre/pcre/8.36/pcre-8.36.tar.gz
$ tar -zxvf pcre-8.36.tar.gz
$ cd pcre-8.36
$ ./configure
$ make
$ make install
2.安装zlib库
$ cd /usr/local/
$ wget http://zlib.net/zlib-1.2.11.tar.gz
$ tar -zxvf zlib-1.2.11.tar.gz
$ cd zlib-1.2.11
$ ./configure
$ make
$ make install
3.安装ssl
$ cd /usr/local/
$ wget http://www.openssl.org/source/openssl-1.0.1j.tar.gz
$ tar -zxvf openssl-1.0.1j.tar.gz
$ cd openssl-1.0.1j
$ ./config
$ make
$ make install
4.安装Tengine
$ cd /usr/local/
$ wget http://tengine.taobao.org/download/tengine-2.2.0.tar.gz
$ tar -zxvf tengine-2.2.0.tar.gz
$ cd tengine-2.2.0
$ ./configure --prefix=/usr/local/nginx --with-http_concat_module --with-http_upstream_check_module --with-http_stub_status_module --with-openssl=/usr/local/openssl-1.0.1j --with-pcre=/usr/local/pcre-8.36
$ make
$ make install
5.启动
$ /usr/local/nginx/sbin/nginx
检查是否启动成功:
打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。
6. 使用非root用户启动nginx
su root
chown root:${cws} nginx 启动nginx的用户
chmod +s nginx 增加权限
部分命令如下:
重启:
$ /usr/local/nginx/sbin/nginx -s reload
停止:
$ /usr/local/nginx/sbin/nginx -s stop
测试配置文件是否正常:
$ /usr/local/nginx/sbin/nginx -t
强制关闭:
$ pkill nginx
使用示例
==== html部分 ====
<script src="/public/js/??jquery.min.js,index.js,blog/b1.js,blog/b2.js"></script>
==== nginx配置部分(合并css、js) ====
location /public/js/ {
concat on;
}
location / {
root html;
index index.html index.htm;
}
使用完整示例(后端健康检查)
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
# load modules compiled as Dynamic Shared Object (DSO)
#
#dso {
# load ngx_http_fastcgi_module.so;
# load ngx_http_rewrite_module.so;
#}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
upstream cluster1 {
ip_hash; # 使用ip_hash模式,如果服务器down机后,会将用户切换到另一台服务器,重启后又会切换回来
# simple round-robin
server 192.168.6.135:8080;
server 192.168.6.136:8080;
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
server {
listen 80;
server_name localhost;
charset UTF-8;
access_log logs/host.access.log main;
location / {
proxy_pass http://cluster1;
index index.html index.htm;
}
location /status {
check_status;
#access_log off;
#allow SOME.IP.ADD.RESS;
#deny all;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
}
资料
03 Nginx安装的扩展
Tengine 中文文档
Tengine 简单例子
Tengine ngx_http_concat_module
Tengine 安装配置 http_concat_module
Tengine 文档
Tengine 健康检查插件
Https: 免费SSL证书letsencrypt配置教程
Tomcat session共享资料
Redis 命令参考
redis安装与配置
使用redis配置tomcat共享session