全面解析|搞懂Nginx這一篇就夠了

前言

Nginx是一個http服務器,是一個使用c語言開發的高性能的http服務器及反向代理服務器。Nginx是一款高性能的http服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器。由俄羅斯的程序設計師Igor Sysoev所開發,官方測試Nginx可以支撐5萬併發連接,而且cpu、內存等資源消耗卻很是低,運行很是穩定。本文將爲你們詳細介紹關於Nginx的原理以及在應用場景下的相關解析。javascript

 

1. Nginx的概念?php

「什麼是Nginx呢:」css

  • Nginx是一個基於HTTP的反向代理服務器,也是一個基IMAP/POP3/SMTP服務郵件服務器html

  • 反向代理服務器:如今咱們A須要訪問的目標B服務器的10.7.182.100,我要訪問這個B服務器上的資源,如今若是使用了Nginx以後,我能夠經過Nginx服務器從而達到訪問10.7.182.100這個服務器的目的java

  • IMAP/POP/SMTP:這三個是郵件的傳輸協議linux

  • 郵件服務器:發送郵件    接收郵件nginx

  • Web服務器:自己是一個Web服務器的軟件,相似於Tomcat這種Web服務的軟件web

「Nginx能幹什麼呢:」瀏覽器

  • 能夠做爲Web服務器服務器

  • 能夠做爲郵件服務器

  • 能夠做爲反向代理的服務器

  • 動靜分離(就是將動態資源和靜態資源分隔開)

  • 能夠實現負載均衡

二、Nginx的安裝

「Nginx安裝步驟:」

第一步:下載咱們的nginx 這裏以1.6.2版本爲例


第二步:共享安裝文件到咱們的linux上面


第三步:將文件拷貝到/usr/local下面


第四步:安裝 tar -zxvf xxxx.tar.gz


第五步:下載所須要的依賴庫 yum install pcre  yum install pcre-devel  yum install zlib   yum install zlib-devel


第六步:進行config的配置
cd nginx-1.6.2 && ./configure --prefix=/usr/local/nginx


第七步:安裝


make && make install 


第八步:啓動nginx


/usr/local/nginx/sbin/nginx


關閉: .... -s stop -s reload 


查看端口是否有問題


netstat -tunpl |grep 80


瀏覽器進行驗證沒問題就能夠

三、Nginx的配置文件的解析

「配置文件:」

#user  nobody;
#工做的線程(4核8線程那麼下面就設置成8 物理硬件有關)
worker_processes  1;

#全局的錯誤日誌存放的地方
#error_log  logs/error.log;

#全局錯誤日誌存放的地方 後面的notice表示的是輸出錯誤日誌的格式
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#nginx進程號存放的地方
#pid        logs/nginx.pid;


#最大的鏈接數(這個也跟硬件是有關係的)
events {
    worker_connections  1024;
}


