应各位小伙伴们要求,最近都在搭建nginx的环境内容,特此整理一份关于nignx搭建的内容,供童鞋们参考使用。
架构设计思路:
应用通过keepalived VIP连接nginx,nginx为双主模式,当其中一台nginx故障,VIP会漂移至另一台nginx上,从而不影响业务使用。keepalived作为负载进行nginx的负载。
部署环境介绍:
操作系统 | centos6.5 |
内核版本 | 3.10.0-693.2.2.el7.x86_64 |
nginx版本 | nginx-1.14.0 |
keepalived | keepalived-1.3.4 |
这里可能会遇到坑,填坑方式请见:搭建内容续集
<一> nignx部署
(1)上传相应程序包:通过相应工具上传安装包至目录(这里目录一skyfans举例)
(2)安装jdk,这里省略安装步骤,本例采用一键部署jdk的形式,具体脚本请参考:
(3)服务器基础配置,这里采用一键配置的方式,详情参考脚本:
(4)安装nginx依赖包
注意:在安装依赖包时,有的童鞋说会出现yum无法使用的情况,我们在后续的章节里会编写一篇文章进行yum本地仓库搭建及yum源重新配置的文章!!!!!!
yum -y install pcre-devel openssl-devel libxml2-devel libxslt-devel gd-devel lua-devel GeoIP-devel gcc gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel
(5)安装nginx
请替换成你的实际上传的文件路径
cd skyfans/
解压包
tar -zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0
编译安装nignx
./configure
make
make install
确认nginx配置文件正确
/usr/local/nginx/sbin/nginx -t
启动nginx
/usr/local/nginx/sbin/nginx
注:若想实现使用service nginx start命令,请参考nginx方便启停脚本:
启动后测试,浏览器输入IP:端口,能看到如下页面证明nginx搭建完毕,并可启动成功。
(6)nginx主从配置
1.配置nginx(master)
服务器1:创建nginx.conf文件将下面的内容粘贴到nginx.conf,并修改其中IP和端口,注意修改成实际服务器的IP及端口。
vi /usr/local/nginx/conf/nginx.conf
粘贴进如下内容:
#user nobody;
worker_processes 8;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
#plugin
upstream plugin.com {
ip_hash;
server 172.1.1.200:8080;
server 172.1.1.201:8080;
}
server {
listen 8080;
server_name plugin;
#charset koi8-r;
charset utf-8;
#access_log logs/host.access.log main;
location / {
proxy_pass http://plugin.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#platform
upstream platform.com {
ip_hash;
server 172.1.1.200:80;
server 172.1.1.201:80;
}
server {
listen 80;
server_name platform;
#charset koi8-r;
charset utf-8;
#access_log logs/host.access.log main;
location / {
proxy_pass http://platform.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
修改完毕后,重新启动nignx master服务。
service nginx restart
2.配置nginx backup
服务器2:创建nginx.conf文件将下面的内容粘贴到nginx.conf,注意并修改其中IP和端口。
vi /usr/local/nginx/conf/nginx.conf
粘贴如下内容到配置文件:
#user nobody;
worker_processes 8;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
#plugin
upstream plugin.com {
ip_hash;
server 172.1.1.200:8080;
server 172.1.1.201:8080;
}
server {
listen 8080;
server_name plugin;
#charset koi8-r;
charset utf-8;
#access_log logs/host.access.log main;
location / {
proxy_pass http://plugin.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#platform
upstream platform.com {
ip_hash;
server 172.1.1.200:80;
server 172.1.1.201:80;
}
server {
listen 80;
server_name platform;
#charset koi8-r;
charset utf-8;
#access_log logs/host.access.log main;
location / {
proxy_pass http://platform.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
配置完毕,重启nginx从服务
service nginx restart
到此nginx的主从配置完毕,下面安装配置keepalived。
**<二>安装keepalived
0.安装keepalived
cd /skyfasn/
tar -xzvf keepalived-1.3.4.tar.gz
mv keepalived-1.3.4 /usr/local/keepalived
cd /usr/local/keepalived/
./configure --prefix=/usr/local/keepalived
make && make install
1.编写一个shell脚本,用于检测nginx的运行情况
vi /shells/nginx_pidcheck.sh
编写如下内容到脚本中
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 3
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
2.配置keepalived(master)
编辑keepalived.conf文件,修改相应内容:interface eth0为服务器物理网卡名称(注意你的网卡的名称);virtual_router_id 151为自定义,需要与backup节点一致(同组);172.1.1.150为VIP地址,需要与backup节点一致。
! Configuration File for keepalived
global_defs {
notification_email {
}
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_http_port {
script "/shells/nginx_pidcheck.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 151
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.1.1.150
}
}
3.配置keepalived(backup)
编辑keepalived.conf文件,修改相应内容:interface eth0为服务器物理网卡名称(注意修改);virtual_router_id 151为自定义,需要与master节点配置一致;172.1.1.150为VIP地址,需要与master节点配置一致。
! Configuration File for keepalived
global_defs {
notification_email {
}
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_http_port {
script "/shells/nginx_pidcheck.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 151
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.1.1.150
}
}
4.启动keepalived(主从同步执行–主从都需要执行)
cp /usr/local/keepalived/keepalived/etc/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
#/usr/local/nginx/sbin/nginx
分别启动主从keepalived服务:
主节点启动
service keepalived start
从节点启动
service keepalived start
启动完毕后,进行程序验证,到此环境就搭建完毕了!!