Nginx學習筆記<一>Nginx基礎

什麼是Nginx
Nginx是高性能的HTTP和反向代理的服務器,處理高併發能力是十分強大的,能經受高負載的考驗,有報告代表能支持高達 50,000 個併發鏈接數php

Nginx做用
1.反向代理
a.正向代理
須要在客戶端(瀏覽器)配置代理服務器,經過代理服務器進行互聯網訪問
8D2ZTSZ{ZKJG_}`57%U{14H.pnghtml

b.反向代理
其實客戶端對代理是無感知的,由於客戶端不須要任何配置就能夠訪問,咱們只 須要將請求發送到反向代理服務器,由反向代理服務器去選擇目標服務器獲取數據後,在返 回給客戶端,此時反向代理服務器和目標服務器對外就是一個服務器,暴露的是代理服務器 地址,隱藏了真實服務器 IP 地址。
M)D)LO7W~OP0[7@TORWTJ5S.pngnginx

2.負載均衡
單個服務器解決不了,增長服務器的數量,而後將請求分發到各個服務器上,將原先請求集中到單個Nginx服務器上的 狀況改成將請求分發到多個服務器上,將負載分發到不一樣的服務器,也就是咱們所說的負載均衡
1KFFR~4T@6U]F{`WK%8Z{AM.png瀏覽器

3.動靜分離
爲了加快網站的解析速度,能夠把動態頁面和靜態頁面由不一樣的服務器來解析,加快解析速度。下降原來單個服務器的壓力。
JB[C4_YDLHEC$YMWXK2HY(7.png緩存

nginx經常使用命令
1.使用nginx命令前提條件:必須進入nginx錄/usr/local/nginx/sbin
2.查看nginx版本號
./nginx -v
3.啓動nginx
./nginx
4.關閉nginx
./nginx -s stop
5.從新加載nginx
./nginx -s reload服務器

nginx配置文件
vi /usr/local/nginx/conf/nginx.conf網絡

#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  192.168.148.131;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        location /image/{
            root   /data/;
            autoindex on;
        }

        #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由三部分組成
1.全局塊
從配置文件開始到 events 塊之間的內容,主要會設置一些影響nginx 服務器總體運行的配置指令,主要包括配 置運行 Nginx 服務器的用戶(組)、容許生成的 worker process 數,進程 PID 存放路徑、日誌存放路徑和類型以 及配置文件的引入等。
好比上面第一行配置的session

worker_processes  1;

這是 Nginx 服務器併發處理服務的關鍵配置,worker_processes 值越大,能夠支持的併發處理量也越多,可是 會受到硬件、軟件等設備的制約
2.event塊
如:併發

events {
    worker_connections  1024;
}

events 塊涉及的指令主要影響 Nginx 服務器與用戶的網絡鏈接,經常使用的設置包括是否開啓對多 work process 下的網絡鏈接進行序列化,是否容許同時接收多個網絡鏈接,選取哪一種事件驅動模型來處理鏈接請求,每一個 word process 能夠同時支持的最大鏈接數等。
上述例子就表示每一個 work process 支持的最大鏈接數爲 1024。app

3.http塊

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    server {
        listen       80;
        server_name  192.168.148.131;
        location /html/{
            root   /data/;
            index  index.html index.htm;
        }
        
        location /image/{
            root   /data/;
            autoindex on;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

這算是 Nginx 服務器配置中最頻繁的部分,代理、緩存和日誌定義等絕大多數功能和第三方模塊的配置都在這裏。須要注意的是:http 塊也能夠包括 http全局塊、server 塊a.http全局塊http全局塊配置的指令包括文件引入、MIME-TYPE 定義、日誌自定義、鏈接超時時間、單連接請求數上限等。 b.server 塊這塊和虛擬主機有密切關係,虛擬主機從用戶角度看,和一臺獨立的硬件主機是徹底同樣的,該技術的產生是爲了 節省互聯網服務器硬件成本。 每一個 http 塊能夠包括多個 server 塊,而每一個 server 塊就至關於一個虛擬主機。而每一個 server 塊也分爲全局 server 塊,以及能夠同時包含多個 locaton 塊。 <1> 全局 server 塊 最多見的配置是本虛擬機主機的監聽配置和本虛擬主機的名稱或IP配置。 <2> location 塊 一個 server 塊能夠配置多個 location 塊。 這塊的主要做用是基於 Nginx服務器接收到的請求字符串(例如 server_name/uri-string),對虛擬主機名稱 (也能夠是IP 別名)以外的字符串(例如 前面的 /uri-string)進行匹配,對特定的請求進行處理。地址定向、數據緩 存和應答控制等功能,還有許多第三方模塊的配置也在這裏進行

相關文章
相關標籤/搜索