Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,以下为Linux centos平台下安装nginx并配置反向代理的过程(采用源码安装的方式)
一:安装编译环境gcc g++ 和make(用于编译源代码成可执行文件)
1yum install gcc gcc-c++ 2 yum -y install gcc automake autoconf libtool make
二:安装pcre (nginx rewrite依赖pcre库)zlib(用于gzip压缩)
1.安装pcre
cd /usr/local/src wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz tar -zxvf pcre-8.34.tar.gzcd pcre-8.34 ./configure make make install
2.安装zlib
cd /usr/local/src wget http://zlib.net/zlib-1.2.8.tar.gz tar -zxvf zlib-1.2.8.tar.gzcd zlib-1.2.8 ./configure make make install
3.安装openssl
wget http://www.openssl.org/source/openssl-1.0.1t.tar.gz tar -zxvf openssl-1.0.1t.tar.gz ./config shared --prefix=/usr/local --openssldir=/usr/local/ssl make depend make sudo make install
三.安装nginx
cd /usr/local/src wget http://nginx.org/download/nginx-1.10.1.tar.gz tar -zxvf nginx-1.10.1.tar.gz cd nginx-1.10.1 ./configure make make install sudo make install
查看nginx启动和配置目录 whereis nginx
注:sbin-path(启动文件路径)
conf-path(配置文件路径)
pid-path(pid文件,刚安装完默认没有,只有启动nginx后才有)
四:启动nginx
Cd /usr/local/nginx/sbin
1.启动 ./nginx
2.重启 ./nginx -s reload
启动的时候报错
./nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
解决方法:
ln -s /usr/local/lib/libpcre.so.1 /lib64
32位系统则:
ln -s /usr/local/lib/libpcre.so.1 /lib
五.配置反向代理 指向node(其中node监听127.0.0.1:3000)
Cd /usr/local/nginx/conf
Vim nginx.conf
其中server选项配置如下
lerver{ listen 80; charset utf-8; location / { proxy_pass http://127.0.0.1:3000; } }
lerver{
listen 80;
charset utf-8;
location / {
proxy_pass http://127.0.0.1:3000;
}
}
六.开启gizp压缩
http选项下
http选项下 gzip on; gzip_min_length 1k; gzip_buffers 16 64k; gzip_http_version 1.1; gzip_comp_level 6; gzip_types application/javascript text/javascript text/plain application/x-javascript text/css application/xml; gzip_vary on; 注意:gzip_types必须加上application/javascript 否则js不会被压缩