先看看配置檔案的内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<code>user nginx;</code>
<code>worker_processes 4;</code>
<code>error_log </code><code>/var/log/nginx/error</code><code>.log warn;</code>
<code>pid </code><code>/var/run/nginx</code><code>.pid;</code>
<code>events {</code>
<code> </code><code>worker_connections 1024;</code>
<code>}</code>
<code>http {</code>
<code> </code><code>include </code><code>/etc/nginx/mime</code><code>.types;</code>
<code> </code><code>default_type application</code><code>/octet-stream</code><code>;</code>
<code> </code><code>log_format main </code><code>'$remote_addr - $remote_user [$time_local] "$request" '</code>
<code> </code><code>'$status $body_bytes_sent "$http_referer" '</code>
<code> </code><code>'"$http_user_agent" "$http_x_forwarded_for"'</code><code>;</code>
<code> </code><code>access_log </code><code>/var/log/nginx/access</code><code>.log main;</code>
<code> </code><code>sendfile on;</code>
<code> </code><code>#tcp_nopush on;</code>
<code> </code><code>keepalive_timeout 65;</code>
<code> </code><code>#gzip on;</code>
<code> </code><code>include </code><code>/etc/nginx/conf</code><code>.d/*.conf;</code>
Nginx的配置分為全局塊、events塊、http塊和server塊。在nginx.conf檔案中隻包含了全局塊、events塊和http塊的内容,server塊的配置需要自己定義。每一個server塊都可以當做一個虛拟主機,一個http塊可以包含多個server塊,每一個server塊的配置都是獨立的,不會影響到其他server塊。http全局塊的配置對server塊有效,但是如果server塊中和http全局塊的配置沖突,則采用就近原則,以server塊的配置為準。下面來看看配置檔案中這些指令的含義和其用法。
全局配置段:
<code>user nginx;</code>
user :指定可以運作Nginx服務的使用者,隻有被設定的使用者或者使用者組成員才有運作nginx程序的權限,如果希望所有使用者都能運作Nginx程序,則可以注釋掉該指令,或者設定為user nobody nonobody;
user指令用法:
user username [groupname];
<code>work_processes 4;</code>
work_processes:設定Nginx伺服器運作時啟動的程序數,理論上設定的值越大,則Nginx伺服器能響應的請求數越多,但是由于受到伺服器軟硬體(CPU和磁盤驅動器)的限制,是以必須合理設定才行。
一般設定為與CPU核心數相等,或者為CPU核心數減去1,我的CPU為4核,是以我設定為4。
work_processes執行用法:
<code>work_processes number | auto;</code>
number為work_processes啟動的最大processes數,設定為auto時,則Nginx伺服器自動檢測并設定。
error_log:設定Nginx伺服器錯誤日志路徑。
error_log用法:
error_log file | stderr 【debug | info | notice | warn | error | crit | alert | emerg】
從文法結構看,Nginx伺服器的日志支援輸出到某一固定的檔案file,或者輸出到标準錯誤輸出stderr,日志級别是可選項,由低到高為debug(需要編譯時使用--with-debug開啟debug開關)info、notice、warn、error、crit 、alert、emerg等。
pid:指定Nginx伺服器PID檔案的存放路徑。
pid用法:
<code>pid </code><code>file</code><code>;</code>
其他配置指令:
1、worker_cpu_affinity CPUMASK CPUMASK ...;
該指令用來為每個程序配置設定CPU的工作核心,其值為幾組二進制的值表示,例如:
worker_cpu_affinity 0001 0010 0100 1000;
如果CPU為8核,則可以這樣設定:
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 0010000 001000000 10000000;
2、worker_priority number;
指定Nginx程序的CPUnice值,範圍為-20到19,值越小,優先級越高。預設所有程序nice值為0
3、worker_rlimit_nofile number;
指定每個worker程序能打開的最大檔案描述符的數量
4、worker_rlimit_sigpending number;
指定每個使用者能夠發給worker程序的信号的最大數量
events配置段:
<code>worker_connections 1024;</code>
worker_connections:設定每一個work_process的最大并發連接配接數,預設為1024。其文法為:
<code>worker_connections number;</code>
1、accept_mutex on | off;
當某一時刻隻有一個網絡請求進來,多個睡眠的Nginx程序會被叫醒來響應請求,但是隻有一個程序能獲得連接配接,如果每次喚醒的程序數目太多,會影響一部分系統性能。accept_mutex就是為解決這一問題。當設定為on的時候,表示讓多個worker輪流的序列化的響應請求。預設為開啟狀态,其隻能在events段中配置。
2、multi_accpet on |off;
設定每個Nginx程序都能同時接收多個請求,預設為關閉狀态。同樣隻能在events段中配置
3、use 【select | poll | kqueus | epoll | rtsig | /dev/poll | eventport】
該指令用來選擇事件驅動的模型,建議讓Nginx自動選擇
http配置段:
<code>include </code><code>/etc/nginx/mime</code><code>.types;</code>
include:引入其他的Nginx配置或者第三方子產品的配置到目前主配置檔案中,文法為:
<code>include </code><code>file</code><code>;</code>
file為要引入的配置檔案,支援相對路徑。
<code>default_type application</code><code>/octet-stream</code><code>;</code>
default_type:用于指定處理前端請求的MIME類型。此指令還可以在http段、server段、或者location中配置
<code>log_format main </code><code>'$remote_addr - $remote_user [$time_local] "$request" '</code>
<code>access_log </code><code>/var/log/nginx/access</code><code>.log main;</code>
access_log:用于設定通路日志的路徑
log_format:用于設定通路日志的格式,配置檔案中的$remote_addr都是nginx的内置變量
<code>sendfile on;</code>
sendfile:用于設定是否啟用sendfile功能,文法為:sendfile on | off;
sendfile_max_chunk_size;
設定sendfile傳輸的資料量最大值,如果設定為0,則不限制。
<code>keepalive_timeout 65;</code>
keepalive_timeout:設定長連接配接的會話保持時間,該指令還可以在server段和location中設定。其文法為:keepalive timeout [header_timeout];
keepalive_requests 100;
Nginx伺服器和使用者端建立連接配接後,使用者端通過此連接配接發送請求,keepalive_requests指令用于限制使用者通過某一連接配接向Nginx伺服器發送請求的次數。預設為100
<code>include </code><code>/etc/nginx/conf</code><code>.d/*.conf;</code>
設定http端配置包含/etc/nginx/conf.d目錄中所有以".conf"檔案結尾的配置檔案,一般将server段配置檔案放在該目錄下。
本文轉自 曾哥最愛 51CTO部落格,原文連結:http://blog.51cto.com/zengestudy/1769705,如需轉載請自行聯系原作者