天天看點

Nginx學習筆記(一)

Nginx學習筆記(一)

一、Nginx的特點與作用

     Nginx可以更快地響應請求。

     Web和反向代理伺服器

     支援非常多的伺服器軟體特性

     處理靜态資源

     用作反向代理

     用作負載均衡

二、Nginx編譯安裝    

 2.1 準備工作

  作業系統:CentOS7.3 IP位址:10.0.0.110

<code>yum </code><code>install</code> <code>-y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel</code>

 2.2 添加使用者

<code>useradd</code> <code>-r nginx</code>

 2.3 編譯安裝  

    ./configure --prefix=/usr/local/nginx --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 --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-threads --with-file-aio

     make &amp;&amp; make install

三、簡單web站點配置

 3.1設定環境變量  

  vi /etc/profile.d/nginx.sh #在最後一行加入下面字段

  export PATH="$PATH:/usr/local/nginx/sbin/"

 3.2啟動nginx

  nginx

 3.3配置nginx的web站點

  編譯配置檔案

   vi /etc/nginx.conf   

   站點A配置段如下:

<code>server {</code>

<code>        </code><code>listen       80;</code>

<code>        </code><code>server_name  www.huwho.cn;</code>

<code>            </code><code>root </code><code>/nginx/web</code><code>;</code>

<code>            </code><code>index index.html;</code>

<code>        </code><code>#charset koi8-r;</code>

<code>        </code><code>#access_log  logs/host.access.log  main;</code>

<code>        </code><code>location </code><code>/images/</code> <code>{</code>

<code>            </code><code>root   </code><code>/nginx/</code><code>;</code>

<code>        </code><code>}</code>

<code>        </code><code>#error_page  404              /404.html;</code>

<code>        </code><code># redirect server error pages to the static page /50x.html</code>

<code>        </code><code>#</code>

<code>        </code><code>error_page   500 502 503 504  </code><code>/50x</code><code>.html;</code>

<code>        </code><code>location = </code><code>/50x</code><code>.html {</code>

<code>            </code><code>root   html;</code>

    站點B配置段如下:

<code> </code><code>server {</code>

<code>        </code><code>listen 80;</code>

<code>        </code><code>server_name blog.huwho.cn;</code>

<code>        </code><code>root </code><code>/nginx/blog</code><code>;</code>

<code>        </code><code>index index.html;</code>

<code>      </code><code>}</code>

<code>}</code>

 3.4測試nginx文法

<code>[root@pxe31 nginx]</code><code># nginx -t</code>

<code>nginx: the configuration </code><code>file</code> <code>/etc/nginx/nginx</code><code>.conf syntax is ok</code>

<code>nginx: configuration </code><code>file</code> <code>/etc/nginx/nginx</code><code>.conf </code><code>test</code> <code>is successful</code>

 3.5重載nginx

<code>[root@pxe31 nginx]</code><code># nginx -s reload</code>

 3.6站點目錄以及檔案配置

  mkdir /nginx/{web,blog}

 3.7修改windows的hosts檔案

<a href="https://s1.51cto.com/wyfs02/M00/99/3B/wKiom1lGFPCzYHH9AABxV8ZXkG0344.png" target="_blank"></a>

 3.8通路測試

<a href="https://s4.51cto.com/wyfs02/M02/99/3B/wKiom1lGFgHjA9IpAABMNtzc9cM449.png" target="_blank"></a>

本文轉自 PowerMichael 51CTO部落格,原文連結:http://blog.51cto.com/huwho/1939487,如需轉載請自行聯系原作者

繼續閱讀