Keepalived+Nginx雙機配置小結(jié)
1.1 軟硬件要求
- Linux服務(wù)器:RedHat 6+ 或 CentOS 6+,編碼 UTF-8。
1.2 部署前服務(wù)器配置調(diào)優(yōu)
關(guān)閉SELinux:
修改 /etc/selinux/config 文件中的 SELINUX 參數(shù)值為 disabled。
關(guān)閉防火墻:
chkconfig iptables off service iptables stop
修改hostname:
修改 /etc/hosts 文件,添加服務(wù)器真實 IP 和 hostname。
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 192.168.1.100 OSS-103
修改用戶進程可打開文件數(shù)限制:
修改 /etc/security/limits.conf 文件,添加:
* soft nproc 65536 * hard nproc 65536 * soft nofile 65536 * hard nofile 65536
修改 /etc/security/limits.d/90-nproc.conf 文件,注釋掉:
* soft nproc 1024
修改 /etc/pam.d/login 文件,添加:
session required pam_limits.so
重啟 SSH 服務(wù):
service sshd restart
Linux服務(wù)器時間同步:
- 查看時間:
date - 手動修改時間:
date –s “2015-5-8 19:48:00” - 自動更新時間:
ntpdate -u us.pool.ntp.org
1.3 Nginx+Keepalived部署
1.3.1 Nginx部署
安裝約定
- 使用普通用戶
aiuap安裝。 - 確認服務(wù)器已安裝 C 環(huán)境。
安裝過程
上傳
nginx-1.8.0.tar.gz到aiuap目錄下。創(chuàng)建
nginx文件目錄:cd /opt mkdir nginx cd /home/aiuap
將
nginx安裝包移動到tmp目錄下,進行解壓:mv nginx-1.8.0.tar.gz /tmp cd /tmp tar zxvf nginx-1.8.0.tar.gz
安裝
nginx:cd nginx-1.8.0 ./configure --prefix=/home/csss/nginx
注意:可能需要安裝
pcre-devel和zlib-devel庫。編譯安裝:
make make install
Nginx配置
查看 nginx 安裝目錄:
cd /home/nginx ls
修改 nginx 配置文件 nginx.conf:
vi conf/nginx.conf
配置內(nèi)容(示例):
worker_processes 24;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream xxxx{
ip_hash;
server 192.168.1.101:8088 weight=1;
}
server {
listen 8099;
server_name localhost;
location / {
root html;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
proxy_pass http://xxxx/;
}
}
upstream csss {
ip_hash;
server 192.168.1.102:8080 weight=1;
}
server {
listen 9000;
server_name 192.168.1.102;
location / {
root html;
index index.html index.htm;
proxy_pass http://xxxx/;
}
}
}
1.3.2 Nginx+Keepalived實現(xiàn)雙機熱備
- 準備一個浮動 IP(如:
192.168.1.200)。 - 配置備
nginx與主nginx相同。 - 安裝
keepalived。
安裝Keepalived
安裝依賴庫:
yum -y install openssl-devel yum -y install ipvsadm yum -y install libnl libnl-devel yum -y install popt-devel
創(chuàng)建
keepalived安裝目錄:mkdir /home/keepalived
解壓安裝包并安裝:
tar -zxvf keepalived-1.2.2.tar.gz cd keepalived-1.2.2 ./configure --prefix=/home/test/keepalived make makeinstall
建立軟連接:
ln -s /home/test/keepalived/sbin/keepalived /sbin/ ln -s /home/test/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ mkdir /etc/keepalived ln -s /home/test/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
配置
keepalived:- 主服務(wù)器配置:
vi /home/test/keepalived/etc/keepalived/keepalived.conf
- 備服務(wù)器配置:
vi /home/keepalived/etc/keepalived/keepalived.conf
- 主服務(wù)器配置:
創(chuàng)建腳本
chk_nginx_pid.sh并賦予執(zhí)行權(quán)限:chmod +x /home/test/keepalived/chk_nginx_pid.sh
啟動
keepalived:- RHEL 7 以下:
keepalived -D -f /home/test/keepalived/etc/keepalived/keepalived.conf
- RHEL 7 以上:
systemctl start keepalived.service
- RHEL 7 以下:
停止
keepalived:- RHEL 7 以下:
service keepalived stop
- RHEL 7 以上:
systemctl stop keepalived.service
- RHEL 7 以下:
1.3.3 啟動和停止Nginx
啟動 Nginx:
/home/nginx/sbin/nginx
停止 Nginx:
從容停止:
kill -QUIT `cat /home/nginx/logs/nginx.pid`
快速停止:
kill -TERM `cat /home/nginx/logs/nginx.pid`
強制停止:
kill -9 `cat /home/nginx/logs/nginx.pid`
平滑重啟:
kill -HUP `cat /home/nginx/logs/nginx.pid`
1.4 使用和維護
- 日志位置:
<nginx安裝目錄>/logs/access.log - 雙機狀態(tài)查看:
ip addr show bond0
1.5 在已經(jīng)安裝的Nginx上增加SSL模塊
檢查 Nginx 版本和模塊:
/usr/local/nginx/sbin/nginx -V
下載并配置 Nginx 源碼:
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
編譯但不安裝:
make
停止 Nginx 并替換二進制文件:
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid` cp ~/download/nginx-1.10.3/objs/nginx /usr/local/nginx/sbin/
驗證模塊:
/usr/local/nginx/sbin/nginx -V
修改
nginx.conf文件以支持 SSL。
1.6 配置示例
SSL 配置:
server {
server_name example.com;
listen 443 ssl;
ssl_certificate /usr/local/nginx/conf/example.com_server.txt;
ssl_certificate_key /usr/local/nginx/conf/example.com_private.txt;
location / {
# 配置內(nèi)容
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name example.com;
rewrite ^(.*)$ https://example.com$1 permanent;
}
以上是對 Keepalived+Nginx 雙機配置的詳細指南,包括安裝、配置、啟動、停止和維護等步驟
1.6 Nginx基線配置
1.6.1 檢查是否隱藏nginx版本信息
server_tokens off;
1.6.2 檢查是否配置日志
修改 nginx.conf 文件
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
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;
error_log logs/error.log error;
1.6.3 檢查是否控制超時時間
修改 nginx.conf 文件
client_body_timeout 20s; # 設(shè)置客戶端請求主體讀取超時時間 client_header_timeout 10s; # 設(shè)置客戶端請求頭讀取超時時間 send_timeout 30s; # 服務(wù)端向客戶端傳輸數(shù)據(jù)的超時時間
1.6.4 檢查是否限制客戶端下載速度
修改 nginx.conf 文件
limit_conn_zone $binary_remote_addr zone=addr:10m; # 添加該行 limit_conn addr 50; # 每個客戶端允許50個線程。 limit_rate 1000k; # 每個線程最大下載速度1000k
1.6.5 檢查是否自定義nginx返回的錯誤信息
修改 nginx 配置文件
error_page 400 401 402 403 404 405 408 410 412 413 414 415 500 501 502 503 504 506 /50x.html;
location = /50x.html {
root html;
}
1.6.6 檢查是否配置防盜鏈設(shè)置
location ~* ^.+\.(aa|bb|cc)$ {
valid_referers none blocked 127.0.0.1;
if ($invalid_referer) {
return 403;
}
}
1.6.7 檢查是否限制IP訪問
deny 1.1.1.1; allow all;
1.7 完整樣例配置
worker_processes 4;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
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;
error_log logs/error.log error;
client_body_timeout 20s;
client_header_timeout 10s;
send_timeout 30s;
limit_conn_zone $binary_remote_addr zone=addr:10m;
sendfile on;
keepalive_timeout 65;
server_tokens off;
upstream xxxx {
ip_hash;
server 192.168.107.2:7001 weight=10;
server 192.168.107.3:7001 weight=10;
}
server {
listen 8080 default;
server_name _;
location / {
return 403;
}
}
server {
listen 8080;
server_name 192.168.107.2 192.168.107.4;
add_header Set-Cookie "HttpOnly=true";
set $flag 0;
if ( $host != '192.168.107.2' ) {
set $flag 1;
}
if ( $host != '192.168.107.4' ) {
set $flag $flag+1;
}
if ( $flag = 3 ) {
return 403;
}
location / {
root html;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
proxy_pass http://xxxx/;
limit_conn addr 50;
limit_rate 1000k;
deny 1.1.1.1;
allow all;
}
location ~* ^.+\.(aa|bb|cc)$ {
valid_referers none blocked 127.0.0.1;
if ($invalid_referer) {
return 403;
}
}
error_page 400 401 402 403 404 405 408 410 412 413 414 415 500 501 502 503 504 506 /50x.html;
location = /50x.html {
root html;
}
}
}
以上是 Nginx 的基線配置指南,包括隱藏版本信息、配置日志、控制超時時間、限制客戶端下載速度、自定義錯誤信息、配置防盜鏈設(shè)置和限制 IP 訪問等。
到此這篇關(guān)于Keepalived+Nginx雙機配置小結(jié)的文章就介紹到這了,更多相關(guān)Keepalived Nginx雙機配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx?Location映射規(guī)則總結(jié)歸納與最佳實踐
Nginx的location指令是配置請求路由的核心機制,其匹配規(guī)則直接影響請求的處理流程,下面給大家介紹Nginx?Location映射規(guī)則總結(jié)歸納,感興趣的朋友一起看看吧2025-06-06
HTTP 499 狀態(tài)碼 nginx下 499錯誤的解決辦法
HTTP狀態(tài)碼出現(xiàn)499錯誤有多種情況,499錯誤是什么?Nginx 499錯誤的原因及解決方法,下面跟著腳本之家小編一起學(xué)習(xí)吧2016-06-06

