天天看點

Nginx之nginx.conf 配置檔案(二)

上一篇部落格我們将 nginx 安裝在 /usr/local/nginx 目錄下,其預設的配置檔案都放在這個目錄的 conf 目錄下,而主配置檔案 nginx.conf 也在其中,後續對 nginx 的使用基本上都是對此配置檔案進行相應的修改,是以本篇部落格我們先大緻介紹一下該配置檔案的結構。

  

Nginx之nginx.conf 配置檔案(二)

回到頂部

1、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  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之nginx.conf 配置檔案(二)
1 worker_processes  1;
 2 
 3 events {
 4     worker_connections  1024;
 5 }
 6 
 7 
 8 http {
 9     include       mime.types;
10     default_type  application/octet-stream;
11 
12 
13     sendfile        on;
14 
15     keepalive_timeout  65;
16 
17     server {
18         listen       80;
19         server_name  localhost;
20 
21         location / {
22             root   html;
23             index  index.html index.htm;
24         }
25 
26         error_page   500 502 503 504  /50x.html;
27         location = /50x.html {
28             root   html;
29         }
30 
31     }
32 
33 }
           
Nginx之nginx.conf 配置檔案(二)

   根據上述檔案,我們可以很明顯的将 nginx.conf 配置檔案分為三部分:

回到頂部

2、全局塊

  從配置檔案開始到 events 塊之間的内容,主要會設定一些影響nginx 伺服器整體運作的配置指令,主要包括配置運作 Nginx 伺服器的使用者(組)、允許生成的 worker process 數,程序 PID 存放路徑、日志存放路徑和類型以及配置檔案的引入等。

  比如上面第一行配置的:

worker_processes  1;
           

  這是 Nginx 伺服器并發處理服務的關鍵配置,worker_processes 值越大,可以支援的并發處理量也越多,但是會受到硬體、軟體等裝置的制約,這個後面會詳細介紹。

回到頂部

3、events 塊

  比如上面的配置:

events {
    worker_connections  1024;
}
           

  events 塊涉及的指令主要影響 Nginx 伺服器與使用者的網絡連接配接,常用的設定包括是否開啟對多 work process 下的網絡連接配接進行序列化,是否允許同時接收多個網絡連接配接,選取哪種事件驅動模型來處理連接配接請求,每個 word process 可以同時支援的最大連接配接數等。

  上述例子就表示每個 work process 支援的最大連接配接數為 1024.

  這部分的配置對 Nginx 的性能影響較大,在實際中應該靈活配置。

回到頂部

4、http 塊

Nginx之nginx.conf 配置檔案(二)
1 http {
 2     include       mime.types;
 3     default_type  application/octet-stream;
 4 
 5 
 6     sendfile        on;
 7 
 8     keepalive_timeout  65;
 9 
10     server {
11         listen       80;
12         server_name  localhost;
13 
14         location / {
15             root   html;
16             index  index.html index.htm;
17         }
18 
19         error_page   500 502 503 504  /50x.html;
20         location = /50x.html {
21             root   html;
22         }
23 
24     }
25 
26 }
           
Nginx之nginx.conf 配置檔案(二)

  這算是 Nginx 伺服器配置中最頻繁的部分,代理、緩存和日志定義等絕大多數功能和第三方子產品的配置都在這裡。

  需要注意的是:http 塊也可以包括 http全局塊、server 塊。

①、http 全局塊

  http全局塊配置的指令包括檔案引入、MIME-TYPE 定義、日志自定義、連接配接逾時時間、單連結請求數上限等。

②、server 塊

  這塊和虛拟主機有密切關系,虛拟主機從使用者角度看,和一台獨立的硬體主機是完全一樣的,該技術的産生是為了節省網際網路伺服器硬體成本。後面會詳細介紹虛拟主機的概念。

  每個 http 塊可以包括多個 server 塊,而每個 server 塊就相當于一個虛拟主機。

  而每個 server 塊也分為全局 server 塊,以及可以同時包含多個 locaton 塊。

  1、全局 server 塊

  最常見的配置是本虛拟機主機的監聽配置和本虛拟主機的名稱或IP配置。

  2、location 塊

  一個 server 塊可以配置多個 location 塊。

  這塊的主要作用是基于 Nginx  伺服器接收到的請求字元串(例如 server_name/uri-string),對虛拟主機名稱(也可以是IP别名)之外的字元串(例如 前面的 /uri-string)進行比對,對特定的請求進行處理。位址定向、資料緩存和應答控制等功能,還有許多第三方子產品的配置也在這裡進行。

繼續閱讀