简单的nginx代理负载搭建

来源:互联网 发布:阿里云cdn配置https 编辑:程序博客网 时间:2024/06/16 16:56

nginx


Nginx (“engine x”) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。


简单的nginx代理负载搭建

测试系统环境:

  • Centos 6.6
  • JDK 1.6.0
  • nginx 1.10.1
  • tomcat 7.0
  • IP地址 192.168.1.241 192.168.1.243
  • 备注:241上搭建tomacat和nginx服务 ;243上只搭建tomcat服务

系统环境搭建

JDK和Tomcat搭建:

jdk:
# yum -y install java-1.6.0-openjdk*
tomacat

# mkdir tomcat-src# wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-7/v7.0.70/bin/apache-tomcat-7.0.70.tar.gz# tar xzf apache-tomcat-7.0.70.tar.gz

nginx搭建:

依赖包pcre
# yum install pcre-devel
建立nginx用户[这个好像用不用都无所谓╮(╯_╰)╭]

# groupadd www# useradd -g www www

解压缩并安装

# wget http://nginx.org/download/nginx-1.10.1.tar.gz# tar -zxvf nginx-1.10.1.tar.gz -C /usr/local/src/# cd入目录:# ./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module  make && make install

[ps:这里如果依赖包没有安装全会报错,后面错误记录有提供所有依赖包安装]


配置与测试

tomacat设置

修改Tomcat连接端口:
192.168.1.241 端口号:18080
192.168.1.243 端口好:38080

nginx设置

文件地址/usr/local/webserver/nginx/conf/nginx.conf
配置信息:

worker_processes  1;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;     upstream  netitcast.com {  #服务器集群名字            server    192.168.1.241:18080  weight=1;         server    192.168.1.243:38080  weight=2;      }           server {        listen       80;        server_name  localhost;    location / {        proxy_pass http://netitcast.com;        proxy_redirect default;    }        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }}

测试

开启nginx和两台的Tomcat;
访问192.168.1.241:80
可以看到这样的页面:
这里写图片描述
这里写图片描述
【ps:两台tomcat已经在页面做了标志】


遇到的问题:

问题1:

CentOS 无网络!
安装CentOS的时候,刚刚安装的系统,无法连接网络,因为网络的东东没有配置:
解决方式:
/etc/sysconfig/network-scripts
这个路径下的文件ifcfg-eth0 内容如下:

# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]DEVICE=eth0BOOTPROTO=noneONBOOT=yesHWADDR=00:0c:29:bb:72:abNETMASK=255.255.255.0IPADDR=192.168.1.254GATEWAY=192.168.1.1TYPE=EthernetUSERCTL=noIPV6INIT=noPEERDNS=yes

还需要配置一下默认的DNS地址;
如果克隆虚拟机 则需要修改网卡mac地址 ;


问题2:nginx安装报错

nginx需要的依赖包
# yum install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

错误1

./configurechecking for OS + Linux 2.6.32-504.el6.i686 i686checking for C compiler ... not found./configure: error: C compiler cc is not found

原因: GCC没有安装
解决: # yum install gcc


错误2

./configure: error: SSL modules require the OpenSSL library.You can either do not enable the modules, or install the OpenSSL libraryinto the system, or build the OpenSSL library statically from the sourcewith nginx by using --with-openssl=<path> option.

原因: openssl 未安装
解决: Centos需要安装openssl-devel Ubuntu则需要安装:sudo apt-get install libssl-dev


附录:

:nginx启动,关闭

启动

# cd usr/local/nginx/sbin# ./nginx

更改配置重启

kill -HUP 主进程号或进程号文件路径或者使用cd /usr/local/nginx/sbin./nginx -s reload

判断配置文件是否正确

nginx -t -c /usr/local/nginx/conf/nginx.conf或者cd /usr/local/nginx/sbin./nginx -t

关闭

查询nginx主进程号  ps -ef | grep nginx  从容停止   kill -QUIT 主进程号  快速停止   kill -TERM 主进程号  强制停止   kill -9 nginx  若nginx.conf配置了pid文件路径,如果没有,则在logs目录下  kill -信号类型 '/usr/local/nginx/logs/nginx.pid'

nginx配置文件分析

worker_processes  1;#工作进程的个数,一般与计算机的cpu核数一致events {    worker_connections  1024;#单个进程最大连接数(最大连接数=连接数*进程数)}http {    include       mime.types; #文件扩展名与文件类型映射表    default_type  application/octet-stream;#默认文件类型    sendfile        on;#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。    keepalive_timeout  65; #长连接超时时间,单位是秒    gzip  on;#启用Gizp压缩    #服务器的集群    upstream  netitcast.com {  #服务器集群名字            server    127.0.0.1:18080  weight=1;#服务器配置   weight是权重的意思,权重越大,分配的概率越大。        server    127.0.0.1:28080  weight=2;    }        #当前的Nginx的配置    server {        listen       80;#监听80端口,可以改成其他端口        server_name  localhost;##############    当前服务的域名    location / {            proxy_pass http://netitcast.com;            proxy_redirect default;        }        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }}

参考博文等

http://blog.csdn.net/fish43237/article/details/40515897
http://network.51cto.com/art/201007/209823_all.htm
http://blog.csdn.net/wang379275614/article/details/47778201
http://www.centoscn.com/CentosServer/cluster/2013/0829/1503.html


2016-08-08 星期一


0 0
原创粉丝点击