Keepalived+Nginx雙機(jī)配置小結(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ù)器真實(shí) IP 和 hostname。
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 192.168.1.100 OSS-103
修改用戶進(jìn)程可打開(kāi)文件數(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ù)器時(shí)間同步:
- 查看時(shí)間:
date
- 手動(dòng)修改時(shí)間:
date –s “2015-5-8 19:48:00”
- 自動(dòng)更新時(shí)間:
ntpdate -u us.pool.ntp.org
1.3 Nginx+Keepalived部署
1.3.1 Nginx部署
安裝約定
- 使用普通用戶
aiuap
安裝。 - 確認(rèn)服務(wù)器已安裝 C 環(huán)境。
安裝過(guò)程
上傳
nginx-1.8.0.tar.gz
到aiuap
目錄下。創(chuàng)建
nginx
文件目錄:cd /opt mkdir nginx cd /home/aiuap
將
nginx
安裝包移動(dòng)到tmp
目錄下,進(jìn)行解壓: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
庫(kù)。編譯安裝:
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實(shí)現(xiàn)雙機(jī)熱備
- 準(zhǔn)備一個(gè)浮動(dòng) IP(如:
192.168.1.200
)。 - 配置備
nginx
與主nginx
相同。 - 安裝
keepalived
。
安裝Keepalived
安裝依賴庫(kù):
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
啟動(dòng)
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 啟動(dòng)和停止Nginx
啟動(dòng) Nginx:
/home/nginx/sbin/nginx
停止 Nginx:
從容停止:
kill -QUIT `cat /home/nginx/logs/nginx.pid`
快速停止:
kill -TERM `cat /home/nginx/logs/nginx.pid`
強(qiáng)制停止:
kill -9 `cat /home/nginx/logs/nginx.pid`
平滑重啟:
kill -HUP `cat /home/nginx/logs/nginx.pid`
1.4 使用和維護(hù)
- 日志位置:
<nginx安裝目錄>/logs/access.log
- 雙機(jī)狀態(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 并替換二進(jìn)制文件:
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid` cp ~/download/nginx-1.10.3/objs/nginx /usr/local/nginx/sbin/
驗(yàn)證模塊:
/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; }
以上是對(duì) Keepalived+Nginx 雙機(jī)配置的詳細(xì)指南,包括安裝、配置、啟動(dòng)、停止和維護(hù)等步驟
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 檢查是否控制超時(shí)時(shí)間
修改 nginx.conf
文件
client_body_timeout 20s; # 設(shè)置客戶端請(qǐng)求主體讀取超時(shí)時(shí)間 client_header_timeout 10s; # 設(shè)置客戶端請(qǐng)求頭讀取超時(shí)時(shí)間 send_timeout 30s; # 服務(wù)端向客戶端傳輸數(shù)據(jù)的超時(shí)時(shí)間
1.6.4 檢查是否限制客戶端下載速度
修改 nginx.conf
文件
limit_conn_zone $binary_remote_addr zone=addr:10m; # 添加該行 limit_conn addr 50; # 每個(gè)客戶端允許50個(gè)線程。 limit_rate 1000k; # 每個(gè)線程最大下載速度1000k
1.6.5 檢查是否自定義nginx返回的錯(cuò)誤信息
修改 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訪問(wèn)
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í)時(shí)間、限制客戶端下載速度、自定義錯(cuò)誤信息、配置防盜鏈設(shè)置和限制 IP 訪問(wèn)等。
到此這篇關(guān)于Keepalived+Nginx雙機(jī)配置小結(jié)的文章就介紹到這了,更多相關(guān)Keepalived Nginx雙機(jī)配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx?location和proxy_pass配置示例詳解
這篇文章主要介紹了Nginx?location和proxy_pass配置的相關(guān)資料,本文詳細(xì)探討了Nginx配置中`location`和`proxy_pass`指令的不同組合方式及其對(duì)請(qǐng)求轉(zhuǎn)發(fā)路徑的影響,通過(guò)列舉多種組合,展示了`location`匹配目錄與`proxy_pass`地址路徑如何相互作用,需要的朋友可以參考下2024-11-11Nginx配置實(shí)現(xiàn)高效精準(zhǔn)的流量限制策略詳解
限流(Rate?Limitting)是服務(wù)降級(jí)的一種方式,通過(guò)限制系統(tǒng)的輸入和輸出流量以達(dá)到保護(hù)系統(tǒng)的目的,下面我們就來(lái)看看如何通過(guò)配置Nginx實(shí)現(xiàn)高效精準(zhǔn)的流量限制策略吧2024-02-02Nginx 安裝筆記(含PHP支持、虛擬主機(jī)、反向代理負(fù)載均衡)
Nginx安裝簡(jiǎn)記(含PHP支持、虛擬主機(jī)、反向代理負(fù)載均衡) Nginx,據(jù)說(shuō)高性能和穩(wěn)定性比Apache還牛,并發(fā)連接處理能力強(qiáng),低系統(tǒng)資源消耗。目前已有250多萬(wàn)web站點(diǎn)在使用2009-10-10nginx部署前端項(xiàng)目的超級(jí)詳細(xì)步驟記錄
眾所周知Nginx是一款高性能的http服務(wù)器/反向代理服務(wù)器及電子郵件(IMAP/POP3)代理服務(wù)器,這篇文章主要給大家介紹了關(guān)于nginx部署前端項(xiàng)目的超級(jí)詳細(xì)步驟,需要的朋友可以參考下2023-02-02使用Nginx搭建流媒體服務(wù)器實(shí)現(xiàn)直播功能
這篇文章主要介紹了使用Nginx搭建流媒體服務(wù)器實(shí)現(xiàn)直播功能,本文通過(guò)實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07解析阿里云centos7服務(wù)器nginx配置及常見(jiàn)問(wèn)題解答
這篇文章主要介紹了阿里云centos7服務(wù)器nginx配置及常見(jiàn)問(wèn)題解答,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07