编译安装nginx
1.安装依赖包
yum -y install openssl openssl-devel zlibzlib-devel gcc gcc-c++
2.安装pcre
安装nginx所需要的pcre库(为了使nginx支持http rewrite模块)
mkdir -p/home/darren/toolscd /home/darren/tools/wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gztar zxf pcre-8.37.tar.gzcd pcre-8.37./configuremake && makeinstallcd ..
如果安装完以后需要重新安装pcre,则需要rm -rf/usr/local/share/man/man3/pcre*
3.安装nginx
创建nginx用户
groupaddnginxuseradd nginx-s /sbin/nologin -M
cd /home/darren/tools/wget http://nginx.org/download/nginx-1.8.1.tar.gzwgethttp://nginx.org/download/nginx-1.2.6.tar.gztarzxf nginx-1.8.1.tar.gzcd nginx-1.8.1./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_modulemake && makeinstall
检查nginx安装
/usr/local/nginx/sbin/nginx -v
#提示错误:/usr/local/nginx/sbin/nginx: error while loading shared libraries:libpcre.so.1: cannot open shared object file: No such file ordirectoryyum install pcre -y 则不会出现这种错误find /-name libpcre.so.1/usr/local/lib/libpcre.so.1echo '/usr/local/lib/'>>/etc/ld.so.confldconfig
检查语法
/usr/local/nginx/sbin/nginx -t
启动nginx服务
/usr/local/nginx/sbin/nginxnetstat -lnt
浏览器中输入10.1.1.1测试
附一个一键安装脚本:
#!/bin/sh#for one install nginx#for wangjiadongge #install centos sourcerpm -Uvh http://mirrors.yun-idc.com/epel/6Server/x86_64/epel-release-6-8.noarch.rpm #install pluginyum install gcc gcc-c++ openssl-devel pcre-devel zlib-devel -y #create user and groupgroupadd nginx useradd nginx -g nginx -s /sbin/nologin -M #cd /tmp/[ -d /tmp/download ] || mkdir -p /tmp/downloadcd /tmp/download/ #wget nginx from www.nginx.orgwget http://nginx.org/download/nginx-1.8.1.tar.gz tar zxvf nginx-1.8.1.tar.gzcd nginx-1.8.1./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_modulemake && make installchown -R nginx.nginx /usr/local/nginx/echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local #end
#nginx负载均衡脚本
mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bakecho "user nginx;worker_processes 4;events { use epoll; worker_connections 10240;}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; keepalive_timeout 120; include conf.d/*.conf;}" >>/usr/local/nginx/conf/nginx.confmkdir /usr/local/nginx/conf/conf.decho "server { listen 80; server_name login.xiaodongge.com; access_log logs/host.access.log main; location / { proxy_pass http://loginserver; }}upstream loginserver { server 10.86.10.21:8300; server 10.86.10.22:8300; server 10.86.10.23:8300; }
解释:nginx做负载均衡默认是轮询负载,下面逐一来看:
#默认的负载方式,轮询 upstream myapp1 { server srv1.example.com; server srv2.example.com; server srv3.example.com; } server { listen 80; location / { proxy_pass http://myapp1; } }
#最小连接数,哪台服务器连接的最少就向哪台服务器上负载upstream myapp1 { least_conn; server srv1.example.com; server srv2.example.com; server srv3.example.com; }
#ip hash如果一个连接建立,就会始终和这台服务器连接upstream myapp1 { ip_hash; server srv1.example.com; server srv2.example.com; server srv3.example.com;}
#权重,自定义设置哪台服务器负载的多 upstream myapp1 { server srv1.example.com weight=3; server srv2.example.com; server srv3.example.com; }