源码安装nginx
这里装nginx的三个依赖,分别是pcre、openssl、zlib
其中编译pcre需要:
yum install gcc gcc-c++ pcre-devel
下载源码包
官网下载最新版即可:
http://www.pcre.org/
http://www.openssl.org
http://www.zlib.net/
http://nginx.org
注意:这里pcre只能是是8.0+,pcre2不支持
会报错:
make[2]: *** No rule to make target `libpcre.la'. Stop.
除了pcre我都用的最新稳定版,给个我用的pcre源码包:
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz
编译
这里不用分别编译安装,直接进入解压的nginx目录下执行
假设文件都放在/home目录
./configure --prefix=/data/nginx
--with-http_realip_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_dav_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_addition_module \
--with-pcre=/home/pcre2-10.00 \
--with-openssl=/home/openssl-1.0.2a \
--with-http_ssl_module \
--with-zlib=/home/zlib-1.2.8
注意绿色的三个是指定源码的目录,不是安装目录,因为本方法是联合编译的,不需要提前编译安装pcre,ssl,zlib
然后就是:
make
make install
执行
按照上面的安装方法,nginx装在/data/nginx
./data/nginx/sbin/nginx -c /data/nginx/conf/nginx.conf
#因为它需要指定配置文件才能运行,执行这条配置文件没有返回,建议使用脚本控制
脚本如下
#!/bin/sh
# config: /usr/local/nginx/conf/nginx.conf
nginx_path="/data/nginx"
nginx_pid="/data/nginx/logs/nginx.pid"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginx_path/sbin/nginx ] || exit 0
RETVAL=0
prog="nginx"
start() {
# Start daemons.
if [ -e $nginx_pid -a ! -z $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
if [ -e $nginx_path/conf/nginx.conf ];then
echo -n $"Starting $prog: "
$nginx_path/sbin/nginx -c $nginx_path/conf/nginx.conf &
RETVAL=$?
[ $RETVAL -eq 0 ] && {
touch /var/lock/subsys/$prog
success $"$prog"
}
echo
else
RETVAL=1
fi
return $RETVAL
}
# Stop daemons.
stop() {
echo -n $"Stopping $prog: "
killproc -d 10 $nigx_path/sbin/nginx
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f $nginx_pid /var/lock/subsys/$prog
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reconfigure)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|reconfigure|status}"
exit 1
esac
exit $RETVAL
如果脚本名字叫nginx.sh
那么可以:
./nginx.sh status|stop|start....
代理的配置
首先在nginx.conf主配置里添加:
include /data/nginx/conf/vhost.d/*.conf;
然后创建vhost.d文件,在里面创建两个配置文件,alfa.conf和bata.conf
/data/nginx/conf/vhost.d/alfa.conf
# 访问test.xxx.com的请求会转发到反向代理服务器的8888端口(公司搞网络的妹子配置转发的),并和alfa.conf的server_name匹配成功,转发到192.168.52.17:80
server {
listen 8888;
server_name test.xxx.com;
location / {
proxy_pass http://192.168.52.17;
proxy_set_header Host $host:8888;# 这里的8888要和上面的对应
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
/data/nginx/conf/vhost.d/bata.conf
# bata.conf
# 它则会匹配test1.xxx.com转发到192.168.44.140:80;
server {
listen 8888;
server_name test1.xxx.com;
location / {
proxy_pass http://192.168.44.140;
proxy_set_header Host $host:8888;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}