{"id":28884,"date":"2025-04-20T03:45:57","date_gmt":"2025-04-19T19:45:57","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/28884.html"},"modified":"2025-04-20T03:45:57","modified_gmt":"2025-04-19T19:45:57","slug":"keepalived-nginx-%e5%8f%8c%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%bb%e5%a4%87%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/28884.html","title":{"rendered":"keepalived + nginx \u53cc\u670d\u52a1\u5668\u4e3b\u5907\u65b9\u6848"},"content":{"rendered":"<h2>\u670d\u52a1\u5668\u90e8\u7f72\u65b9\u6848<\/h2>\n<h3>\u4e00\u3001keepalived &#043; nginx \u4e3b\u5907\u65b9\u6848<\/h3>\n<h4>1. \u67b6\u6784\u56fe<\/h4>\n<p>\u670d\u52a1\u5668\u4e3aCentOS 7\u7cfb\u7edf <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250419194555-6803fd73e450c.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>2. nginx \u90e8\u7f72<\/h4>\n<p>\u4e24\u53f0\u670d\u52a1\u5668\u5e94\u7528\u540c\u6837\u7684\u914d\u7f6e&#xff0c;\u4ec5HTML\u6587\u4ef6\u4e0d\u4e00\u6837\u3002<\/p>\n<h5>2.1 \u670d\u52a1\u5668\u73af\u5883\u521d\u59cb\u5316<\/h5>\n<p>&#xff08;1&#xff09;\u65f6\u95f4\u540c\u6b65<\/p>\n<p><span class=\"token comment\"># \u5b89\u88c5\u65f6\u95f4\u540c\u6b65\u5de5\u5177ntpdata<\/span><br \/>\nyum <span class=\"token parameter variable\">-y<\/span> <span class=\"token function\">install<\/span> ntp ntpdate<\/p>\n<p><span class=\"token comment\"># \u540c\u6b65\u7f51\u7edc\u65f6\u95f4<\/span><br \/>\nntpdate cn.pool.ntp.org<\/p>\n<p>&#xff08;2&#xff09;\u521b\u5efa\u4e00\u4e2acron\u4efb\u52a1&#xff0c;\u8bbe\u7f6e\u6bcf\u5929\u540c\u6b65\u670d\u52a1\u5668\u65f6\u95f4<\/p>\n<p><span class=\"token comment\"># \u6253\u5f00cron\u4f5c\u4e1a\u4efb\u52a1\u5217\u8868\u7684\u7f16\u8f91\u5668<\/span><br \/>\n<span class=\"token function\">crontab<\/span> <span class=\"token parameter variable\">-e<\/span><br \/>\n<span class=\"token comment\"># \u6bcf\u59290\u70b9\u540c\u6b65\u4e00\u6b21<\/span><br \/>\n* 00 * * *  \/usr\/sbin\/ntpdate cn.pool.ntp.org <\/p>\n<p>&#xff08;3&#xff09;\u5173\u95ed\u9632\u706b\u5899<\/p>\n<p>\u5728 windows \u7cfb\u7edf\u4e2d\u8bbf\u95ee linux \u4e2d nginx&#xff0c;\u9ed8\u8ba4\u4e0d\u80fd\u8bbf\u95ee\u7684&#xff0c;\u56e0\u4e3a\u9632\u706b\u5899\u95ee\u9898 &#xff08;1&#xff09;\u5173\u95ed\u9632\u706b\u5899 &#xff08;2&#xff09;\u5f00\u653e\u8bbf\u95ee\u7684\u7aef\u53e3\u53f7.<\/p>\n<p>iptables\u548cfirewalld\u662f\u4e24\u79cd\u4e0d\u540c\u7684\u9632\u706b\u5899&#xff0c;\u4e24\u4e2a\u90fd\u5173\u95ed&#xff0c;\u6709\u54ea\u4e2a\u5173\u54ea\u4e2a\u3002<\/p>\n<p>systemctl stop iptables<br \/>\nsystemctl stop firewalld<\/p>\n<p><span class=\"token comment\"># \u4e24\u79cd\u9632\u706b\u5899&#xff0c;\u4e2a\u4eba\u6bd4\u8f83\u4e60\u60ef\u4f7f\u7528iptables&#xff0c;\u540e\u9762\u505a\u7b56\u7565\u4e5f\u662f\u4f7f\u7528iptables&#xff1b;\u6ca1\u6709\u7684\u8bdd\u53ef\u4ee5\u5b89\u88c5\u4e00\u4e2a&#xff1b;<\/span><br \/>\nyum <span class=\"token function\">install<\/span> iptables-services   <span class=\"token comment\">#\u5b89\u88c5\u8fd9\u4e2a\u6bd4\u8f83\u65b9\u4fbf\u7ba1\u7406\u3002<\/span><\/p>\n<p>\u6216\u8005&#xff0c;\u8bbe\u7f6e\u5f00\u653e\u8bbf\u95ee\u7aef\u53e3\u53f7<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u5f00\u653e\u7684\u7aef\u53e3\u53f7<\/span><br \/>\nfirewall-cmd &#8211;list-all<\/p>\n<p><span class=\"token comment\"># \u8bbe\u7f6e\u5f00\u653e\u7684\u7aef\u53e3\u53f7<\/span><br \/>\nfirewall-cmd &#8211;add-service<span class=\"token operator\">&#061;<\/span>http \u2013permanent<br \/>\nfirewall-cmd &#8211;add-port<span class=\"token operator\">&#061;<\/span><span class=\"token number\">80<\/span>\/tcp <span class=\"token parameter variable\">&#8211;permanent<\/span> <\/p>\n<p><span class=\"token comment\"># \u91cd\u542f\u9632\u706b\u5899<\/span><br \/>\nfirewall-cmd <span class=\"token parameter variable\">-reload<\/span><\/p>\n<h5>2.2 \u5b89\u88c5 nginx<\/h5>\n<p>&#xff08;1&#xff09;\u5b89\u88c5\u4f9d\u8d56\u5305<\/p>\n<p>pcre-8.3.7.tar.gz&#xff0c; openssl-1.0.1t.tar.gz&#xff0c; zlib-1.2.8.tar.gz<\/p>\n<li>nginx\u7684http\u6a21\u5757\u4f7f\u7528pcre\u6765\u89e3\u6790\u6b63\u5219\u8868\u8fbe\u5f0f&#xff0c;\u9700\u8981\u5728linux\u4e0a\u5b89\u88c5pcre\u5e93<\/li>\n<li>nginx\u4f7f\u7528zlib\u5bf9http\u5305\u7684\u5185\u5bb9\u8fdb\u884cgzip&#xff0c;\u9700\u8981\u5728linux\u4e0a\u5b89\u88c5\u5b89\u88c5zlib\u5e93<\/li>\n<li>\u5b89\u88c5openssl\u5e93&#xff0c;\u8ba9 nginx \u652f\u6301 https&#xff08;\u5373\u5728ssl\u534f\u8bae\u4e0a\u4f20\u8f93http&#xff09;<\/li>\n<p>yum <span class=\"token parameter variable\">-y<\/span> <span class=\"token function\">install<\/span> gcc zlib zlib-devel pcre-devel openssl openssl-devel<\/p>\n<p>&#xff08;2&#xff09;\u4e0b\u8f7d\u5e76\u89e3\u538b\u5b89\u88c5\u5305<\/p>\n<p><span class=\"token comment\"># \u521b\u5efa\u4e00\u4e2anginx\u6587\u4ef6\u5939<\/span><br \/>\n<span class=\"token builtin class-name\">cd<\/span> \/usr\/local<br \/>\n<span class=\"token function\">mkdir<\/span> nginx<br \/>\n<span class=\"token builtin class-name\">cd<\/span> nginx<\/p>\n<p><span class=\"token comment\"># \u4e0b\u8f7dtar\u5305<\/span><br \/>\n<span class=\"token function\">wget<\/span> http:\/\/nginx.org\/download\/nginx-1.22.1.tar.gz<\/p>\n<p><span class=\"token comment\"># \u89e3\u538b<\/span><br \/>\n<span class=\"token function\">tar<\/span> <span class=\"token parameter variable\">-xvf<\/span> nginx-1.22.1.tar.gz<\/p>\n<p><span class=\"token comment\"># \u8fdb\u5165nginx-1.22\u76ee\u5f55<\/span><br \/>\n<span class=\"token builtin class-name\">cd<\/span> nginx-1.22.1<\/p>\n<p>&#xff08;2&#xff09;\u6dfb\u52a0 ssl \u8bc1\u4e66\u6a21\u5757<\/p>\n<p>.\/configure &#8211;with-http_stub_status_module &#8211;with-http_ssl_module<\/p>\n<p>&#xff08;3&#xff09;\u7f16\u8bd1\u5b89\u88c5<\/p>\n<ul>\n<li>-j 4 : \u6b64\u53c2\u6570\u4ee3\u8868\u75284\u6838\u5fc3\u7f16\u8bd1<\/li>\n<\/ul>\n<p><span class=\"token function\">make<\/span> <span class=\"token parameter variable\">-j<\/span> <span class=\"token number\">4<\/span> <span class=\"token operator\">&amp;&amp;<\/span> <span class=\"token function\">make<\/span> <span class=\"token function\">install<\/span><\/p>\n<p>&#xff08;4&#xff09;\u914d\u7f6e nginx.conf<\/p>\n<ul>\n<li>\u5982\u679c\u53ef\u4ee5\u6b63\u5e38\u542f\u52a8&#xff0c;\u6b63\u5e38\u8bbf\u95ee\u9875\u9762\u7684\u8bdd\u53ef\u4ee5\u5ffd\u7565\u6b64\u6b65\u9aa4<\/li>\n<\/ul>\n<p><span class=\"token comment\"># root html; \u6539\u6210\u81ea\u5df1\u7684\u8def\u5f84 \/usr\/local\/nginx\/html;<\/span><br \/>\n<span class=\"token comment\"># index \u540e\u9762\u52a0\u4e00\u4e2a index.php \u5426\u5219\u65e0\u6cd5\u663e\u793a\u7f51\u9875\u5185\u5bb9&#xff0c;\u62a5403\u9519\u8bef\u7684\u3002<\/span><br \/>\n    location \/ <span class=\"token punctuation\">{<\/span><br \/>\n       root   \/usr\/local\/nginx\/html<span class=\"token punctuation\">;<\/span><br \/>\n       index index.html index.php index.htm,<span class=\"token punctuation\">;<\/span><\/p>\n<ul>\n<li>\u5173\u95ed\u663e\u793anginx\u7248\u672c\u53f7&#xff08;\u6b64\u5185\u5bb9\u5728http\u6a21\u5757\u4e0b\u627e\u4e2a\u5730\u65b9\u5199&#xff09;<\/li>\n<\/ul>\n<p>server_tokens off<span class=\"token punctuation\">;<\/span><\/p>\n<p>&#xff08;5&#xff09;\u6821\u9a8c\u914d\u7f6e\u6587\u4ef6\u662f\u5426\u6b63\u786e<\/p>\n<p>\/usr\/local\/nginx\/sbin\/nginx <span class=\"token parameter variable\">-t<\/span> <\/p>\n<p><span class=\"token comment\"># \u770b\u5230\u5982\u4e0b\u663e\u793a\u4ee3\u8868\u6b63\u5e38&#xff1a;<\/span><br \/>\n<span class=\"token comment\"># nginx.conf syntax is ok<\/span><br \/>\n<span class=\"token comment\"># nginx.conf test is successful<\/span><\/p>\n<p>&#xff08;6&#xff09;\u542f\u52a8nginx<\/p>\n<p>\/usr\/local\/nginx\/sbin\/nginx <span class=\"token parameter variable\">-c<\/span> \/usr\/local\/nginx\/conf\/nginx.conf<br \/>\n\u91cd\u65b0\u52a0\u8f7d\u914d\u7f6e\u547d\u4ee4&#xff1a;<br \/>\n\/usr\/local\/nginx\/sbin\/nginx <span class=\"token parameter variable\">-s<\/span> reload<\/p>\n<p>&#xff08;7&#xff09;\u9a8c\u8bc1<\/p>\n<p>\u6d4f\u89c8\u5668\u9875\u9762\u8f93\u5165\u81ea\u5df1\u7684\u670d\u52a1\u5668\u5730\u5740\u51fa\u73b0nginx\u5b57\u6837\u7684\u7f51\u9875&#xff0c;\u8868\u793a\u5b89\u88c5\u6210\u529f&#xff1a;<\/p>\n<p>&#xff08;8&#xff09;\u4fee\u6539\u73af\u5883\u53d8\u91cf<\/p>\n<p>\u6dfb\u52a0\u73af\u5883\u53d8\u91cf&#xff0c;\u4f7fnginx\u5728\u4efb\u610f\u76ee\u5f55\u90fd\u53ef\u4ee5\u6267\u884c\u542f\u52a8\u64cd\u4f5c&#xff1b;<\/p>\n<p><span class=\"token comment\"># \u7f16\u8f91\/etc\/profile\u6587\u4ef6\u5728\u6700\u540e\u6dfb\u52a0\u5982\u4e0b\u547d\u4ee4&#xff1a;<\/span><br \/>\n<span class=\"token function\">vim<\/span> \/etc\/profile<\/p>\n<p><span class=\"token comment\"># \u6dfb\u52a0\u547d\u4ee4<\/span><br \/>\n<span class=\"token assign-left variable\"><span class=\"token environment constant\">PATH<\/span><\/span><span class=\"token operator\">&#061;<\/span><span class=\"token environment constant\">$PATH<\/span>:\/usr\/local\/nginx\/sbin<br \/>\n<span class=\"token builtin class-name\">export<\/span> PAT<\/p>\n<p><span class=\"token comment\"># \u4fdd\u5b58\u9000\u51fa<\/span><br \/>\n:wq<\/p>\n<p><span class=\"token comment\"># \u6267\u884c\u751f\u6548\u547d\u4ee4<\/span><br \/>\n<span class=\"token builtin class-name\">source<\/span> \/etc\/profile <\/p>\n<p>\u6240\u6709\u7684\u670d\u52a1\u5668\u90e8\u7f72\u65b9\u6cd5\u76f8\u540c<\/p>\n<h4>3. keepalived \u90e8\u7f72<\/h4>\n<p>&#xff08;1&#xff09;\u5b89\u88c5\u4f9d\u8d56<\/p>\n<p>yum <span class=\"token function\">install<\/span> <span class=\"token parameter variable\">-y<\/span> gcc openssl-devel libnl3-devel net-snmp-devel<\/p>\n<p>&#xff08;2&#xff09;\u5b89\u88c5keepalived<\/p>\n<p>yum <span class=\"token function\">install<\/span>  keepalived <span class=\"token parameter variable\">-y<\/span><\/p>\n<ul>\n<li>\u914d\u7f6e\u6587\u4ef6\u7684\u4f4d\u7f6e&#xff1a;\/etc\/keepalived\/<\/li>\n<\/ul>\n<h5>3.1 \u4e3b keepalived \u914d\u7f6e\u6587\u4ef6\u89e3\u6790<\/h5>\n<ul>\n<li>\u9700\u8981\u8bbe\u7f6e\u4e00\u4e2aVIP\u7684\u5730\u5740&#xff0c;\u4f5c\u4e3a\u865a\u62dfIP\u5730\u5740<\/li>\n<\/ul>\n<p><span class=\"token builtin class-name\">cd<\/span> \/etc\/keepalived\/<br \/>\n<span class=\"token function\">vim<\/span> keepalived.conf<\/p>\n<p><span class=\"token comment\"># &#8212; &#8212;- keepalived.conf &#8212; &#8212;-<\/span><\/p>\n<p><span class=\"token operator\">!<\/span> Configuration File <span class=\"token keyword\">for<\/span> keepalived<\/p>\n<p><span class=\"token comment\"># \u7ba1\u7406\u5458\u90ae\u7bb1&#xff08;\u7cfb\u7edf\u6545\u969c\u901a\u77e5&#xff09;<\/span><br \/>\nglobal_defs <span class=\"token punctuation\">{<\/span><br \/>\n notification_email <span class=\"token punctuation\">{<\/span><br \/>\n acassen&#064;firewall.loc<br \/>\n failover&#064;firewall.loc<br \/>\nsysadmin&#064;firewall.loc<br \/>\n <span class=\"token punctuation\">}<\/span><\/p>\n<p>notification_email_from Alexandre.Cassen&#064;firewall.loc   <span class=\"token comment\"># \u90ae\u4ef6\u53d1\u4ef6\u4eba\u90ae\u7bb1<\/span><br \/>\nsmtp_server <span class=\"token number\">127.0<\/span>.0.1<span class=\"token comment\"># SMTP\u670d\u52a1\u5668\u5730\u5740(\u90ae\u7bb1\u670d\u52a1\u5668\u5730\u5740)<\/span><br \/>\nsmtp_connect_timeout <span class=\"token number\">30<\/span><span class=\"token comment\">#\u8d85\u65f6\u65f6\u95f4<\/span><br \/>\nrouter_id NGINX<span class=\"token comment\"># \u8def\u7531\u5668\u6807\u8bc6&#xff0c;\u4e00\u822c\u4e0d\u7528\u6539&#xff0c;\u4e5f\u53ef\u6539\u6210\u5e94\u7528\u540d&#xff0c;\u4e3b\u4ece\u670d\u52a1\u5668\u8981\u4e00\u81f4<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">#\u4e00\u4e2avrrp_instance\u5c31\u662f\u5b9a\u4e49\u4e00\u4e2a\u865a\u62df\u8def\u7531\u5668\u7684&#xff0c;\u5b9e\u4f8b\u540d\u79f0<\/span><br \/>\nvrrp_script check_nginx <span class=\"token punctuation\">{<\/span> <span class=\"token comment\">#\u811a\u672c\u6a21\u5757<\/span><br \/>\nscript <span class=\"token string\">&#034;\/etc\/keepalived\/check_nginx.sh&#034;<\/span>  <span class=\"token comment\">#\u811a\u672c\u6a21\u5757<\/span><br \/>\ninterval <span class=\"token number\">2<\/span>  <span class=\"token comment\">#\u6bcf2\u79d2\u949f\u68c0\u6d4b\u4e00\u6b21\u811a\u672c<\/span><br \/>\n<span class=\"token comment\">#weight 10    #\u4f18\u5148\u7ea7<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>vrrp_instance VI_1 <span class=\"token punctuation\">{<\/span><br \/>\nstate MASTER<span class=\"token comment\"># \u5b9a\u4e49\u521d\u59cb\u72b6\u6001&#xff0c;\u53ef\u4ee5\u662fMASTER\u6216\u8005BACKUP<\/span><br \/>\ninterface eth0<span class=\"token comment\">#\u5f53\u524d\u4f7f\u7528\u7684\u7f51\u5361\u540d<\/span><br \/>\nvirtual_router_id <span class=\"token number\">51<\/span><span class=\"token comment\">#\u865a\u62df\u8def\u7531id\u53f7<\/span><br \/>\npriority <span class=\"token number\">100<\/span> <span class=\"token comment\">#\u5f53\u524d\u670d\u52a1\u5668\u4f18\u5148\u7ea7&#xff0c;\u6570\u5b57\u8d8a\u5927\u8d8a\u4f18\u5148<\/span><br \/>\nadvert_int <span class=\"token number\">1<\/span><span class=\"token comment\">#\u68c0\u6d4b\u5fc3\u8df3\u65f6\u95f4\u4e3a1\u79d2<\/span><br \/>\nauthentication <span class=\"token punctuation\">{<\/span><span class=\"token comment\">#\u8eab\u4efd\u9a8c\u8bc1\u65b9\u5f0f\u901a\u4fe1\u8ba4\u8bc1\u673a\u5236&#xff0c;\u8fd9\u91cc\u662f\u660e\u6587\u8ba4\u8bc1\u8fd8\u6709\u4e00\u79cd\u662f\u52a0\u5bc6\u8ba4\u8bc1<\/span><br \/>\n    auth_type PASS<span class=\"token comment\">#\u5bc6\u7801\u9a8c\u8bc1<\/span><br \/>\n    auth_pass <span class=\"token number\">1111<\/span><span class=\"token comment\">#\u5bc6\u7801\u4e3a1111<\/span><\/p>\n<p>    <span class=\"token comment\">## \u5982\u679c\u4f7f\u7528\u52a0\u5bc6\u8ba4\u8bc1&#xff08;\u6240\u6709\u8282\u70b9\u5fc5\u987b\u4e00\u81f4&#xff09;<\/span><br \/>\n    <span class=\"token comment\"># auth_type AH<\/span><br \/>\n    <span class=\"token comment\"># auth_pass &lt;key&gt;  # \u901a\u5e38\u4e3a\u4e00\u4e2a\u957f\u5ea6\u4e3a8\u6216\u800516\u7684\u5b57\u7b26\u4e32<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\nvirtual_ipaddress <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token number\">192.168<\/span>.10.100     <span class=\"token comment\">#VIP\u5730\u5740<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\ntrack_script<span class=\"token punctuation\">{<\/span>        <span class=\"token comment\">#\u6dfb\u52a0\u76d1\u63a7\u6761\u4ef6<\/span><br \/>\ncheck_nginx <span class=\"token comment\">#\u811a\u672c\u6a21\u5757\u540e\u8fb9\u5b9a\u4e49\u7684\u540d\u79f0<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h5>3.2 \u76d1\u63a7nginx\u72b6\u6001<\/h5>\n<p>\u65b0\u5efa\u4e00\u4e2a\u811a\u672c\u6587\u4ef6&#xff0c;\u5e76\u6253\u5f00<\/p>\n<p><span class=\"token function\">vim<\/span> \/etc\/keepalived\/check_nginx.sh<\/p>\n<p>\u7f16\u5199\u76d1\u63a7\u811a\u672c<\/p>\n<p><span class=\"token shebang important\">#!\/bin\/bash<\/span><br \/>\n<span class=\"token comment\">#1\u3001\u5224\u65ad Nginx \u662f\u5426\u5b58\u6d3b<\/span><br \/>\n<span class=\"token assign-left variable\">counter<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token variable\"><span class=\"token variable\">&#096;<\/span><span class=\"token function\">ps<\/span> <span class=\"token parameter variable\">-C<\/span> nginx &#8211;no-header <span class=\"token operator\">|<\/span> <span class=\"token function\">wc<\/span> <span class=\"token parameter variable\">-l<\/span><span class=\"token variable\">&#096;<\/span><\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">[<\/span> <span class=\"token variable\">$counter<\/span> <span class=\"token parameter variable\">-eq<\/span> <span class=\"token number\">0<\/span> <span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token keyword\">then<\/span><br \/>\n<span class=\"token comment\">#2\u3001\u5982\u679c\u4e0d\u5b58\u6d3b\u5219\u5c1d\u8bd5\u542f\u52a8 Nginx<\/span><br \/>\n\/usr\/local\/nginx\/sbin\/nginx<br \/>\n<span class=\"token function\">sleep<\/span> <span class=\"token number\">5<\/span><br \/>\n<span class=\"token comment\">#3\u3001\u7b49\u5f85 5 \u79d2\u540e\u518d\u6b21\u83b7\u53d6\u4e00\u6b21 Nginx \u72b6\u6001<\/span><br \/>\n<span class=\"token assign-left variable\">counter<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token variable\"><span class=\"token variable\">&#096;<\/span><span class=\"token function\">ps<\/span> <span class=\"token parameter variable\">-C<\/span> nginx &#8211;no-header <span class=\"token operator\">|<\/span> <span class=\"token function\">wc<\/span> <span class=\"token parameter variable\">-l<\/span><span class=\"token variable\">&#096;<\/span><\/span><br \/>\n<span class=\"token comment\">#4\u3001\u518d\u6b21\u8fdb\u884c\u5224\u65ad&#xff0c;\u5982 Nginx \u8fd8\u4e0d\u5b58\u6d3b\u5219\u505c\u6b62 Keepalived&#xff0c;\u8ba9\u5730\u5740\u8fdb\u884c\u6f02\u79fb<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">[<\/span> <span class=\"token variable\">$counter<\/span> <span class=\"token parameter variable\">-eq<\/span> <span class=\"token number\">0<\/span> <span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token keyword\">then<\/span><br \/>\nsystemctl stop keepalived  <span class=\"token comment\">#\u505c\u7528keepalived\u670d\u52a1<\/span><br \/>\n<span class=\"token keyword\">fi<\/span><br \/>\n<span class=\"token keyword\">fi<\/span><\/p>\n<p>\u6ce8\u610f&#xff0c;\u8fd9\u91cc\u7684\u91cd\u542fnginx\u547d\u4ee4\/usr\/local\/nginx\/sbin\/nginx\u88ab\u6ce8\u91ca\u6389&#xff0c;\u6d4b\u8bd5\u65f6\u5173\u95ed\u624d\u80fd\u770b\u5230\u6548\u679c\u3002<\/p>\n<h5>3.3 \u4ece keepalived\u914d\u7f6e<\/h5>\n<p><span class=\"token operator\">!<\/span> Configuration File <span class=\"token keyword\">for<\/span> keepalived<\/p>\n<p>global_defs <span class=\"token punctuation\">{<\/span><br \/>\nnotification_email <span class=\"token punctuation\">{<\/span><br \/>\n acassen&#064;firewall.loc<br \/>\n failover&#064;firewall.loc<br \/>\n sysadmin&#064;firewall.loc<br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\nnotification_email_from Alexandre.Cassen&#064;firewall.loc<br \/>\nsmtp_server <span class=\"token number\">127.0<\/span>.0.1<br \/>\nsmtp_connect_timeout <span class=\"token number\">30<\/span><br \/>\nrouter_id NGINX<br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>vrrp_script check_nginx <span class=\"token punctuation\">{<\/span>  <span class=\"token comment\">#\u8fd9\u4e2a\u5730\u65b9\u540d\u5b57\u4e0d\u80fd\u4e71\u6539&#xff0c;\u7cfb\u7edf\u4e0d\u8ba4\u8bc6&#xff0c;<\/span><br \/>\nscript <span class=\"token string\">&#034;\/etc\/keepalived\/check_nginx.sh&#034;<\/span><br \/>\ninterval <span class=\"token number\">2<\/span>  <span class=\"token comment\">#\u6bcf2\u79d2\u949f\u68c0\u6d4b\u4e00\u6b21\u811a\u672c<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>vrrp_instance VI_1 <span class=\"token punctuation\">{<\/span><br \/>\nstate BACKUP<span class=\"token comment\">#\u6539\u8fd9\u91cc<\/span><br \/>\ninterface eth0<br \/>\nvirtual_router_id <span class=\"token number\">51<\/span><br \/>\npriority <span class=\"token number\">80<\/span><span class=\"token comment\">#\u6539\u8fd9\u91cc<\/span><br \/>\nadvert_int <span class=\"token number\">1<\/span><br \/>\nauthentication <span class=\"token punctuation\">{<\/span><br \/>\n    auth_type PASS<br \/>\n    auth_pass <span class=\"token number\">1111<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\nvirtual_ipaddress <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token number\">172.28<\/span>.127.100<br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\ntrack_script <span class=\"token punctuation\">{<\/span><br \/>\ncheck_nginx<br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>keepalived\u76f8\u5173\u547d\u4ee4<\/p>\n<p>\u542f\u52a8&#xff1a;systemctl start keepalived<br \/>\n\u91cd\u542f&#xff1a;systemctl restart keepalived<br \/>\n\u52a8\u6001\u6301\u7eed\u67e5\u770b\u65e5\u5fd7&#xff1a;tail <span class=\"token parameter variable\">-f<\/span> \/var\/log\/messages<\/p>\n<h3>\u4e8c\u3001nginx \u7aef\u53e3\u8f6c\u53d1\/\u53cd\u5411\u4ee3\u7406<\/h3>\n<h4>1. nginx \u7aef\u53e3\u8f6c\u53d1<\/h4>\n<h5>1.1 \u4fee\u6539nginx.conf<\/h5>\n<p>server <span class=\"token punctuation\">{<\/span><br \/>\n    listen      <span class=\"token number\">19100<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\"># \u76d1\u542c19180\u7aef\u53e3<\/span><br \/>\n    server_name  <span class=\"token number\">192.168<\/span>.248.10<span class=\"token punctuation\">;<\/span><\/p>\n<p>    location \/ <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token comment\"># \u8bf7\u6c42\u5934\u8f6c\u53d1<\/span><br \/>\n            proxy_set_header Host <span class=\"token variable\">$host<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token comment\"># \u83b7\u5f97\u771f\u5b9eip<\/span><br \/>\n            proxy_set_header X-Real-IP <span class=\"token variable\">$remote_addr<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            proxy_set_header X-Forwarded-For <span class=\"token variable\">$proxy_add_x_forwarded_for<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token comment\"># \u8f6c\u53d1\u5230\u76ee\u6807\u5730\u5740<\/span><br \/>\n            proxy_pass http:\/\/192.168.0.100:60080<span class=\"token punctuation\">;<\/span><\/p>\n<p>            <span class=\"token comment\"># \u8fd8\u53ef\u4ee5\u5199root\u548cindex\u5b57\u6bb5&#xff0c;\u53ea\u4e0d\u8fc7\u7aef\u53e3\u9ed8\u8ba4\u4f18\u5148\u8f6c\u53d1&#xff0c;\u5f53\u8f6c\u53d1\u4e0d\u6210\u529f\u65f6&#xff0c;\u624d\u4f1a\u9009\u62e9\u8d70root\u5b57\u6bb5\u4e0b\u7684\u76ee\u5f55\u548chtml<\/span><br \/>\n            <span class=\"token comment\"># root \/usr\/local\/nginx\/html;<\/span><br \/>\n            <span class=\"token comment\"># index index.html index.php index.htm;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token comment\"># &#8230; &#8230;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\"># \u8fd9\u4e2a60086\u7aef\u53e3&#xff0c;\u76ee\u524d\u6ca1\u6709\u670d\u52a1&#xff0c;\u8bbf\u95ee\u662f\u88ab\u62d2\u7edd\u7684&#xff01;<\/span><\/p>\n<p>\u4fdd\u5b58\u9000\u51fa<\/p>\n<p>nginx <span class=\"token parameter variable\">-s<\/span> reload<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\"># \u8f93\u5165vip\u5730\u5740\u8bbf\u95ee<\/span><br \/>\n<span class=\"token number\">192.168<\/span>.248.10:19180<\/p>\n<h5>1.2 \u5206\u79bbnginx\u4e2d\u7684server\u6a21\u5757\u4e3a\u5355\u72ec\u7684\u6587\u4ef6<\/h5>\n<p><span class=\"token comment\"># \u5728nginx\u914d\u7f6e\u7684\u6700\u540e\u4e00\u4e2a\u4e2d\u62ec\u53f7\u4e0a\u65b9\u6dfb\u52a0\u5982\u4e0b\u547d\u4ee4<\/span><br \/>\ninclude \/usr\/local\/nginx\/conf\/conf.d\/*.conf<span class=\"token punctuation\">;<\/span><\/p>\n<p>\u53e6\u5916\u5728conf.d\u6587\u4ef6\u5939\u4e0b\u65b0\u5efaconf\u6587\u4ef6&#xff0c;\u5355\u72ec\u7f16\u5199server\u6a21\u5757\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\u914d\u7f6e\u5c31\u544a\u4e00\u6bb5\u843d\u4e86&#xff0c;\u540e\u9762\u7b97\u662f\u8865\u5145\u7684 nginx \u914d\u7f6e<\/p>\n<hr \/>\n<h4>2. nginx \u591a\u7aef\u53e3\u8f6c\u53d1<\/h4>\n<h5>2.1 \u914d\u7f6e\u6587\u4ef6<\/h5>\n<ul>\n<li>\u6bcf\u4e2a http \u5757\u53ef\u4ee5\u5305\u62ec\u591a\u4e2a server \u5757&#xff0c;\u800c\u6bcf\u4e2a server \u5757\u5c31\u76f8\u5f53\u4e8e\u4e00\u4e2a\u865a\u62df\u4e3b\u673a\u3002<\/li>\n<li>\u800c\u6bcf\u4e2a server \u5757\u4e5f\u5206\u4e3a\u5168\u5c40 server \u5757&#xff0c;\u4ee5\u53ca\u53ef\u4ee5\u540c\u65f6\u5305\u542b\u591a\u4e2a locaton \u5757\u3002<\/li>\n<\/ul>\n<p>server <span class=\"token punctuation\">{<\/span><br \/>\nlisten<span class=\"token number\">8001<\/span><span class=\"token punctuation\">;<\/span><br \/>\nserver_name<span class=\"token number\">208.208<\/span>.128.122<span class=\"token punctuation\">;<\/span><\/p>\n<p>location ~\/vod\/ <span class=\"token punctuation\">{<\/span><br \/>\nproxy_passhttp:\/\/127.0.0.1:8081<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>location ~\/edu\/ <span class=\"token punctuation\">{<\/span><br \/>\nproxy_passhttp:\/\/127.0.0.1:8082<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h5>2.1 \u5339\u914d\u89c4\u5219<\/h5>\n<li>&#061; &#xff1a;\u7528\u4e8e\u4e0d\u542b\u6b63\u5219\u8868\u8fbe\u5f0f\u7684 uri \u524d&#xff0c;\u8981\u6c42\u8bf7\u6c42\u5b57\u7b26\u4e32\u4e0e uri \u4e25\u683c\u5339\u914d&#xff0c;\u5982\u679c\u5339\u914d \u6210\u529f&#xff0c;\u5c31\u505c\u6b62\u7ee7\u7eed\u5411\u4e0b\u641c\u7d22\u5e76\u7acb\u5373\u5904\u7406\u8be5\u8bf7\u6c42\u3002<\/li>\n<li>~&#xff1a;\u7528\u4e8e\u8868\u793a uri \u5305\u542b\u6b63\u5219\u8868\u8fbe\u5f0f&#xff0c;\u5e76\u4e14\u533a\u5206\u5927\u5c0f\u5199\u3002<\/li>\n<li>~*&#xff1a;\u7528\u4e8e\u8868\u793a uri \u5305\u542b\u6b63\u5219\u8868\u8fbe\u5f0f&#xff0c;\u5e76\u4e14\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u3002<\/li>\n<li>^~&#xff1a;\u7528\u4e8e\u4e0d\u542b\u6b63\u5219\u8868\u8fbe\u5f0f\u7684 uri \u524d&#xff0c;\u8981\u6c42 Nginx \u670d\u52a1\u5668\u627e\u5230\u6807\u8bc6 uri \u548c\u8bf7\u6c42\u5b57 \u7b26\u4e32\u5339\u914d\u5ea6\u6700\u9ad8\u7684 location \u540e&#xff0c;\u7acb\u5373\u4f7f\u7528\u6b64 location \u5904\u7406\u8bf7\u6c42&#xff0c;\u800c\u4e0d\u518d\u4f7f\u7528 location \u5757\u4e2d\u7684\u6b63\u5219 uri \u548c\u8bf7\u6c42\u5b57\u7b26\u4e32\u505a\u5339\u914d\u3002<\/li>\n<h5>2.3 \u5b9e\u73b0\u6548\u679c<\/h5>\n<ul>\n<li>\u5982\u679c\u8bf7\u6c42208.208.128.122:8001\/vod\/&#xff0c;\u5c31\u4f1a\u81ea\u52a8\u8df3\u8f6c\u5230127.0.0.1:8081<\/li>\n<li>\u5982\u679c\u8bf7\u6c42208.208.128.122:8001\/edu\/&#xff0c;\u5c31\u4f1a\u81ea\u52a8\u8df3\u8f6c\u5230127.0.0.1:8082<\/li>\n<\/ul>\n<h3>\u56db\u3001nginx \u8d1f\u8f7d\u5747\u8861<\/h3>\n<h4>1. \u914d\u7f6e\u8bf4\u660e<\/h4>\n<h5>1.1 \u4fee\u6539 nginx.conf<\/h5>\n<p>    <span class=\"token comment\"># \u5b9a\u4e49myserver\u7ed3\u6784\u4f53<\/span><br \/>\n    upstream myserver <span class=\"token punctuation\">{<\/span><br \/>\n        server <span class=\"token number\">208.208<\/span>.128.122:8081<span class=\"token punctuation\">;<\/span><br \/>\n        server <span class=\"token number\">208.208<\/span>.128.122:8082<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    server <span class=\"token punctuation\">{<\/span><br \/>\n        listen       <span class=\"token number\">80<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        server_name  <span class=\"token number\">208.208<\/span>.128.122<span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">#charset koi8-r;<\/span><\/p>\n<p>        <span class=\"token comment\">#access_log  logs\/host.access.log  main;<\/span><\/p>\n<p>        location \/ <span class=\"token punctuation\">{<\/span><br \/>\n            root   html<span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token comment\"># \u5728\u8fd9\u91cc\u8c03\u7528<\/span><br \/>\n            <span class=\"token comment\">#proxy_pass   http:\/\/myserver;<\/span><br \/>\n            proxy_pass   http:\/\/127.0.0.1:8081<span class=\"token punctuation\">;<\/span><br \/>\n            index  index.html index.htm<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<h5>1.2 \u5b9e\u73b0\u6548\u679c<\/h5>\n<ul>\n<li>\u8bf7\u6c42208.208.128.122:80\/edu\/&#xff0c;\u8d1f\u8f7d\u5747\u8861\u5e73\u5747\u5230127.0.0.1:8081\/edu\/\u548c127.0.0.1:8082\/edu\/\u5f53\u4e2d\u3002<\/li>\n<\/ul>\n<h4>2. \u4fee\u6539\u8d1f\u8f7d\u5747\u8861\u5206\u914d\u7b56\u7565<\/h4>\n<ul>\n<li>\u8f6e\u8be2&#xff08;\u9ed8\u8ba4&#xff09;&#xff1a;\u6309\u65f6\u95f4\u987a\u5e8f\u9010\u4e00\u5206\u914d&#xff0c;\u5982\u679cdown\u6389&#xff0c;\u81ea\u52a8\u5254\u9664<\/li>\n<li>weight&#xff1a;\u9ed8\u8ba4\u4e3a1&#xff0c;\u6743\u91cd\u8d8a\u9ad8\u5206\u914d\u5ba2\u6237\u8d8a\u591a\u3002<\/li>\n<\/ul>\n<p>upstream myserver <span class=\"token punctuation\">{<\/span><br \/>\n    server <span class=\"token number\">208.208<\/span>.128.122:8081 <span class=\"token assign-left variable\">weight<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token number\">10<\/span><span class=\"token punctuation\">;<\/span>   <span class=\"token comment\">#  \u5728\u8fd9\u513f<\/span><br \/>\n    server <span class=\"token number\">208.208<\/span>.128.122:8082 <span class=\"token assign-left variable\">weight<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token number\">10<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\nserver <span class=\"token punctuation\">{<\/span><br \/>\n    listen       <span class=\"token number\">80<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    server_name  <span class=\"token number\">208.208<\/span>.128.122<span class=\"token punctuation\">;<\/span><br \/>\n    location \/ <span class=\"token punctuation\">{<\/span><br \/>\n        root   html<span class=\"token punctuation\">;<\/span><br \/>\n        proxy_pass   http:\/\/myserver<span class=\"token punctuation\">;<\/span><br \/>\n        index  index.html index.htm<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<ul>\n<li>ip_hash&#xff1a;\u6bcf\u4e2a\u8bf7\u6c42\u6309\u8bbf\u95ee ip \u7684 hash \u7ed3\u679c\u5206\u914d&#xff0c;\u8fd9\u6837\u6bcf\u4e2a\u8bbf\u5ba2\u56fa\u5b9a\u8bbf\u95ee\u4e00\u4e2a\u540e\u7aef\u670d\u52a1\u5668\u3002<\/li>\n<\/ul>\n<p>upstream myserver <span class=\"token punctuation\">{<\/span><br \/>\n    ip_hash<span class=\"token punctuation\">;<\/span><span class=\"token comment\">#  \u5728\u8fd9\u513f<\/span><br \/>\n    server <span class=\"token number\">208.208<\/span>.128.122:8081 <span class=\"token punctuation\">;<\/span><br \/>\n    server <span class=\"token number\">208.208<\/span>.128.122:8082 <span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\nserver <span class=\"token punctuation\">{<\/span><br \/>\n    listen       <span class=\"token number\">80<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    server_name  <span class=\"token number\">208.208<\/span>.128.122<span class=\"token punctuation\">;<\/span><br \/>\n    location \/ <span class=\"token punctuation\">{<\/span><br \/>\n        root   html<span class=\"token punctuation\">;<\/span><br \/>\n        proxy_pass   http:\/\/myserver<span class=\"token punctuation\">;<\/span><br \/>\n        index  index.html index.htm<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<ul>\n<li>fair&#xff08;\u7b2c\u4e09\u65b9&#xff09;&#xff1a;\u6309\u540e\u7aef\u670d\u52a1\u5668\u7684\u54cd\u5e94\u65f6\u95f4\u6765\u5206\u914d\u8bf7\u6c42&#xff0c;\u54cd\u5e94\u65f6\u95f4\u77ed\u7684\u4f18\u5148\u5206\u914d\u3002<\/li>\n<\/ul>\n<p>    upstream myserver <span class=\"token punctuation\">{<\/span><br \/>\n        server <span class=\"token number\">208.208<\/span>.128.122:8081 <span class=\"token punctuation\">;<\/span><br \/>\n        server <span class=\"token number\">208.208<\/span>.128.122:8082 <span class=\"token punctuation\">;<\/span><br \/>\n        fair<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">#  \u5728\u8fd9\u513f<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    server <span class=\"token punctuation\">{<\/span><br \/>\n        listen       <span class=\"token number\">80<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        server_name  <span class=\"token number\">208.208<\/span>.128.122<span class=\"token punctuation\">;<\/span><br \/>\n        location \/ <span class=\"token punctuation\">{<\/span><br \/>\n            root   html<span class=\"token punctuation\">;<\/span><br \/>\n            proxy_pass   http:\/\/myserver<span class=\"token punctuation\">;<\/span><br \/>\n            index  index.html index.htm<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<h3>\u4e94\u3001\u67b6\u6784<\/h3>\n<h4>1. \u52a8\u9759\u5206\u79bb<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250419194556-6803fd745633e.png\" alt=\"img\" \/><\/p>\n<p>Nginx \u52a8\u9759\u5206\u79bb\u7b80\u5355\u6765\u8bf4\u5c31\u662f\u628a\u52a8\u6001\u8ddf\u9759\u6001\u8bf7\u6c42\u5206\u5f00&#xff0c;\u4e0d\u80fd\u7406\u89e3\u6210\u53ea\u662f\u5355\u7eaf\u7684\u628a\u52a8\u6001\u9875\u9762\u548c \u9759\u6001\u9875\u9762\u7269\u7406\u5206\u79bb\u3002\u4e25\u683c\u610f\u4e49\u4e0a\u8bf4\u5e94\u8be5\u662f\u52a8\u6001\u8bf7\u6c42\u8ddf\u9759\u6001\u8bf7\u6c42\u5206\u5f00&#xff0c;\u53ef\u4ee5\u7406\u89e3\u6210\u4f7f\u7528 Nginx \u5904\u7406\u9759\u6001\u9875\u9762&#xff0c;Tomcat \u5904\u7406\u52a8\u6001\u9875\u9762\u3002\u52a8\u9759\u5206\u79bb\u4ece\u76ee\u524d\u5b9e\u73b0\u89d2\u5ea6\u6765\u8bb2\u5927\u81f4\u5206\u4e3a\u4e24\u79cd&#xff1a;<\/p>\n<ul>\n<li>\u4e00\u79cd\u662f\u7eaf\u7cb9\u628a\u9759\u6001\u6587\u4ef6\u72ec\u7acb\u6210\u5355\u72ec\u7684\u57df\u540d&#xff0c;\u653e\u5728\u72ec\u7acb\u7684\u670d\u52a1\u5668\u4e0a&#xff0c;\u4e5f\u662f\u76ee\u524d\u4e3b\u6d41\u63a8\u5d07\u7684\u65b9\u6848&#xff1b;<\/li>\n<li>\u53e6\u5916\u4e00\u79cd\u65b9\u6cd5\u5c31\u662f\u52a8\u6001\u8ddf\u9759\u6001\u6587\u4ef6\u6df7\u5408\u5728\u4e00\u8d77\u53d1\u5e03&#xff0c;\u901a\u8fc7 nginx \u6765\u5206\u5f00\u3002<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250419194556-6803fd7480c0d.png\" alt=\"img\" \/><\/p>\n<h4>2. \u9ad8\u53ef\u7528\u96c6\u7fa4<\/h4>\n<p>keepalived &#043; nginx \u6a21\u5f0f<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250419194556-6803fd74a9b78.png\" alt=\"img\" \/><\/p>\n<h4>3. \u5176\u4ed6<\/h4>\n<ul>\n<li>master-worker\u67b6\u6784&#xff0c;\u53ea\u80fd\u6709\u4e00\u4e2amaster&#xff0c;\u5176\u4ed6worker\u901a\u8fc7\u4e89\u62a2\u83b7\u53d6\u8bf7\u6c42\u3002<\/li>\n<li>worker\u6570\u548cCPU\u6570\u76f8\u7b49\u6700\u4e3a\u9002\u5b9c\u3002<\/li>\n<li>\u53d1\u9001\u8bf7\u6c42&#xff0c;\u5360\u7528\u4e86worker\u7684\u51e0\u4e2a\u8fde\u63a5\u6570&#xff1a;2~4\u4e2a<\/li>\n<li>\u666e\u901a\u9759\u6001\u8bbf\u95ee\u6700\u5927\u5e76\u53d1\u6570\u662f&#xff1a;worker_connections * worker_processes \/ 2<\/li>\n<li>HTTP\u505a\u53cd\u5411\u4ee3\u7406&#xff0c;\u6700\u5927\u5e76\u53d1\u6570 worker_connections * worder_processes \/ 4<\/li>\n<\/ul>\n<p>\u8fd9\u4e2a\u503c\u662f\u8868\u793a\u6bcf\u4e2a worker \u8fdb\u7a0b\u6240\u80fd\u5efa\u7acb\u8fde\u63a5\u7684\u6700\u5927\u503c&#xff0c;\u6240\u4ee5&#xff0c;\u4e00\u4e2a nginx \u80fd\u5efa\u7acb\u7684\u6700\u5927\u8fde\u63a5 \u6570&#xff0c;\u5e94\u8be5\u662f worker_connections * worker_processes\u3002\u5f53\u7136&#xff0c;\u8fd9\u91cc\u8bf4\u7684\u662f\u6700\u5927\u8fde\u63a5\u6570&#xff0c;\u5bf9\u4e8e HTTP \u8bf7 \u6c42 \u672c \u5730 \u8d44 \u6e90 \u6765 \u8bf4 &#xff0c; \u80fd \u591f \u652f \u6301 \u7684 \u6700 \u5927 \u5e76 \u53d1 \u6570 \u91cf \u662f worker_connections * worker_processes&#xff0c;\u5982\u679c\u662f\u652f\u6301 http1.1 \u7684\u6d4f\u89c8\u5668\u6bcf\u6b21\u8bbf\u95ee\u8981\u5360\u4e24\u4e2a\u8fde\u63a5&#xff0c;\u6240\u4ee5\u666e\u901a\u7684\u9759\u6001\u8bbf \u95ee\u6700\u5927\u5e76\u53d1\u6570\u662f&#xff1a; worker_connections * worker_processes \/2&#xff0c;\u800c\u5982\u679c\u662f HTTP \u4f5c \u4e3a\u53cd\u5411\u4ee3 \u7406\u6765\u8bf4&#xff0c;\u6700\u5927\u5e76\u53d1\u6570\u91cf\u5e94\u8be5\u662f worker_connections * worker_processes\/4\u3002\u56e0\u4e3a\u4f5c\u4e3a\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668&#xff0c;\u6bcf\u4e2a\u5e76\u53d1\u4f1a\u5efa\u7acb\u4e0e\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u548c\u4e0e\u540e\u7aef\u670d \u52a1\u7684\u8fde\u63a5&#xff0c;\u4f1a\u5360\u7528\u4e24\u4e2a\u8fde\u63a5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e31\u6b21\uff0c\u6536\u85cf10\u6b21\u3002keepalived+nginx \u53cc\u670d\u52a1\u5668\u4e3b\u5907\u914d\u7f6e_nginx +keepalived\u90e8\u7f72\u6587\u6863<\/p>\n","protected":false},"author":2,"featured_media":28880,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[117,2334,2333],"topic":[],"class_list":["post-28884","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-nginx","tag-2334","tag-2333"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>keepalived + nginx \u53cc\u670d\u52a1\u5668\u4e3b\u5907\u65b9\u6848 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/28884.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"keepalived + nginx \u53cc\u670d\u52a1\u5668\u4e3b\u5907\u65b9\u6848 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e31\u6b21\uff0c\u6536\u85cf10\u6b21\u3002keepalived+nginx \u53cc\u670d\u52a1\u5668\u4e3b\u5907\u914d\u7f6e_nginx +keepalived\u90e8\u7f72\u6587\u6863\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/28884.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-19T19:45:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250419194555-6803fd73e450c.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/28884.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/28884.html\",\"name\":\"keepalived + nginx \u53cc\u670d\u52a1\u5668\u4e3b\u5907\u65b9\u6848 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-19T19:45:57+00:00\",\"dateModified\":\"2025-04-19T19:45:57+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/28884.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/28884.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/28884.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"keepalived + nginx \u53cc\u670d\u52a1\u5668\u4e3b\u5907\u65b9\u6848\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"keepalived + nginx \u53cc\u670d\u52a1\u5668\u4e3b\u5907\u65b9\u6848 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/28884.html","og_locale":"zh_CN","og_type":"article","og_title":"keepalived + nginx \u53cc\u670d\u52a1\u5668\u4e3b\u5907\u65b9\u6848 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e31\u6b21\uff0c\u6536\u85cf10\u6b21\u3002keepalived+nginx \u53cc\u670d\u52a1\u5668\u4e3b\u5907\u914d\u7f6e_nginx +keepalived\u90e8\u7f72\u6587\u6863","og_url":"https:\/\/www.wsisp.com\/helps\/28884.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-19T19:45:57+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250419194555-6803fd73e450c.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/28884.html","url":"https:\/\/www.wsisp.com\/helps\/28884.html","name":"keepalived + nginx \u53cc\u670d\u52a1\u5668\u4e3b\u5907\u65b9\u6848 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-19T19:45:57+00:00","dateModified":"2025-04-19T19:45:57+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/28884.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/28884.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/28884.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"keepalived + nginx \u53cc\u670d\u52a1\u5668\u4e3b\u5907\u65b9\u6848"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/28884","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=28884"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/28884\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/28880"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=28884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=28884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=28884"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=28884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}