Linux安裝配置反向 ProxyNginx教程,安裝配置nginx
應用情境
對於一個大型網站來說,負載平衡是永恒的話題,顧名思義,負載平衡即是將負載分攤到不同的服務單元,既保證服務的可用性,又保證響應足夠快,給使用者很好的體驗。隨著硬體技術的迅猛發展,越來越多的負載平衡硬體裝置湧現出來,如F5 BIG-IP、Citrix NetScaler、Radware等等,雖然可以解決問題,但其高昂的價格卻往往令人望而卻步,因此負載平衡軟體仍然是大部分公司的不二之選。Nginx作為webserver的後起之秀,其優秀的反向 Proxy功能和靈活的負載平衡策略受到了業界廣泛的關注。
Nginx進程基於Master + Slave(worker)多進程模型,自身具有非常穩定的子進程管理功能。在Master進程分配模式下,Master進程永遠不進行業務處理,只是進行任務分發,從而達到Master進程的存活高可靠性。
Keepalived是Linux下面實現VRRP 備份路由的高可靠性運行件。基於Keepalived設計的服務模式能夠真正做到主伺服器和備份伺服器故障時IP瞬間無縫交接。二者結合,可以構架出比較穩定的軟體負載平衡方案。
操作步驟1. Nginx安裝配置
1.1 系統基礎配置
關閉selinux # vi /etc/selinux/config……SELINUX=disabled……關閉防火牆,在Centos6.5中 # service iptables stop # chkconfig iptables off關閉防火牆,在Centos7中 # systemctl stop firewalld # systemctl disable firewalld修改系統檔案開啟限制數量,增加在設定檔最後 # vi /etc/security/limits.conf* soft noproc 65535* hard noproc 65535* soft nofile 409600* hard nofile 409600重啟機器 # reboot
1.2 設定時間同步
安裝ntpdate , ntpd # yum install -y ntpdate ntp pcre-devel pcre複製時區至本地時區 # cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime時間同步,ip請改成可用的時間伺服器的ip地址,並寫入時間戳記 # ntpdate ip # hwclock -w開啟ntpd服務,在Centos6中 # service start ntpd # service enable ntpd開啟ntpd服務,在Centos7中 # systemctl start ntpd # systemctl enable ntpd
1.3 安裝軟體包
安裝包https://pan.baidu.com/s/1hrUHlOw安裝相依元件 # yum install zlib zlib-devel # yum install pcre-devel將安裝包中的檔案拷貝到伺服器,並解壓縮安裝包 # tar -zxvf nginx-1.8.1.tar.gz然後進入目錄進行安裝 # cd nginx-1.8.1 #./configure --prefix=/opt/nginx1.8.1 # make # make install # ln -sf /opt/nginx1.8.1 /usr/local/nginx # echo 'export PATH=/usr/local/nginx/sbin:$PATH' >> /etc/profile && source /etc/profile
1.4 佈建服務
建立啟動指令碼將啟動指令檔夾中的nginx檔案發送到伺服器中並 # cp nginx /etc/init.d/ # chmod +x /etc/init.d/nginx修改使用者權限 # useradd -r -M nginx # mkdir -p /var/log/nginx # chown nginx -R /var/log/nginx註冊啟動服務 # chmod a+x /etc/init.d/nginx # chkconfig --add nginx # chkconfig nginx on
1.5 啟動服務
# service nginx start
2. 配置高可用
註:在主備兩台Nginx伺服器上都要安裝Keepalived。
2.1 安裝Keepalived
安裝依賴包,將keepalived6(Centos6環境中) 或 keepalived7(Centos7中)傳送到伺服器 # tar zxvf keepalived6.tar.gz 或tar zxvf keepalived7.tar.gz # yum localinstall keepalived/*.rpm -y將軟體源中的keepalived-1.2.16.tar.gz傳送到伺服器並解壓 # tar zxvf keepalived-1.2.16.tar.gz編譯安裝 # cd keepalived-1.2.16 # ./configure # make # make install佈建服務 # cp /usr/local/sbin/keepalived /usr/sbin/ # cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/ # cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ # chmod +x /etc/init.d/keepalived # chkconfig --add keepalived # chkconfig keepalived on # mkdir /etc/keepalived
2.2 配置主伺服器
建立設定檔 # vi /etc/keepalived/keepalived.conf修改設定檔並儲存! Configuration File for keepalivedglobal_defs { notification_email { #abc@example.com } #notification_email_from admin@example.com #smtp_server smtp.example.com #smtp_connect_timeout 30 router_id nginx_master}vrrp_script chk_http_port { script "
2.3 配置待命伺服器
建立設定檔 # vi /etc/keepalived/keepalived.conf修改設定檔並儲存! Configuration File for keepalivedglobal_defs { notification_email { #abc@example.com } #notification_email_from admin@example.com #smtp_server smtp.example.com #smtp_connect_timeout 30 router_id nginx_backup}vrrp_script chk_http_port { script "
2.4 安裝驗證
瀏覽器訪問https://ip出現以下提示說明nginx正常運行
查看keepalived綁定虛擬ip的情況 # ip a