LK最近在部署项目的时候了解到项目架构中使用到了ngnix负载均衡,虽然使用的是阿里云的负载均衡服务,但是LK还是想深入了解一下nginx,主要有一下知识总结:
1.nginx是什么
2.nginx优点
3.nginx安装和部署
4.nginx负载均衡和反向代理
5.nginx+tomcat+keepalived
作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使 Nginx 尤其受到虚拟主机提供商的欢迎。能够支持高达 50,000 个并发连接数的响应,感谢 Nginx 为我们选择了 epoll and kqueue 作为开发模型.
作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP,也可以支持作为 HTTP代理服务器 对外进行服务。Nginx 用 C 编写, 不论是系统资源开销还是 CPU 使用效率都比 Perlbal 要好的多。
作为邮件代理服务器: Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last.fm 描述了成功并且美妙的使用经验
一、安装Nginx:
wget下载: http://nginx.org/download/nginx-1.4.2.tar.gz
进行安装: tar -zxvf nginx-1.4.2.tar.gz
下载锁需要的依赖库文件:
yum install pcre
yum install pcre-devel
yum install zlib
yum install zlib-devel
进行configure配置:cd nginx-1.4.2 && ./configure --prefix=/usr/local/nginx 查看是否报错
编译安装 make && make install
二,Nginx配置
配置nginx.conf ,将/usr/local/nginx/conf/nginx.conf替换为以下内容
user www www;
worker_processes 2; #设置值和CPU核心数一致
error_log /usr/local/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http
{
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - r e m o t e u s e r [ remote_user [ remoteuser[time_local] “KaTeX parse error: Double superscript at position 23: …" ' '̲status b o d y b y t e s s e n t " body_bytes_sent " bodybytessent"http_referer” ’
‘"$http_user_agent" $http_x_forwarded_for’;
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler KaTeX parse error: Expected 'EOF', got '#' at position 28: …e_addr 10m; #̲下面是server虚拟主机的配…
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ ..(gif|jpg|jpeg|png|bmp|swf|ico)$
{
expires 30d;
#access_log off;
}
location ~ ..(js|css)?$
{
expires 15d;
#access_log off;
}
access_log off;
}
}
检查配置文件nginx.conf的正确性命令:
启动命令:/usr/local/nginx/sbin/nginx -s start 关闭(stop)重启(reload)
成功:查看是否启动(netstat -ano | grep 80)
失败:可能为80端口被占用等。
最终
nginx已经启动 无法访问页面
问题不是出在nginx上,而是出在iptable上,在iptable上添加80端口
#vi /etc/sysconfig/iptables
//在倒数第二行加入80端口
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
//重启iptables
#/etc/init.d/iptables restart
重启会报如下错误:
解决方法:
重新启动nginx会报如下错:
执行如下命令会生成nginx.pid文件,但会报如下错:
[root@localhost ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
执行 killall -9 nginx 杀死80进程
重新执行
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
查看nginx是否启动
ps -ef | grep nginx
#设定负载均衡的服务器列表
#upstream myproject {
#weigth参数表示权值,权值越高被分配到的几率越大
#max_fails 当有#max_fails个请求失败,就表示后端的服务器不可用,默认为1,将其设置为0可以关闭检查
#fail_timeout 在以后的#fail_timeout时间内nginx不会再把请求发往已检查出标记为不可用的服务器
#}
#webap
#upstream myapp {
#server 192.168.43.132:8080 weight=1 max_fails=2 fail_timeout=30s;
#server 192.168.43.90:8080 weight=1 max_fails=2 fail_timeout=30s;
#}
#返回的相应文件地址
location / {
#设置客户端真实ip地址
#proxy_set_header X-real-ip $remote_addr;
#负载均衡反向代理
#proxy_pass http://myapp;
#返回根路径地址(相对路径:相对于/usr/local/nginx/)
root html;
#默认访问文件
index index.html index.htm;
}
运行结果:
第一步:下载keepalived地址:http://www.keepalived.org/download.html
解压安装:tar -zxvf keepalived-1.2.18.tar.gz -C /usr/local/
yum install -y openssl openssl-devel(需要安装一个软件包)
cd keepalived-1.2.18/ && ./configure -prefix=/usr/local/keepalived
make && make install
第二步:将keepalived安装成Linux系统服务,因为没有使用keepalived的默认安装路径(默认路径:/usr/local),安装完成之后,需要做一些修改工作:
首先创建文件夹,将keepalived配置文件进行复制:mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
然后复制keepalived脚本文件:
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/keepalived /usr/sbin/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/可以设置开机启动: ,到此我们安装完毕!
第三步:对配置文件进行修改:vim /etc/keepalived/keepalived.confkeepalived.conf配置文件说明:
(一)Master
iglobal_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server smtp.hysec.com
smtp_connection_timeout 30
router_id nginx_master # 设置nginx master的id,在一个网络应该是唯一}.
vrrp_script chk_http_port {
script “/etc/keepalived/nginx_check.sh” #最后手动执行下此脚本,以确保此本能够正常执行
interval 2 #(检测脚本执行的间隔,单位是秒)
weight 2
}
vrrp_instance VI_1 {
state MASTER # 指定keepalived的角色,MASTER为主,BACKUP为备
interface eth1 # 当前进行vrrp通讯的网络接口卡(当前centos的网卡)
virtual_router_id 66 # 虚拟路由编号,主从要一直
priority 100 # 优先级,数值越大,获取处理请求的优先级越高
advert_int 1 # 检查间隔,默认为1s(vrrp组播周期秒数)
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port #(调用检测脚本)
}
virtual_ipaddress {
192.168.0.200 # 定义虚拟ip(VIP),可多设,每行一个
}
}
(二)Backup!
bal_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server smtp.hysec.com
smtp_connection_timeout 30
router_id nginx_backup # 设置nginx backup的id,在一个 络应该是唯一的
}
vrrp_script chk_http_port {
script “/etc/keepalived/nginx_check.sh”
interval 2 #(检测脚本执行的间隔)
weight 2
}
vrrp_instance VI_1 {
state BACKUP # 指定keepalived的角色,MASTER 主,BACKUP为备
interface eth0 # 当前进行vrrp通讯的网络接口卡(> 当前centos的网卡)
virtual_router_id 66 # 虚拟路由编号,主从要一直
priority 99 # 优先级,数值越大,获取处理请 的优先级越高
advert_int 1 # 检查间隔,默认为1s(vrrp组播周> 期秒数)
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port #(调用检测脚本)
}
virtual_ipaddress {
192.168.0.200 # 定义虚拟ip(VIP),可多设,每行> 一个
}
}
(三)
!/bin/bash
A=ps -C nginx --no-header |wc -l
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx #重启nginx
if [ ps -C nginx --no-header |wc -l
-eq 0 ];then #nginx重启失败
exit 1
else
exit 0
fi
else
exit 0
fi
~
~
(四)我们需要把master的keepalived配置文件 copy到master机器(172)的 /etc/keepalived/ 文件夹下,在把backup的keepalived配置文件copy到backup机器(173)的 /etc/keepalived/ 文件夹下,最后把nginx_check.sh脚本分别copy到两台机器的 /etc/keepalived/文件夹下。
(五)nginx_check.sh脚本授权。赋予可执行权限:chmod +x /etc/keepalived/nginx_check.sh
(六)启动2台机器的nginx之后。我们启动两台机器的
keepalived /usr/local/nginx/sbin/nginx
service keepalived start
ps -ef | grep nginx
ps -ef | grep keepalived
运行结果: