<一>安装编译环境
yum install gcc-c++
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl--devel
或者
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
------安装pcre------
--1、下载
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
--2、解压
tar zxvf pcre-8.35.tar.gz
--3、进入目录
cd pcre-8.35
--4、检测系统配置及编译安装
./configure
make
make install
<二> 安装nginx
1.进入默认目录
cd /usr/local
2.下载nginx
注意:nginx-1.9.0之后支持socket转发,之前不支持。
nginx版本说明:http://nginx.org/ mainline version代表内核版本,stable version代表稳定版
nginx下载地址:http://nginx.org/download/
我这里需要转发socket请求,所以下载稳定版1.10
wget http://nginx.org/download/nginx-1.10.3.tar.gz
3.解压
tar -zxvf nginx-1.10.3.tar.gz
4.进入目录
cd nginx-1.10.3
5.编译安装
如只需要支持转发普通的http请求,则按照下面步骤安装
./configure #默认安装在/usr/local/nginx
make
make install
如需要支持socket转发,需要用stream模块安装
./configure --with-stream
make
make install
6.安装目录
whereis nginx
/usr/local/nginx
<三>添加组用户
/usr/sbin/groupadd nginx
/usr/sbin/useradd -g nginx nginx
<四>启动nginx
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s reload
/usr/local/nginx/sbin/nginx -s stop
####
/usr/local/nginx #如果Nginx未启动
/usr/local/nginx -s reload #--如果Nginx已启动
启动成功后浏览器访问http://ip如看到nginx 欢迎界面说明启动成功。
<五>安装过程中可能遇到的问题
1.启动nginx 过程中可能报如下错误
[root@localhost sbin]# ./nginx -reload
./nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
[root@localhost sbin]#
从错误提示信息可以得知是因为缺少lib文件导致,进一步查看具体内容
[root@localhost sbin]# ldd $(which /usr/local/nginx/sbin/nginx)
linux-vdso.so.1 => (0x00007ffef09f6000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003420600000)
libcrypt.so.1 => /lib64/libcrypt.so.1 (0x000000342ba00000)
libpcre.so.1 => not found
libcrypto.so.10 => /usr/lib64/libcrypto.so.10 (0x00007f57fbb0a000)
libz.so.1 => /lib64/libz.so.1 (0x0000003421200000)
libc.so.6 => /lib64/libc.so.6 (0x0000003420200000)
/lib64/ld-linux-x86-64.so.2 (0x000000341fa00000)
libfreebl3.so => /lib64/libfreebl3.so (0x000000342be00000)
libdl.so.2 => /lib64/libdl.so.2 (0x000000341fe00000)
[root@localhost sbin]#
查看结果显示 : libpcre.so.1 => not found ,同时注意lib库的路径,有/lib/* 和 /lib64/* 之分。
解决办法:
1、首先确认已经安装好pcre软件(nginx依赖改软件)。
2、创建软连接
对于/lib/* 32位系统来说:
[root@localhost /]# cd lib
[root@localhost lib]# ls *pcre*
libpcre.so.0 libpcre.so.0.0.1
###添加软连接###
[root@localhost lib]# ln -s libpcre.so.0.0.1 libpcre.so.1
###测试###
[root@localhost lib]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost sbin]#
对于/lib/* 64位来说:
[root@localhost /]# cd lib64
[root@localhost lib64]# ls *pcre*
libpcre.so.0 libpcre.so.0.0.1
[root@localhost lib64]# ln -s libpcre.so.0.0.1 libpcre.so.1
[root@localhost lib64]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost sbin]#
<六>配置并启动Nginx
–1、启动方式
###启动###
[root@localhost sbin]# ./nginx
###修改配置文件后启动###
[root@localhost sbin]# ./nginx -s reload
###停止###
[root@localhost sbin]# ./nginx -s stop
2.nginx.conf文件的简单配置
###stream是nginx1.9.0后支持socket转发的东东,在1.9.0之前配置中会报错的###
配置文件是conf下的nginx.conf
如下配置表示:
socket监听10092端口,转发到内网的20011,20012,20013端口
http监听10090端口,转发至内网的10081,10082,10083端口
stream {
upstream socket_port {
hash $remote_addr consistent;
server 127.0.0.1:20011;
server 127.0.0.1:20012;
server 127.0.0.1:20013;
}
server {
listen 10092;
proxy_connect_timeout 50s;
proxy_timeout 50s;
proxy_pass socket_port;
}
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream localhost {
server 127.0.0.1:10081 weight=1 max_fails=2 fail_timeout=30s;
server 127.0.0.1:10082 weight=1 max_fails=2 fail_timeout=30s;
server 127.0.0.1:10083 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 10090;
server_name 119.53.211.78;
location /{
proxy_pass http://localhost;
proxy_set_header Host $host:$server_port; ###加入$server_port是防止nginx转发后端口丢失
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}