linux --nginx篇

NGINX是什麼?

nginx是開源的,支持高性能的,高併發的www服務和代理服務軟件,就是web服務器,nginx不可是一個優秀的web服務軟件,還能夠作反向代理,負載均衡,以及緩存服務使用.html

優勢:支持高併發,支持幾萬的併發鏈接;資源消耗少.在3萬併發鏈接下開啓10個nginx線程消耗內存不到200M;能夠作負載均衡,反向代理;支持異步網絡I/O事件模型epoll前端

Tengine是淘寶網的web項目,是Nginx的高性能版,tengine的性能和穩定性極高.python

web服務器和web框架的關係

web服務器(nginx):接收HTTP請求(例如www.alonemans.com/dudu.jpg)並返回數據linux

web框架(django,flask):開發web應用程序,處理接收到的數據nginx

基礎安裝

  • 下載源碼包  --- wget http://tengine.taobao.org/download/tengine-2.2.0.tar.gz
  • 卸載掉以前經過yum源安裝的nginx  -- yum remove nginx -y
  • 解決編譯安裝的依賴環境  
    yum install gcc patch libffi-devel python-devel  zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel openssl openssl-devel -y

     

  • 解壓縮源碼包 --- tar -zxf tengine-2.2.0.tar.gz
  • 釋放Makefile ---    ./configure  --prefix=/opt/tnginx220
  • 執行  make  && make install
  • 編譯完成後 tnginx能夠正常使用
  • 瞭解tnginx220的目錄結構做用
    /opt/tnginx220
    
    [root@master tnginx220]# ll
    
    drwxr-xr-x. 2 root   root 4096 Mar 11 08:50 conf #放nginx全部配置文件的地兒 drwxr-xr-x. 2 root root 40 Mar 11 08:50 html #存放前端 html文件的 drwxr-xr-x. 2 root root 4096 Mar 11 08:50 include drwxr-xr-x. 2 root root 41 Mar 11 08:52 logs #nginx的日誌文件夾 drwxr-xr-x. 2 root root 6 Mar 11 08:50 modules drwxr-xr-x. 2 root root 35 Mar 11 08:50 sbin #存放nginx二進制命令的
  • 添加linux環境變量PATH,使用快捷命令
    echo $PATH  
    PATH="/opt/python36/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/opt/tnginx220/sbin" #將tnginx220 的路徑添加到PATH中,在後面添加便可

    編輯 #vim /etc/profile
    添加後執行 #source /etc/profile
  • nginx亦能夠正常使用了

nginx.conf配置文件相關

http內核模塊

