{"id":4608,"date":"2022-03-27T14:11:29","date_gmt":"2022-03-27T06:11:29","guid":{"rendered":"https:\/\/egonlin.com\/?p=4608"},"modified":"2022-03-27T14:11:29","modified_gmt":"2022-03-27T06:11:29","slug":"%e7%ac%ac%e4%b8%80%e8%8a%82%ef%bc%9a%e5%b0%8f%e7%99%bd%e5%ad%a6nginx%e4%b9%8bnginx%e7%9a%84%e5%9f%ba%e7%a1%80","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=4608","title":{"rendered":"\u7b2c\u4e00\u8282\uff1a\u5c0f\u767d\u5b66nginx\u4e4bnginx\u7684\u57fa\u7840"},"content":{"rendered":"<h4>\u5c0f\u767d\u5b66nginx\u4e4bnginx\u7684\u57fa\u7840<\/h4>\n<h5>Nginx\u5b89\u88c5\u548c\u542f\u52a8<\/h5>\n<p>1.yum\u65b9\u5f0f\uff08\u5b98\u65b9\uff09<\/p>\n<pre><code class=\"language-bash\">[root@web01 ~]# vim \/etc\/yum.repos.d\/nginx.repo\n[nginx]\nname=nginx repo\nbaseurl=http:\/\/nginx.org\/packages\/centos\/7\/$basearch\/\ngpgcheck=0\nenabled=1\n\n[root@web01 ~]# yum install nginx -y\n\n#\u67e5\u770b\u7248\u672c\n[root@web01 ~]# nginx -v\nnginx version: nginx\/1.14.2<\/code><\/pre>\n<p>2.\u6e90\u7801\u7f16\u8bd1\u5b89\u88c5<\/p>\n<pre><code class=\"language-bash\">[root@web01 ~]# wget http:\/\/nginx.org\/download\/nginx-1.14.2.tar.gz\n[root@web01 ~]# tar xf nginx-1.14.2.tar.gz\n[root@web01 ~]# cd nginx-1.14.2\/\n[root@web01 nginx-1.14.2]# .\/configure --prefix=\/etc\/nginx --sbin-path=\/usr\/sbin\/nginx --modules-path=\/usr\/lib64\/nginx\/modules --conf-path=\/etc\/nginx\/nginx.conf --error-log-path=\/var\/log\/nginx\/error.log --http-log-path=\/var\/log\/nginx\/access.log --pid-path=\/var\/run\/nginx.pid --lock-path=\/var\/run\/nginx.lock --http-client-body-temp-path=\/var\/cache\/nginx\/client_temp --http-proxy-temp-path=\/var\/cache\/nginx\/proxy_temp --http-fastcgi-temp-path=\/var\/cache\/nginx\/fastcgi_temp --http-uwsgi-temp-path=\/var\/cache\/nginx\/uwsgi_temp --http-scgi-temp-path=\/var\/cache\/nginx\/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt=&#039;-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC&#039; --with-ld-opt=&#039;-Wl,-z,relro -Wl,-z,now -pie&#039;\n[root@web01 nginx-1.14.2]# make &amp;&amp; make install<\/code><\/pre>\n<p>2.\u542f\u52a8Nginx<\/p>\n<pre><code class=\"language-bash\">\u5148\u5173\u95ed\u4e4b\u524d\u5df2\u542f\u52a8\u7684httpd\u670d\u52a1\n[root@web01 ~]# systemctl stop httpd\n[root@web01 ~]# systemctl disable httpd\n\n\u7136\u540e\u542f\u52a8Nginx\u670d\u52a1\n[root@web01 ~]# systemctl enable nginx\n[root@web01 ~]# systemctl start nginx<\/code><\/pre>\n<p>3.\u542f\u52a8\u65b9\u5f0f\u6709\u4e24\u79cd\uff1a<\/p>\n<pre><code class=\"language-bash\">nginx  \u542f\u52a8\nnginx -s stop \u505c\u6b62\nnginx -s reload |restart \u91cd\u8f7d\u670d\u52a1\n\nsystemctl start nginx\nsystemctl restart nginx\nsystemctl stop nginx <\/code><\/pre>\n<h5>nginx \u914d\u7f6e\u6587\u4ef6<\/h5>\n<pre><code class=\"language-bash\">[root@web01 ~]# cat \/etc\/nginx\/nginx.conf\n---------------\u6838\u5fc3\u6a21\u5757\nuser  nginx;                                    #nginx\u8fdb\u7a0b\u8fd0\u884c\u7684\u7528\u6237\nworker_processes  1;                            #nginx\u5de5\u4f5c\u7684\u8fdb\u7a0b\u6570\u91cf\nerror_log  \/var\/log\/nginx\/error.log warn;       #nginx\u7684\u9519\u8bef\u65e5\u5fd7\u3010\u8b66\u544a\u53ca\u5176\u8b66\u544a\u4ee5\u4e0a\u7684\u90fd\u8bb0\u5f55\u3011\npid        \/var\/run\/nginx.pid;              #nginx\u8fdb\u7a0b\u8fd0\u884c\u540e\u7684\u8fdb\u7a0bid\n--------------\n\n---------------\u4e8b\u4ef6\u6a21\u5757\nevents {\n    worker_connections  1024;               #\u4e00\u4e2awork\u8fdb\u7a0b\u7684\u6700\u5927\u8fde\u63a5\u6570\n    use epool;                              #\u4f7f\u7528epool\u7f51\u7edc\u6a21\u578b\n}\n--------------\n\n---------------http\u6838\u5fc3\u5c42\u6a21\u5757\nhttp {\n    include       \/etc\/nginx\/mime.types;            #\u5305\u542b\u8d44\u6e90\u7c7b\u578b\u6587\u4ef6\n    default_type  application\/octet-stream;         #\u9ed8\u8ba4\u4ee5\u4e0b\u8f7d\u65b9\u5f0f\u4f20\u8f93\u7ed9\u6d4f\u89c8\u5668\uff08\u524d\u63d0\u662f\u8be5\u8d44\u6e90\u5728mime.types\u4e2d\u65e0\u6cd5\u627e\u5230\uff09\n\n    \u65e5\u5fd7\u683c\u5f0f\u5b9a\u4e49\n    log_format  main  &#039;$remote_addr - $remote_user [$time_local] &quot;$request&quot; &#039;\n                      &#039;$status $body_bytes_sent &quot;$http_referer&quot; &#039;\n                      &#039;&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot;&#039;;\n\n    access_log  \/var\/log\/nginx\/access.log  main;        #\u8bbf\u95ee\u65e5\u5fd7\n\n    sendfile        on;     \n    #tcp_nopush     on;\n    keepalive_timeout  65;      #\u957f\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\n    #gzip  on;                  #\u662f\u5426\u5f00\u542f\u538b\u7f29\u529f\u80fd\n\n    include \/etc\/nginx\/conf.d\/*.conf;   #\u5305\u542b\u54ea\u4e2a\u76ee\u5f55\u4e0b\u9762\u7684*.conf\u6587\u4ef6\n\n    server {  \u5b9a\u4e49\u4e00\u4e2a\u7f51\u7ad9\n        listen       80;            #\u76d1\u542c\u7aef\u53e3\n        server_name  localhost;     #\u57df\u540d\n\n        #charset koi8-r;            #\u5b57\u7b26\u96c6\n\n        location \/ {                #\u4f4d\u7f6e\n            root   \/usr\/share\/nginx\/html;   #\u4ee3\u7801\u7684\u4e3b\u6587\u4ef6\u4f4d\u7f6e\n            index  index.html index.htm;    #\u670d\u52a1\u7aef\u9ed8\u8ba4\u8fd4\u56de\u7ed9\u7528\u6237\u7684\u6587\u4ef6\n        }\n        location \/test {                #\u4f4d\u7f6e\n            root   \/code\/test\/123\/; #\u4ee3\u7801\u7684\u4e3b\u6587\u4ef6\u4f4d\u7f6e\n            index  index.html index.htm;    #\u670d\u52a1\u7aef\u9ed8\u8ba4\u8fd4\u56de\u7ed9\u7528\u6237\u7684\u6587\u4ef6\n        }\n    }<\/code><\/pre>\n<p><strong>\u603b\u7ed3\uff1a<\/strong><\/p>\n<blockquote>\n<p>http server location\u6269\u5c55\u4e86\u89e3\u9879<br \/>\nhttp{}\u5c42\u4e0b\u5141\u8bb8\u6709\u591a\u4e2aServer{}\u5c42\uff0c\u4e00\u4e2aServer{}\u5c42\u4e0b\u53c8\u5141\u8bb8\u6709\u591a\u4e2aLocation<br \/>\nhttp{} \u6807\u7b7e\u4e3b\u8981\u7528\u6765\u89e3\u51b3\u7528\u6237\u7684\u8bf7\u6c42\u4e0e\u54cd\u5e94\u3002<br \/>\nserver{} \u6807\u7b7e\u4e3b\u8981\u7528\u6765\u54cd\u5e94\u5177\u4f53\u7684\u67d0\u4e00\u4e2a\u7f51\u7ad9\u3002<br \/>\nlocation{} \u6807\u7b7e\u4e3b\u8981\u7528\u4e8e\u5339\u914d\u7f51\u7ad9\u5177\u4f53URL\u8def\u5f84\u3002<\/p>\n<\/blockquote>\n<h5>Nginx\u642d\u5efa\u4e00\u4e2a\u9759\u6001\u8d44\u6e90web\u670d\u52a1\u5668<\/h5>\n<p>1.\u7f16\u5199Nginx\u914d\u7f6e\u6587\u4ef6<\/p>\n<pre><code class=\"language-bash\">[root@web01 conf.d]# cat game.conf\nserver {\n    listen 80;\n    server_name game.oldboy.com;\n\n    location \/ {\n        root \/code;\n        index index.html;\n    }\n}<\/code><\/pre>\n<p>2.\u6839\u636e\u914d\u7f6e\u6587\u4ef6\uff0c\u521b\u5efa\u76ee\u5f55\uff0c\u4e0a\u4f20\u4ee3\u7801<\/p>\n<pre><code class=\"language-bash\">[root@web01 ~]# mkdir \/code\n[root@web01 ~]# cd \/code\n[root@web01 ~]# rz html5.zip\n[root@web01 code]# unzip html5.zip <\/code><\/pre>\n<p>3.\u91cd\u8f7dnginx\u670d\u52a1<\/p>\n<pre><code class=\"language-bash\">[root@web01 code]# systemctl restart nginx      #\u7acb\u5373\u91cd\u542f\n[root@web01 code]# systemctl reload nginx       #\u5e73\u6ed1\u91cd\u542f<\/code><\/pre>\n<p>4.\u914d\u7f6e\u57df\u540d\u89e3\u6790<\/p>\n<pre><code class=\"language-bash\">Windows:  C:\\Windows\\System32\\drivers\\etc\n                10.0.0.7      game.oldboy.com\n\nMac   sudo vim \/etc\/hosts\n                10.0.0.7      game.oldboy.com\n\nC:\\Users\\Administrator&gt;ping game.oldboy.com          #\u68c0\u67e5\u89e3\u6790\u7684\u662f\u5426\u662f10.0.0.7\n\u6b63\u5728 Ping game.oldboy.com [10.0.0.7] \u5177\u6709 32 \u5b57\u8282\u7684\u6570\u636e:\n\u6765\u81ea 10.0.0.7 \u7684\u56de\u590d: \u5b57\u8282=32 \u65f6\u95f4=9ms TTL=64\n\u6765\u81ea 10.0.0.7 \u7684\u56de\u590d: \u5b57\u8282=32 \u65f6\u95f4&lt;1ms TTL=64\n\u6765\u81ea 10.0.0.7 \u7684\u56de\u590d: \u5b57\u8282=32 \u65f6\u95f4&lt;1ms TTL=64\n\u6765\u81ea 10.0.0.7 \u7684\u56de\u590d: \u5b57\u8282=32 \u65f6\u95f4&lt;1ms TTL=64<\/code><\/pre>\n<p>5.\u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95ee\u5bf9\u5e94\u7684\u9879\u76ee<\/p>\n<pre><code class=\"language-bash\">http:\/\/game.oldboy.com\/game\/duxinshu\/index.html    \u7528\u6237\u8bf7\u6c42\u7684\u8def\u5f84\n\n\u5b9e\u9645\u4e0a\u670d\u52a1\u5668\u67e5\u627e\u7684\u8def\u5f84     \/code\/game\/duxinshu\/index.html<\/code><\/pre>\n<h5>nginx\u865a\u62df\u4e3b\u673a<\/h5>\n<p>Nginx\u914d\u7f6e\u865a\u62df\u4e3b\u673a\u6709\u5982\u4e0b\u4e09\u79cd\u65b9\u5f0f\uff1a<\/p>\n<p>\u65b9\u5f0f\u4e00\u3001\u57fa\u4e8e\u4e3b\u673a\u591aIP\u65b9\u5f0f<\/p>\n<p>1.\u914d\u7f6e\u591a\u7f51\u5361\u591aIP\u7684\u65b9\u5f0f<\/p>\n<pre><code class=\"language-bash\">[root@web01 conf.d]# cat ip.conf \nserver {\n    listen 10.0.0.7:80;\n    server_name _;\n\n    location \/ {\n        root \/code_ip_eth0;\n        index index.html;\n    }\n}\n\nserver {\n    listen 172.16.1.7:80;\n    server_name _;\n\n    location \/ {\n        root \/code_ip_eth1;\n        index index.html;\n    }\n}<\/code><\/pre>\n<p>2.\u6839\u636e\u914d\u7f6e\u521b\u5efa\u76ee\u5f55<\/p>\n<pre><code class=\"language-bash\">[root@web01 conf.d]# mkdir \/code_ip_eth0\n[root@web01 conf.d]# echo &quot;Eth0&quot; &gt; \/code_ip_eth0\/index.html\n\n[root@web01 conf.d]# mkdir \/code_ip_eth1\n[root@web01 conf.d]# echo &quot;Eth1&quot; &gt; \/code_ip_eth1\/index.html<\/code><\/pre>\n<p>3.\u91cd\u542fnginx\u670d\u52a1<\/p>\n<pre><code class=\"language-bash\">[root@web01 conf.d]# systemctl restart nginx<\/code><\/pre>\n<p>4.\u4f7f\u7528curl\u547d\u4ee4\u6d4b\u8bd5<\/p>\n<pre><code class=\"language-bash\">[root@web01 ~]# curl 172.16.1.7\nEth1\n[root@web01 ~]# curl 10.0.0.7\nEth0<\/code><\/pre>\n<p>\u65b9\u5f0f\u4e8c\u3001\u57fa\u4e8e\u7aef\u53e3\u7684\u914d\u7f6e\u65b9\u5f0f<\/p>\n<p>1.\u914d\u7f6e\u591a\u7aef\u53e3\u7684\u865a\u62df\u4e3b\u673a<\/p>\n<pre><code class=\"language-bash\">[root@web01 conf.d]# vim port.conf\nserver {\n        listen 81;\n        location \/ { \n                root \/code_81;\n                index index.html;\n        }\n}\nserver {\n        listen 82;\n        location \/ { \n                root \/code_82;\n                index index.html;\n        }\n}       <\/code><\/pre>\n<p>2.\u6839\u636e\u914d\u7f6e\u6587\u4ef6\u521b\u5efa\u6240\u9700\u7684\u76ee\u5f55<\/p>\n<pre><code class=\"language-bash\">[root@web01 conf.d]# mkdir \/code_8{1..2}\n[root@web01 conf.d]# echo &quot;81&quot; &gt; \/code_81\/index.html\n[root@web01 conf.d]# echo &quot;82&quot; &gt; \/code_82\/index.html<\/code><\/pre>\n<p>3.\u68c0\u67e5\u8bed\u6cd5\u5e76\u91cd\u542f\u670d\u52a1<\/p>\n<pre><code class=\"language-bash\">[root@web01 conf.d]# 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@web01 conf.d]# systemctl restart nginx<\/code><\/pre>\n<p>4.\u5982\u4f55\u53bb\u8bbf\u95ee<\/p>\n<blockquote>\n<p><a href=\"http:\/\/10.0.0.7:82\/\">http:\/\/10.0.0.7:82\/<\/a><\/p>\n<\/blockquote>\n<p>\u65b9\u5f0f\u4e09\u3001\u57fa\u4e8e\u591a\u4e2ahosts\u540d\u79f0\u65b9\u5f0f(\u591a\u57df\u540d\u65b9\u5f0f)<br \/>\n1.\u51c6\u5907\u591a\u865a\u62df\u4e3b\u673a\u914d\u7f6e\u6587\u4ef6<\/p>\n<pre><code class=\"language-bash\">[root@web01 conf.d]# cat test1.xxx.com.conf \nserver {\n    listen 80;\n    server_name test1.xxx.com;\n\n    location \/ {\n        root \/code\/test1;\n        index index.html;\n    }\n}\n\n[root@web01 conf.d]# cat test2.xxx.com.conf \nserver {\n    listen 80;\n    server_name test2.xxx.com;\n\n    location \/ {\n        root \/code\/test2;\n        index index.html;\n    }\n}<\/code><\/pre>\n<p>2.\u6839\u636e\u914d\u7f6e\u6587\u4ef6\u521b\u5efa\u5bf9\u5e94\u7684\u76ee\u5f55<\/p>\n<pre><code class=\"language-bash\">[root@web01 conf.d]# mkdir \/code\/test{1..2} -p\n[root@web01 conf.d]# echo &quot;test1_server&quot; &gt; \/code\/test1\/index.html\n[root@web01 conf.d]# echo &quot;test2_server&quot; &gt; \/code\/test2\/index.html\n[root@web01 conf.d]# nginx -t\n[root@web01 conf.d]# systemctl restart nginx<\/code><\/pre>\n<p>3.\u914d\u7f6e\u57df\u540d\u89e3\u6790<\/p>\n<pre><code class=\"language-bash\">10.0.0.7      test1.xxx.com\n10.0.0.7      test2.xxx.com<\/code><\/pre>\n<p>4.\u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95ee\u8be5\u7f51\u7ad9<\/p>\n<p>5.nginx\u81ea\u67e5<\/p>\n<pre><code class=\"language-bash\">1.\u4fee\u6539\u5b8c\u914d\u7f6e\u8bb0\u5f97\u4f7f\u7528 nginx -t \u68c0\u67e5\u8bed\u6cd5\n2.\u5982\u679c\u6ca1\u6709\u68c0\u67e5\u8bed\u6cd5\uff0c\u76f4\u63a5\u91cd\u8f7d\u5bfc\u81f4\u62a5\u9519\u3002systemctl status nginx -l \u67e5\u770b\u9519\u8bef\u4fe1\u606f<\/code><\/pre>\n<h5>nginx\u65e5\u5fd7<\/h5>\n<p>1.\u8bbf\u95ee\u65e5\u5fd7\uff1a<\/p>\n<pre><code class=\"language-bash\">server {\n    listen 80;\n    server_name code.xxx.com;\n\n    #\u5c06\u5f53\u524d\u7684server\u7f51\u7ad9\u7684\u8bbf\u95ee\u65e5\u5fd7\u8bb0\u5f55\u81f3\u5bf9\u5e94\u7684\u76ee\u5f55\uff0c\u4f7f\u7528main\u683c\u5f0f\n    access_log \/var\/log\/nginx\/code.xxx.com.log main;\n    location \/ {\n        root \/code;\n    }\n\n    #\u5f53\u6709\u4eba\u8bf7\u6c42\u6539favicon.ico\u65f6\uff0c\u4e0d\u8bb0\u5f55\u65e5\u5fd7\n    location \/favicon.ico {\n        access_log off;\n        return 200;\n    }\n}<\/code><\/pre>\n<p>2.\u9519\u8bef\u65e5\u5fd7<\/p>\n<blockquote>\n<p>tail -f \/var\/log\/nginx\/error.log<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u5c0f\u767d\u5b66nginx\u4e4bnginx\u7684\u57fa\u7840 Nginx\u5b89\u88c5\u548c\u542f\u52a8 1.yum\u65b9\u5f0f\uff08\u5b98\u65b9\uff09 [root@web01 ~] [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[352,351],"tags":[],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/4608"}],"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=4608"}],"version-history":[{"count":0,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/4608\/revisions"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}