{"id":343,"date":"2021-07-28T15:30:48","date_gmt":"2021-07-28T07:30:48","guid":{"rendered":"http:\/\/egonlin.com\/?p=343"},"modified":"2024-03-30T21:29:52","modified_gmt":"2024-03-30T13:29:52","slug":"%e7%ac%ac%e4%ba%8c%e8%8a%82%ef%bc%9anginx%e5%b8%b8%e8%a7%81%e9%97%ae%e9%a2%98%e4%ba%8c","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=343","title":{"rendered":"\u7b2c\u4e8c\u8282\uff1anginx\u5e38\u89c1\u95ee\u9898\u4e8c"},"content":{"rendered":"<h1>Nginx\u5e38\u89c1\u95ee\u9898<\/h1>\n<h2>\u4e00\u3001server\u4f18\u5148\u7ea7<\/h2>\n<pre><code class=\"language-bash\">1.\u9996\u5148\u9009\u62e9\u6240\u6709\u7684\u5b57\u7b26\u4e32\u5b8c\u5168\u5339\u914d\u7684server_name\u3002\uff08\u5b8c\u5168\u5339\u914d\uff09\n2.\u9009\u62e9\u901a\u914d\u7b26\u5728\u524d\u9762\u7684server_name\uff0c\u5982.egonlin.com blog.egonlin.com\n3.\u9009\u62e9\u901a\u914d\u7b26\u5728\u540e\u9762\u7684server_name\uff0c\u5982egonlin. egonlin.com egonlin.cn\n4.\u6700\u540e\u9009\u62e9\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7684server_name\n5.\u5982\u679c\u5168\u90e8\u90fd\u6ca1\u6709\u5339\u914d\u5230\uff0c\u90a3\u4e48\u5c06\u9009\u62e9\u5728listen\u914d\u7f6e\u9879\u540e\u52a0\u5165[default_server]\u7684server\u5757\n6.\u5982\u679c\u6ca1\u5199\uff0c\u90a3\u4e48\u5c31\u627e\u5230\u5339\u914dlisten\u7aef\u53e3\u7684\u7b2c\u4e00\u4e2aServer\u5757\u7684\u914d\u7f6e\u6587\u4ef6<\/code><\/pre>\n<h2>\u4e8c\u3001\u7981\u6b62IP\u8bbf\u95ee<\/h2>\n<pre><code class=\"language-bash\">\u5f53\u7528\u6237\u901a\u8fc7\u8bbf\u95eeIP\u6216\u8005\u672a\u77e5\u57df\u540d\u8bbf\u95ee\u4f60\u5f97\u7f51\u7ad9\u7684\u65f6\u5019\uff0c\u4f60\u5e0c\u671b\u7981\u6b62\u663e\u793a\u4efb\u4f55\u6709\u6548\u5185\u5bb9\uff0c\u53ef\u4ee5\u7ed9\u4ed6\u8fd4\u56de500\uff0c\u76ee\u524d\u56fd\u5185\u5f88\u591a\u673a\u623f\u90fd\u8981\u6c42\u7f51\u7ad9\u5173\u95ed\u7a7a\u4e3b\u673a\u5934\uff0c\u9632\u6b62\u672a\u5907\u6848\u7684\u57df\u540d\u6307\u5411\u8fc7\u6765\u9020\u6210\u9ebb\u70e6<\/code><\/pre>\n<pre><code class=\"language-bash\">[root@lb01 conf.d]# cat server4.conf \nserver {\n    listen 80 default_server;           #\u9ed8\u8ba4\u4f18\u5148\u8fd4\u56de\uff1b\n    server_name _;                      #\u7a7a\u4e3b\u673a\u5934\u6216\u8005IP\uff1b\n    return 500;                         #\u76f4\u63a5\u8fd4\u56de500\u9519\u8bef\uff1b\n}<\/code><\/pre>\n<h2>\u4e09\u3001Nginx try_file\u8def\u5f84\u5339\u914d<\/h2>\n<pre><code class=\"language-bash\">nginx\u7684try_file\u8def\u5f84\u5339\u914d\uff0cNginx\u4f1a\u6309\u987a\u5e8f\u68c0\u67e5\u6587\u4ef6\u53ca\u76ee\u5f55\u662f\u5426\u5b58\u5728\uff08\u6839\u636e root \u548c alias \u6307\u4ee4\u8bbe\u7f6e\u7684\u53c2\u6570\u6784\u9020\u5b8c\u6574\u7684\u6587\u4ef6\u8def\u5f84\uff09\uff0c\u5e76\u7528\u627e\u5230\u7684\u7b2c\u4e00\u4e2a\u6587\u4ef6\u63d0\u4f9b\u670d\u52a1\u3002\u5728\u5143\u7d20\u540d\u540e\u9762\u6dfb\u52a0\u659c\u6760 \/ \u8868\u793a\u8fd9\u4e2a\u662f\u76ee\u5f55\u3002\u5982\u679c\u6587\u4ef6\u548c\u76ee\u5f55\u90fd\u4e0d\u5b58\u5728\uff0cNginx\u4f1a\u6267\u884c\u5185\u90e8\u91cd\u5b9a\u5411\uff0c\u8df3\u8f6c\u5230\u547d\u4ee4\u7684\u6700\u540e\u4e00\u4e2a uri \u53c2\u6570\u5b9a\u4e49\u7684 URI \u4e2d\u3002<\/code><\/pre>\n<h3>1.\u6b63\u5e38\u7684\u914d\u7f6e\u6587\u4ef6<\/h3>\n<pre><code class=\"language-bash\">[root@web01 conf.d]# vim linux.try.com.conf \nserver {\n    listen 80;\n    server_name linux.try.com;\n\n    location \/ {\n        root \/code;\n        index index.html;\n    }\n}\n\n[root@web01 conf.d]# systemctl restart nginx\n[root@web01 conf.d]# echo &quot;test try_file&quot; &gt; \/code\/index.html<\/code><\/pre>\n<h3>2.\u4f7f\u7528try_file\u7684\u914d\u7f6e<\/h3>\n<pre><code class=\"language-bash\">[root@web01 conf.d]# vim linux.try.com.conf \nserver {\n    listen 80;\n    server_name linux.try.com;\n\n    location \/ {\n        root \/code;\n        try_files $uri \/1.jpg;\n    }\n}\n\n#\u8bbf\u95ee\u6d4b\u8bd5\uff1a\n1.\u8bbf\u95ee\u57df\u540d\u65f6 linux.try.com\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u662f 1.jpg\n\u7531\u4e8e\u8bf7\u6c42\u7684\u662f\u57df\u540d\uff0c\u540e\u9762\u6ca1\u6709 uri\uff0c\u90a3\u4e48 $uri \u5339\u914d\u5230\u7684\u5c31\u662f &quot;\u7a7a&quot;\uff0c\u5339\u914d\u4e0d\u5230\u5185\u5bb9\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd4\u56de 1.jpg\n\n2.\u8bbf\u95ee\u57df\u540d linux.try.com\/index.html\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u662f index.html\n\u7531\u4e8e\u8bf7\u6c42\u7684\u662flinux.try.com\/index.html\uff0c$uri \u5339\u914d\u5230\u7684\u662f index.html\uff0c\u5c31\u8fd4\u56de\u76f8\u5e94\u5185\u5bb9<\/code><\/pre>\n<h3>3.\u4fee\u6539try_file\u914d\u7f6e<\/h3>\n<pre><code class=\"language-bash\">[root@web01 conf.d]# vim linux.try.com.conf \nserver {\n    listen 80;\n    server_name linux.try.com;\n\n    location \/ {\n        root \/code;\n        try_files $uri $uri\/ \/1.jpg;\n    }\n}\n\n#\u8bbf\u95ee\u6d4b\u8bd5\uff1a\n1.\u8bbf\u95ee\u57df\u540d linux.try.com\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u662f index.html\n\u7531\u4e8e\u8bf7\u6c42\u7684\u662f\u57df\u540d\uff0c\u540e\u9762\u6ca1\u6709 uri\uff0c\u90a3\u4e48 $uri \u5339\u914d\u5230\u7684\u5c31\u662f &quot;\u7a7a&quot;\uff0c$uri \u5339\u914d\u4e0d\u5230\u5185\u5bb9\u7684\u60c5\u51b5\u4e0b\uff0c\u5339\u914d $uri\/\uff0c\u5339\u914d\u5230\u7684\u662f &quot;\u7a7a\/&quot;\uff0c\/ \u914d\u7f6e\u7684\u662f \/code\uff0c\u90a3\u4e48\u5c31\u56de\u53bb\u8bf7\u6c42code\u76ee\u5f55\u4e0b\u7684 index.html<\/code><\/pre>\n<h3>4.\u4e00\u822c\u4f7f\u7528\u573a\u666f<\/h3>\n<h4>1\uff09\u914d\u7f6enginx<\/h4>\n<pre><code class=\"language-bash\">[root@lb01 conf.d]# vim linux.try.com.conf\nserver {\n    listen 80;\n    server_name linux.try.com;\n    root \/code;\n\n    location \/ {\n        try_files $uri $uri\/ @java;           #\u5f53$uri\u548c$uri\/\u90fd\u5339\u914d\u4e0d\u5230\u65f6\uff0c\u7531\u540e\u7aef\u7684java\u6765\u8fdb\u884c\u5904\u7406\uff0c\u540d\u5b57\u53ef\u81ea\u5b9a\u4e49\uff0c\u4f46\u4e00\u5b9a\u8981\u52a0@\n    }\n\n    location @java {\n        proxy_pass http:\/\/172.16.1.8:8080;          #\u914d\u7f6e\u540e\u7aeftomcat\n    }\n}<\/code><\/pre>\n<h4>2\uff09\u5b89\u88c5tomcat<\/h4>\n<pre><code class=\"language-bash\">[root@web01 ~]# yum install -y tomcat\n[root@web01 ~]# cd \/usr\/share\/tomcat\/webapps\/\n[root@web01 webapps]# mkdir ROOT\n[root@web01 webapps]# echo &quot;test try_file @java&quot; &gt; ROOT\/index.html<\/code><\/pre>\n<h4>3\uff09\u6d4b\u8bd5<\/h4>\n<pre><code class=\"language-bash\">1.code\u76ee\u5f55\u4e0b\u6e38index.html\u7684\u60c5\u51b5\uff0c\u8bbf\u95ee\u57df\u540d\u6b63\u5e38\u663e\u793a index.html\n2.\u628acode\u76ee\u5f55\u6539\u540d\uff0c\u8bbf\u95ee\u57df\u540d\uff0c\u8fd4\u56de\u7684\u65f6tomcat\u4e0b\u9762\u914d\u7f6e\u7684\u9875\u9762<\/code><\/pre>\n<h2>\u56db\u3001Nginx\u4f18\u96c5\u663e\u793a\u9519\u8bef\u9875\u9762<\/h2>\n<div id=\"rml_readmorelogin_placeholder\" style=\"position:relative;\"><div id=\"rml_fade_content\" style=\"position: absolute;\r\ntop:-10em;\r\nwidth:100%;\r\nheight:10em;\r\nbackground: -webkit-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -moz-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -o-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -ms-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\"><\/div><div class=\"wpf-controller aru_rml_from_in_post\" style=\"background-color:#eeeeee;border:5px solid #cce6ff;\" id=\"ARU_ReadMoreLogin_ReadMoreLoginController\"><h2 id=\"Header\">\u67e5\u770b\u66f4\u591a<\/h2><div id=\"Message\"><p>\u8054\u7cfb\u7ba1\u7406\u5458\u5fae\u4fe1tutu19192010\uff0c\u6ce8\u518c\u8d26\u53f7<\/p>\n<\/div><div id=\"StatusBarHeader\"><\/div><form id=\"ARU_ReadMoreLogin_ReadMoreLoginController\"><input name=\"post_id\" value=\"343\" type=\"hidden\"\/><input name=\"_init_callback\" value=\"InitLogin\" type=\"hidden\"\/><input name=\"post_id\" value=\"343\" type=\"hidden\"\/><input name=\"rt_ype\" value=\"1\" type=\"hidden\"\/><input name=\"nonce\" value=\"e57512139e\" type=\"hidden\"\/><input name=\"_wpnonce\" value=\"fe0b6c877b\" type=\"hidden\"\/><input name=\"_controller\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginController\" type=\"hidden\"\/><input name=\"_proxy_controller\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginController\" type=\"hidden\"\/><input name=\"_view\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginView\" type=\"hidden\"\/><table class=\"wpf-table-placeholder\"><tbody class=\"wpf-table-placeholder\"><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\" width=\"400px\"><table class=\"wpf-table-placeholder\"><tbody class=\"wpf-table-placeholder\"><tr class=\"wpf-table-placeholder\"><th class=\"wpf-table-placeholder-input\"><label class=\"wpf-label\">Username:<\/label><\/th><\/tr><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\"><input class=\"regular-text text_input\" name=\"username\" value=\"\" type=\"text\"\/><\/td><\/tr><tr class=\"wpf-table-placeholder\"><th class=\"wpf-table-placeholder-input\"><label class=\"wpf-label\">Password:<\/label><\/th><\/tr><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\"><input class=\"regular-text text_input\" name=\"password\" value=\"\" type=\"password\"\/><\/td><\/tr><\/tbody><\/table><p class=\"wpf-table-placeholder submit\"><button class=\"wp_plugin_framework_ajax_button\" type=\"button\" style=\"background-color:#4D90FE;;color:#ffffff;;border:1px solid #3079ed;\" name=\"_event\" value=\"ButtonLogin\">Log in<\/button><\/p><\/td><td class=\"wpf-table-placeholder-input\"><\/td><\/tr><\/tbody><\/table><\/form><div id=\"ButtonStartRegister\"><a href=\"https:\/\/egonlin.com\/wp-login.php?action=register\">Register<\/a><\/div><div id=\"Link1\"><a href=\"https:\/\/egonlin.com\/wp-login.php?action=lostpassword\">Forgotten username or password?<\/a><\/div><div id=\"StatusBarFooter\"><\/div><\/div><\/div><div id=aru_remaining_content><\/div>","protected":false},"excerpt":{"rendered":"<p>Nginx\u5e38\u89c1\u95ee\u9898 \u4e00\u3001server\u4f18\u5148\u7ea7 1.\u9996\u5148\u9009\u62e9\u6240\u6709\u7684\u5b57\u7b26\u4e32\u5b8c\u5168\u5339\u914d\u7684server_name\u3002\uff08\u5b8c\u5168\u5339 [&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":[21,19,58,22,57,20],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/343"}],"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=343"}],"version-history":[{"count":1,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/343\/revisions"}],"predecessor-version":[{"id":9463,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/343\/revisions\/9463"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}