Nginx學習

Nginx學習

最近工做中,每作一次新項目,都是在不一樣的域名中,遇到各類跨域問題,讓運維和後端配下環境,也是各類不專業,算了,本身來來,學習了下nginx的運用,只要能配置運用就行,不要求深刻掌握php

1.Nginx經常使用功能

1.1.Http代理,反向代理
  • 即只要瀏覽器發起HTTP請求,無論是瀏覽器地址欄裏的請求,仍是圖片文件等資源請求,Nginx能夠根據不一樣的正則匹配,採起不一樣的轉發策略
1.2負載均衡
  • Nginx提供的負載均衡策略有2種:內置策略和擴展策略。內置策略爲輪詢,加權輪詢,Ip hash。擴展策略,就天馬行空,只有你想不到的沒有他作不到的啦,你能夠參照全部的負載均衡算法,給他一一找出來作下實現。
  • 上3個圖,理解這三種負載均衡算法的實現

我我的理解,輪詢就是對同一個ip的不一樣請求在不一樣的web服務器上執行,而加權輪詢就在輪詢的基礎是,加了個權重,優先執行哪一個。html

  • Ip hash算法,對客戶端請求的ip進行hash操做,而後根據hash結果將同一個客戶端ip的請求分發給同一臺服務器進行處理,能夠解決session不共享的問題。

這負載均衡不是很理解,先記錄下吧,之後用到研究python

1.3web緩存
  • Nginx能夠對不一樣的文件作不一樣的緩存處理,配置靈活,而且支持FastCGI_Cache,主要用於對FastCGI的動態程序進行緩存。配合着第三方的ngx_cache_purge,對制定的URL緩存內容能夠的進行增刪管理。
1.4 下載地址

Nginx配置文件結構

在nginx.conf的註釋符號位#nginx


默認的configweb

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /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文件結構算法

...              #全局塊

events {         #events塊
   ...
}

http      #http塊
{
    ...   #http全局塊
    server        #server塊
    { 
        ...       #server全局塊
        location [PATTERN]   #location塊
        {
            ...
        }
        location [PATTERN] 
        {
            ...
        }
    }
    server
    {
      ...
    }
    ...     #http全局塊
}
複製代碼
  • 全局塊:配置影響nginx全局的指令。通常有運行nginx服務器的用戶組,nginx進程pid存放路徑,日誌存放路徑,配置文件引入,容許生成worker process數等。
  • events塊:配置影響nginx服務器或與用戶的網絡鏈接。有每一個進程的最大鏈接數,選取哪一種事件驅動模型處理鏈接請求,是否容許同時接受多個網路鏈接,開啓多個網絡鏈接序列化等。
  • http塊:能夠嵌套多個server,配置代理,緩存,日誌定義等絕大多數功能和第三方模塊的配置。如文件引入,mime-type定義,日誌自定義,是否使用sendfile傳輸文件,鏈接超時時間,單鏈接請求數等。
  • server塊:配置虛擬主機的相關參數,一個http中能夠有多個server。
  • location塊:配置請求的路由,以及各類頁面的處理狀況。

下面上一個配置文件,示例一下後端

########### 每一個指令必須有分號結束。#################
#user administrator administrators; #配置用戶或者組,默認爲nobody nobody。
#worker_processes 2; #容許生成的進程數,默認爲1
#pid /nginx/pid/nginx.pid; #指定nginx進程運行文件存放地址
error_log log/error.log debug;  #制定日誌路徑,級別。這個設置能夠放入全局塊,http塊,server塊,級別以此爲:debug|info|notice|warn|error|crit|alert|emerg
events {
    accept_mutex on;   #設置網路鏈接序列化,防止驚羣現象發生,默認爲on
    multi_accept on;  #設置一個進程是否同時接受多個網絡鏈接,默認爲off
    #use epoll; #事件驅動模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
    worker_connections  1024;    #最大鏈接數,默認爲512
}
http {
    include       mime.types;   #文件擴展名與文件類型映射表
    default_type  application/octet-stream; #默認文件類型,默認爲text/plain
    #access_log off; #取消服務日誌 
    log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定義格式
    access_log log/access.log myFormat;  #combined爲日誌格式的默認值
    sendfile on;   #容許sendfile方式傳輸文件,默認爲off,能夠在http塊,server塊,location塊。
    sendfile_max_chunk 100k;  #每一個進程每次調用傳輸數量不能大於設定的值,默認爲0,即不設上限。
    keepalive_timeout 65;  #鏈接超時時間,默認爲75s,能夠在http,server,location塊。

    upstream mysvr {   
      server 127.0.0.1:7878;
      server 192.168.10.121:3333 backup;  #熱備
    }
    error_page 404 https://www.baidu.com; #錯誤頁
    server {
        keepalive_requests 120; #單鏈接請求上限次數。
        listen       4545;   #監聽端口
        server_name  127.0.0.1;   #監聽地址 
        location  ~*^.+$ {       #請求的url過濾,正則匹配,~爲區分大小寫,~*爲不區分大小寫。
           #root path; #根目錄
           #index vv.txt; #設置默認頁
           proxy_pass  http://mysvr;  #請求轉向mysvr 定義的服務器列表
           deny 127.0.0.1;  #拒絕的ip
           allow 172.18.5.54; #容許的ip 
        } 
    }
}
複製代碼
上面是nginx的基本配置,須要注意的有如下幾點:
  • 語法
    • remote_addr 與http_x_forwarded_for 用以記錄客戶端的ip地址;
    • $remote_user :用來記錄客戶端用戶名稱;
    • $time_local : 用來記錄訪問時間與時區;
    • $request : 用來記錄請求的url與http協議;
    • $status : 用來記錄請求狀態;成功是200;
    • $body_bytes_s ent :記錄發送給客戶端文件主體內容大小;
    • $http_referer :用來記錄從那個頁面連接訪問過來的;
    • $http_user_agent :記錄客戶端瀏覽器的相關信息;
  • 驚羣現象 :一個網路鏈接到來,多個睡眠的進程被同事叫醒,但只有一個進程能得到連接,這樣會影響系統性能。
  • 每一個指令必須有分號結束。
相關文章
相關標籤/搜索