#運(yùn)行用戶
user nobody;
#啟動(dòng)進(jìn)程,通常設(shè)置成和cpu的數(shù)量相等
worker_processes 1;
#全局錯(cuò)誤日志及PID文件
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#工作模式及連接數(shù)上限
events {
#epoll是多路復(fù)用IO(I/O Multiplexing)中的一種方式,
#僅用于linux2.6以上內(nèi)核,可以大大提高nginx的性能
use epoll;
#單個(gè)后臺(tái)worker process進(jìn)程的最大并發(fā)鏈接數(shù)
worker_connections 1024;
# 并發(fā)總數(shù)是 worker_processes 和 worker_connections 的乘積
# 即 max_clients = worker_processes * worker_connections
# 在設(shè)置了反向代理的情況下,max_clients = worker_processes * worker_connections / 4 為什么
# 為什么上面反向代理要除以4,應(yīng)該說(shuō)是一個(gè)經(jīng)驗(yàn)值
# 根據(jù)以上條件,正常情況下的Nginx Server可以應(yīng)付的最大連接數(shù)為:4 * 8000 = 32000
# worker_connections 值的設(shè)置跟物理內(nèi)存大小有關(guān)
# 因?yàn)椴l(fā)受IO約束,max_clients的值須小于系統(tǒng)可以打開的最大文件數(shù)
# 而系統(tǒng)可以打開的最大文件數(shù)和內(nèi)存大小成正比,一般1GB內(nèi)存的機(jī)器上可以打開的文件數(shù)大約是10萬(wàn)左右
# 我們來(lái)看看360M內(nèi)存的VPS可以打開的文件句柄數(shù)是多少:
# $ cat /proc/sys/fs/file-max
# 輸出 34336
# 32000 < 34336,即并發(fā)連接總數(shù)小于系統(tǒng)可以打開的文件句柄總數(shù),這樣就在操作系統(tǒng)可以承受的范圍之內(nèi)
# 所以,worker_connections 的值需根據(jù) worker_processes 進(jìn)程數(shù)目和系統(tǒng)可以打開的最大文件總數(shù)進(jìn)行適當(dāng)?shù)剡M(jìn)行設(shè)置
# 使得并發(fā)總數(shù)小于操作系統(tǒng)可以打開的最大文件數(shù)目
# 其實(shí)質(zhì)也就是根據(jù)主機(jī)的物理CPU和內(nèi)存進(jìn)行配置
# 當(dāng)然,理論上的并發(fā)總數(shù)可能會(huì)和實(shí)際有所偏差,因?yàn)橹鳈C(jī)還有其他的工作進(jìn)程需要消耗系統(tǒng)資源。
# ulimit -SHn 65535
}
http {
#設(shè)定mime類型,類型由mime.type文件定義
include mime.types;
default_type application/octet-stream;
#設(shè)定日志格式
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 指令指定 nginx 是否調(diào)用 sendfile 函數(shù)(zero copy 方式)來(lái)輸出文件,
#對(duì)于普通應(yīng)用,必須設(shè)為 on,
#如果用來(lái)進(jìn)行下載等應(yīng)用磁盤IO重負(fù)載應(yīng)用,可設(shè)置為 off,
#以平衡磁盤與網(wǎng)絡(luò)I/O處理速度,降低系統(tǒng)的uptime.
sendfile on;
#tcp_nopush on;
#連接超時(shí)時(shí)間
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
#開啟gzip壓縮
gzip on;
gzip_disable "MSIE [1-6].";
#設(shè)定請(qǐng)求緩沖
client_header_buffer_size 128k;
large_client_header_buffers 4 128k;
#設(shè)定虛擬主機(jī)配置
server {
#偵聽80端口
listen 80;
#定義使用 www.nginx.cn訪問(wèn)
server_name www.nginx.cn;
#定義服務(wù)器的默認(rèn)網(wǎng)站根目錄位置
root html;
#設(shè)定本虛擬主機(jī)的訪問(wèn)日志
access_log logs/nginx.access.log main;
#默認(rèn)請(qǐng)求
location / {
#定義首頁(yè)索引文件的名稱
index index.php index.html index.htm;
}
# 定義錯(cuò)誤提示頁(yè)面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
#靜態(tài)文件,nginx自己處理
location —— ^/(images|javascript|js|css|flash|media|static)/ {
#過(guò)期30天,靜態(tài)文件不怎么更新,過(guò)期可以設(shè)大一點(diǎn),
#如果頻繁更新,則可以設(shè)置得小一點(diǎn)。
expires 30d;
}
#PHP 腳本請(qǐng)求全部轉(zhuǎn)發(fā)到 FastCGI處理. 使用FastCGI默認(rèn)配置.
location —— .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#禁止訪問(wèn) .htxxx 文件
location —— /.ht {
deny all;
}
}
}