天天看點

Nginx配置檔案詳解(nginx.conf)

預設配置檔案

#user  nobody;
worker_processes  ;

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

#pid        logs/nginx.pid;


events {
    worker_connections  ;
}


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  ;

    #gzip  on;

    server {
        listen       ;
        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        /x.html;
        location = /x.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全局塊
}
           

1、全局塊:配置影響nginx全局的指令。一般有運作nginx伺服器的使用者組,nginx程序pid存放路徑,日志存放路徑,配置檔案引入,允許生成worker process數等。

2、events塊:配置影響nginx伺服器或與使用者的網絡連接配接。有每個程序的最大連接配接數,選取哪種事件驅動模型處理連接配接請求,是否允許同時接受多個網路連接配接,開啟多個網絡連接配接序列化等。

3、http塊:可以嵌套多個server,配置代理,緩存,日志定義等絕大多數功能和第三方子產品的配置。如檔案引入,mime-type定義,日志自定義,是否使用sendfile傳輸檔案,連接配接逾時時間,單連接配接請求數等。

4、server塊:配置虛拟主機的相關參數,一個http中可以有多個server。

5、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  ;    #最大連接配接數,預設為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 ;  #每個程序每次調用傳輸數量不能大于設定的值,預設為0,即不設上限。
    keepalive_timeout ;  #連接配接逾時時間,預設為75s,可以在http,server,location塊。

    upstream mysvr {   
        # down 表示單前的server暫時不參與負載
        # Weight 預設為1.weight越大,負載的權重就越大。
        # max_fails允許請求失敗的次數預設為1.當超過最大次數時,傳回proxy_next_upstream 子產品定義的錯誤
        # fail_timeout max_fails 次失敗後,暫停的時間。
        # Backup其它所有的非backup機器down或者忙的時候,請求backup機器。是以這台機器壓力會最輕。
        # ip_hash;
        server ;
        server  backup;  #熱備
    }
    error_page  https://www.baidu.com; #錯誤頁
    server {
        keepalive_requests ; #單連接配接請求上限次數。
        listen       ;   #監聽端口
        server_name  ;   #監聽位址       
        location  ~*^.+$ {       #請求的url過濾,正則比對,~為區分大小寫,~*為不區分大小寫。
           #root path;  #根目錄
           #index vv.txt;  #設定預設頁
           proxy_pass  http://mysvr;  #請求轉向mysvr 定義的伺服器清單
           deny ;  #拒絕的ip
           allow ; #允許的ip           
        } 
    }
}
           

PS:

1、1.remote_addr 與http_x_forwarded_for 用以記錄用戶端的ip位址; 2.remote_user :用來記錄用戶端使用者名稱; 3.time_local : 用來記錄通路時間與時區;4.request : 用來記錄請求的url與http協定;

5.status : 用來記錄請求狀态;成功是200, 6.body_bytes_s ent :記錄發送給用戶端檔案主體内容大小;7.http_referer :用來記錄從那個頁面連結通路過來的; 8.http_user_agent :記錄用戶端浏覽器的相關資訊;

2、驚群現象:一個網路連接配接到來,多個睡眠的程序被同僚叫醒,但隻有一個程序能獲得連結,這樣會影響系統性能。

3、每個指令必須有分号結束。

繼續閱讀