//公共的配置定義在http{}
http {  //http層開始
... //使用Server配置網站, 每一個Server{}表明一個網站(簡稱虛擬主機) 'server' { listen 80; //監聽端口, 默認80 server_name localhost; //提供服務的域名或主機名 access_log host.access.log //訪問日誌 //控制網站訪問路徑 'location' / { root /usr/share/nginx/html; //存放網站代碼路徑 index index.html index.htm; //服務器返回的默認頁面文件  } //指定錯誤代碼, 統必定義錯誤頁面, 錯誤代碼重定向到新的Locaiton error_page 500 502 503 504 /50x.html; } ... //第二個虛擬主機配置 'server' { ... } include /etc/nginx/conf.d/*.conf; //包含/etc/nginx/conf.d/目錄下全部以.conf結尾的文件 } //http層結束

 

基於域名的多虛擬主機實戰

也就是在一個服務器上運行多個網站web

方法:算法

1.環境準備,準備好兩個域名,這裏模擬的本地域名解析,找到windows下的hosts文件( 因爲咱們是經過windows訪問,達到訪問不一樣的  域名,所以配置windows的hosts)sql

#編輯文件 C:\Windows\System32\drivers\etc\hosts
    
#寫入
192.168.11.229  meihao.com 
192.168.11.229  shenghuo.com

 

2.配置nginx支持多虛擬主機django

  -- 修改nginx.conf 修改2個server虛擬主機配置flask

#meihao的虛擬主機
server {
    listen 80; server_name meihao.com; # 當咱們訪問meihao.com:80/的時候,就進入這個虛擬主機,且找到這個location,進行網站資源分配 location / { root /opt/meihao/; index index.html; } } #第二個虛擬主機,shenghuo server{ listen 80; server_name shenghuo.com; location / { root /opt/shenghuo/; index index.html; } }

  --分別修改兩個網址的根目錄數據

mkdir -p  /opt/{meihao,shenghuo}

#分別在/opt/meihao/建立 index.html;/opt/shenghuo/建立 index.html

  --修改完配置文件,先檢測語法   nginx -t

  -- 平滑加載nginx(不重啓nginx,從新讀取配置文件)       nginx -s  reload

nginx的訪問日誌logs功能

vim nginx.conf  
打開註釋

http {
    include       mime.types;
    default_type  application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; #開啓日誌功能,生成access.log文件,能夠監控訪問者

 

nginx的404網頁優化

編輯nginx.conf

    server {
        listen       80; server_name meihao.com; #charset koi8-r; #access_log logs/host.access.log main;  location / { root /opt/meihao; index index.html index.htm; } #打開這個參數的配置,開啓錯誤頁面 error_page 404 403 402 401 /40x.html; #40幾的錯誤會對應生成40x.html }

 

記得在 /opt/meihao/  建立一個40x.html 文件   --- touch 40x.html  ,寫入你的創意404界面...嘿嘿要美觀,當你訪問的meihao.com/asdfasdas 的時候自動跳轉的舒適的404頁面

nginx拒絕ip訪問

  location / {
            deny  你想限制的ip;  #!!!還能夠限制網段
            root   /opt/meihao; index index.html index.htm; allow 10.0.0.1; #限制網段訪問 }

 

nginx反向代理

什麼是反向代理?

對於客戶端而言,代理服務端就是原始服務端,實際來說,就是爲了保護和隱藏原始服務器不受到攻擊.

反向代理的實現:

  • 環境準備:兩臺服務器
    192.168.220.130   #真是資源服務器
    
    192.168.220.128   #nginx代理服務器

     

  • 咱們做爲客戶端,訪問代理服務器,代理服務器,將資源服務器上的東西進行返回
  • 先配置資源服務器  192.168.220.130  meihao.com
  • 配置代理服務器 192.168.220.128    修改192.168.220.128 這臺機器上的配置文件,開始反向代理
    #配置 nginx.conf 的server{}以下:
    
            server {
                listen       80 ; server_name _; location / { #反向代理參數,當咱們請求192.168.11.136:80/的時候,進入這裏server,而後location進行資源分配 proxy_pass http://192.168.220.130; #就是間接的訪問了 192.168.220.130:80/  } }  

nginx負載均衡

配置nginx負載均衡環境準備,三臺服務器,這三臺服務器都是nginx實現的

192.168.220.128    反向代理服務器

192.168.220.130    資源服務器  返回xiaohua頁面

192.168.220.131    資源服務器   返回一個h1標籤,且行且珍惜

實現過程:

  • 配置反向代理 192.168.220.128服務器   修改nginx.conf參數以下
    #定義負載均衡池
    
    upstream load_leveling_pool{
         server 192.168.220.130; server 192.168.220.131; } #轉發請求給負載均衡池 location /{ proxy_pass http:// load_leveling_pool; }

     

  • 此時經過負載均衡器 192.168.220.128.進行測試訪問,默認是輪詢機制 

  • nginx負載均衡算法
    輪詢      #按照時間順序逐一分配到不一樣的服務器(默認)
    
    weight    #加權輪詢,weight值越大,分配到的訪問概率越高
     upstream s17server { server 192.168.220.130 weight=8; server 192.168.220.131 weight=2; } #此時 192.168.220.130訪問的概率比較高了  ip_hash #每一個請求按照ip的hash結果分配,這樣來自同一ip的固定訪問最後一個服務器  url_hash #按照訪問的urlhash結果來分配,是每一個固定的url定向到一個後端服務器  least_conn #最少連接數,哪一個機器連接數少就分發

....

相關文章
相關標籤/搜索