#下面就是跟HTTP請求有關係的了
http {
    #表示的是當前服務器支持的類型
    include       mime.types;
    #默認傳輸的數據類型是流 
    default_type  application/octet-stream;

    #聲明瞭一種日誌格式 這種日誌格式的名字叫作  main
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    #表示的是每一次請求的日誌記錄 格式就是上面的main格式
    access_log  logs/access.log  main;
    
    #是否能夠發送文件
    sendfile        on;
    #tcp_nopush     on;

    #這個是雙活的超時的時間
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #是否打開文件的壓縮
    #gzip  on;

    #虛擬一個主機
   
   #負載均衡的配置
   upstream qianyu {
      ip_hash;
      server 10.7.182.110:8080;
      server 10.7.182.87:8080;
   }

    #server {
      # listen     90;
      # server_name localhost; 
      # location / {
      #    root qianyu;
      #    index qianyu.html;
      # }
      #作一個反向代理
      #表示的是 若是你訪問的後綴是 .jpg結尾的話那麼 就訪問下面的另外的服務器
       # location ~ \.jpg$ {
       #     proxy_pass   http://10.7.182.110:8080;
       # }

      #下面要演示一個負載均衡的例子
       #location ~ \.jpg$ {
       #    proxy_pass   http://qianyu;
       #}
    #}

   #虛擬一個動靜分離的機器
   server {
     listen     9999;
     server_name localhost;
     
     #表示的是動態資源訪問的機器

      location / {
            proxy_pass   http://10.7.182.54:8080;
      }
     
 
    #表示的是非  css和js文件訪問的地址
    location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
        {
            root /usr/local/webapp;
            expires 30d;
        }
      #表示的是css和js的訪問地址
      location ~ .*\.(js|css)?$
       {
         root /usr/local/webapp;
         expires 1h;
       }

  }

    #虛擬了服務器
    server {
        #端口是  80
        listen       80;
        #這個表示的是虛擬服務器默認訪問的是本機
        server_name  localhost;
        #這個是虛擬服務器的編碼
        #charset koi8-r;
        #當前服務器的日誌的存儲的地方
        #access_log  logs/host.access.log  main;

        #作了一個地址的映射
        location / {
            root   html;
            index  index.html index.htm;
        }
        
        #若是報404的時候訪問的頁面
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #報錯50開頭的是 就訪問50x.html
        error_page   500 502 503 504  /50x.html;
        #下面對50x.html又作了一個映射  就訪問根目錄下的html中的  50x.html
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

四、實現Nginx下的反向代理

「作一個反向代理:」

  • 表示的是若是你訪問的後綴是 .jpg結尾的話,那麼就訪問下面的另外的服務器

location ~ \.jpg$ {
            proxy_pass   http://10.7.182.110:8080;
        }

‍五、實現Nginx下的負載均衡

「第一種策略:默認是輪循的策略:」

配置upstream
   upstream qianyu {
      server 10.7.182.110:8080;
      server 10.7.182.87:8080;
   }
   配置負載均衡
   location ~ \.jpg$ {
           proxy_pass   http://qianyu;
   }

「第二種策略:權重(weight):」

#負載均衡的配置
  upstream qianyu {
     server 10.7.182.110:8080 weight=2;
     server 10.7.182.87:8080 weight=1;
  }
 配置負載均衡
     location ~ \.jpg$ {
           proxy_pass   http://qianyu;
     }

「第三種策略:IPHash的使用:」

 #負載均衡的配置
   upstream qianyu {
      ip_hash;
      server 10.7.182.110:8080;
      server 10.7.182.87:8080;
   }
配置負載均衡
    location ~ \.jpg$ {
           proxy_pass   http://qianyu;
    }

六、實現Nginx下的動靜分離

「動靜分離:」

  • 簡單的來講就是將動態資源和靜態資源給分隔開

  • 靜態資源放到Nginx服務器上

  • 動態資源放到Tomcat服務器上

Jpg   html   css    png   gif    ....靜態資源  ---->放到Nginx服務器上
.action結尾的都是動態的資源     ------------------------->放到Tomcat的服務器上

「動靜分離的實現:」

  • 在/usr/local目錄下建立webapp文件夾

  • 在webapp目錄下建立css、html、js、img文件夾

  • 編寫HTML的內容並將html文件放到webapp目錄下

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8" />
  <title></title>
  <link rel="stylesheet" type="text/css" href="/css/main.css"/>
  <script src="/js/main.js" type="text/javascript" charset="utf-8"></script>
 </head>
 <body>
  <img src="/img/bbb.jpg"/>
  <br />
  <a href="/Dynamic_Resource/qianyu.action">點擊我訪問動態資源</a>
 
 </body>
</html>

  • 將圖片放到img目錄下,將css放到css目錄下,將js文件放到js的目錄下

  • 編寫動態資源的這個工程

package com.qy.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class qianyuServlet extends HttpServlet {

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
            this.doPost(request, response);
 }

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  response.setContentType("text/html;charset=utf-8");
  PrintWriter writer=response.getWriter();
  writer.write("this is dynamic resource test");
  writer.flush();
  writer.close(); 
 }
}

  • 編寫配置文件    /conf/nginx.xml文件

#虛擬一個動靜分離的機器
   server {
     listen     9999;
     server_name localhost;

     #表示的是動態資源訪問的機器

      location / {
            proxy_pass   http://10.7.182.54:8080;
      }
    #表示的是非  css和js文件訪問的地址
    location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
        {
            root /usr/local/webapp;
            expires 30d;
        }
      #表示的是css和js的訪問地址
      location ~ .*\.(js|css)?$
  {
         root /usr/local/webapp;
         expires 1h;
       }

  }

  • 測試

七、虛擬主機

「虛擬主機配置:」

server {
       listen     90;
       server_name localhost;
       location / {
          root qianyu;
          index qianyu.html;
       }
      #作一個反向代理
      #表示的是 若是你訪問的後綴是 .jpg結尾的話那麼 就訪問下面的另外的服務器
        location ~ \.jpg$ {
            proxy_pass   http://10.7.182.110:8080;
        }
}

結語

本篇關於Nginx的介紹就先到這裏結束了,後續會出更多關於Nginx系列更多文章,謝謝你們支持!

點個贊,證實你還愛我

相關文章
相關標籤/搜索