{"id":9457,"date":"2024-03-30T21:03:00","date_gmt":"2024-03-30T13:03:00","guid":{"rendered":"https:\/\/egonlin.com\/?p=9457"},"modified":"2024-03-30T21:03:00","modified_gmt":"2024-03-30T13:03:00","slug":"if%e5%88%a4%e6%96%ad%e5%ae%9e%e7%8e%b0nginx%e8%b5%84%e6%ba%90%e5%88%86%e7%a6%bb%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=9457","title":{"rendered":"if\u5224\u65ad\u5b9e\u73b0nginx\u8d44\u6e90\u5206\u79bb\u5b9e\u8df5"},"content":{"rendered":"<h2>nginx\u8d44\u6e90\u5206\u79bb\u5b9e\u8df5<\/h2>\n<h3>1.\u51c6\u5907\u73af\u5883<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u4e3b\u673a<\/th>\n<th>IP<\/th>\n<th>\u529f\u80fd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>lb01<\/td>\n<td>10.0.0.4,172.16.1.4<\/td>\n<td>\u8d1f\u8f7d\u5747\u8861<\/td>\n<\/tr>\n<tr>\n<td>web01<\/td>\n<td>172.16.1.7<\/td>\n<td>Android\u9875\u9762<\/td>\n<\/tr>\n<tr>\n<td>web02<\/td>\n<td>172.16.1.8<\/td>\n<td>iPhone\u9875\u9762<\/td>\n<\/tr>\n<tr>\n<td>web03<\/td>\n<td>172.16.1.9<\/td>\n<td>PC\u7aef\u9875\u9762<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>2.\u914d\u7f6eweb01\u670d\u52a1\u5668<\/h3>\n<h4>1\uff09\u914d\u7f6enginx<\/h4>\n<pre><code class=\"language-bash\">[root@web01 ~]# vim \/etc\/nginx\/conf.d\/linux.sj.com.conf\nserver {\n    listen 80;\n    server_name linux.sj.com;\n    charset utf8;\n\n    location \/ {\n        root \/code\/android;\n        index index.html;\n    }\n}\n\n[root@web01 ~]# systemctl restart nginx<\/code><\/pre>\n<h4>2\uff09\u521b\u5efa\u7ad9\u70b9\u76ee\u5f55<\/h4>\n<pre><code class=\"language-bash\">[root@web01 ~]# mkdir \/code\/android\n[root@web01 ~]# echo &quot;\u6211\u662fandroid&quot; &gt;&gt; \/code\/android\/index.html\n[root@web01 ~]# chown -R www.www \/code\/android\/<\/code><\/pre>\n<h4>3\uff09\u8bbf\u95ee\u6d4b\u8bd5<\/h4>\n<pre><code class=\"language-bash\">1.\u914d\u7f6ehosts\n10.0.0.7 linux.sj.com<\/code><\/pre>\n<h3>3.\u914d\u7f6eweb02\u670d\u52a1\u5668<\/h3>\n<h4>1\uff09\u914d\u7f6enginx<\/h4>\n<pre><code class=\"language-bash\">[root@web02 ~]# vim \/etc\/nginx\/conf.d\/linux.sj.com.conf\nserver {\n    listen 80;\n    server_name linux.sj.com;\n    charset utf8;\n\n    location \/ {\n        root \/code\/iphone;\n        index index.html;\n    }\n}<\/code><\/pre>\n<h4>2\uff09\u521b\u5efa\u7ad9\u70b9\u6587\u4ef6<\/h4>\n<pre><code class=\"language-bash\">[root@web02 ~]# mkdir \/code\/iphone\n[root@web02 ~]# echo &quot;\u6211\u662fIphone&quot; &gt;&gt; \/code\/iphone\/index.html\n[root@web02 ~]# chown -R www.www \/code\/iphone\/<\/code><\/pre>\n<h4>3\uff09\u8bbf\u95ee\u6d4b\u8bd5<\/h4>\n<pre><code class=\"language-bash\">1.\u914d\u7f6ehosts\n10.0.0.8 linux.sj.com<\/code><\/pre>\n<h3>4.\u914d\u7f6eweb03\u670d\u52a1\u5668<\/h3>\n<h4>1\uff09\u914d\u7f6enginx<\/h4>\n<pre><code class=\"language-bash\">[root@web03 ~]# vim \/etc\/nginx\/conf.d\/linux.sj.com.conf\nserver {\n    listen 80;\n    server_name linux.sj.com;\n    charset utf8;\n\n    location \/ {\n        root \/code\/pc;\n        index index.html;\n    }\n}\n[root@web02 ~]# systemctl restart nginx<\/code><\/pre>\n<h4>2\uff09\u521b\u5efa\u7ad9\u70b9\u6587\u4ef6<\/h4>\n<pre><code class=\"language-bash\">[root@web03 ~]# mkdir \/code\/pc -p\n[root@web03 ~]# echo &quot;\u6211\u662fpc\u7aef&quot; &gt;&gt; \/code\/pc\/index.html\n[root@web03 ~]# chown -R www.www \/code\/<\/code><\/pre>\n<h4>3\uff09\u8bbf\u95ee\u6d4b\u8bd5<\/h4>\n<pre><code class=\"language-bash\">1.\u914d\u7f6ehosts\n10.0.0.9 linux.sj.com<\/code><\/pre>\n<h3>5.\u914d\u7f6e\u8d1f\u8f7d\u5747\u8861<\/h3>\n<h4>1.\u914d\u7f6enginx<\/h4>\n<pre><code class=\"language-bash\">[root@lb01 ~]# vim \/etc\/nginx\/conf.d\/linux.sj.com.conf\nupstream android {\n    server 10.0.0.7;\n}\n\nupstream iphone {\n    server 10.0.0.8;\n}\n\nupstream pc {\n    server 10.0.0.9;\n}\n\nserver {\n    listen 80;\n    server_name linux.sj.com;\n\n    location \/ {\n        if ($http_user_agent ~* &quot;Android&quot;) { #\u5224\u65ad\u5982\u679c\u662f\u5b89\u5353\u7aef\n            proxy_pass http:\/\/android;       #\u4ee3\u7406\u5230android\u865a\u62df\u4e3b\u673a\u6c60\n        }\n        if ($http_user_agent ~* &quot;iPhone&quot;) {    #\u5224\u65ad\u5982\u679c\u662f\u82f9\u679c\u7aef\n            proxy_pass http:\/\/iphone;       #\u4ee3\u7406\u5230iphone\u865a\u62df\u4e3b\u673a\u6c60\n        }\n        if ($http_user_agent ~* &quot;WOW64&quot;) {    #\u5224\u65ad\u5982\u679c\u662fIE\u6d4f\u89c8\u5668\n            return 403;                    #\u76f4\u63a5\u8fd4\u56de403\n        }\n        proxy_pass http:\/\/pc;               #\u5982\u679c\u6ca1\u6709\u5339\u914d\u5230\u4ee5\u4e0a\u5185\u5bb9\uff0c\u9ed8\u8ba4\u90fd\u4ee3\u7406\u5230pc\u865a\u62df\u4e3b\u673a\u6c60\n        include proxy_params;\n    }\n}<\/code><\/pre>\n<h4>2.\u91cd\u542f\u8bbf\u95ee<\/h4>\n<pre><code class=\"language-bash\">1.\u91cd\u542f\n[root@lb01 ~]# nginx -t\nnginx: the configuration file \/etc\/nginx\/nginx.conf syntax is ok\nnginx: configuration file \/etc\/nginx\/nginx.conf test is successful\n[root@lb01 ~]# systemctl restart nginx\n\n2.\u914d\u7f6ehosts\n10.0.0.4 linux.sj.com\n\n3.\u8bbf\u95ee<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>nginx\u8d44\u6e90\u5206\u79bb\u5b9e\u8df5 1.\u51c6\u5907\u73af\u5883 \u4e3b\u673a IP \u529f\u80fd lb01 10.0.0.4,172.16.1.4 \u8d1f\u8f7d [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/9457"}],"collection":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9457"}],"version-history":[{"count":1,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/9457\/revisions"}],"predecessor-version":[{"id":9458,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/9457\/revisions\/9458"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}