1 #運行用戶 2 user nobody; 3 #啓動進程,一般設置成和cpu的數量相等 4 worker_processes 1; 5 6 #全局錯誤日誌及PID文件 7 #error_log logs/error.log; 8 #error_log logs/error.log notice; 9 #error_log logs/error.log info; 10 11 #pid logs/nginx.pid; 12 13 #工做模式及鏈接數上限 14 events { 15 #epoll是多路複用IO(I/O Multiplexing)中的一種方式, 16 #僅用於linux2.6以上內核,能夠大大提升nginx的性能 17 use epoll; 18 19 #單個後臺worker process進程的最大併發連接數 20 worker_connections 1024; 21 22 # 併發總數是 worker_processes 和 worker_connections 的乘積 23 # 即 max_clients = worker_processes * worker_connections 24 # 在設置了反向代理的狀況下,max_clients = worker_processes * worker_connections / 4 爲何 25 # 爲何上面反向代理要除以4,應該說是一個經驗值 26 # 根據以上條件,正常狀況下的Nginx Server能夠應付的最大鏈接數爲:4 * 8000 = 32000 27 # worker_connections 值的設置跟物理內存大小有關 28 # 由於併發受IO約束,max_clients的值須小於系統能夠打開的最大文件數 29 # 而系統能夠打開的最大文件數和內存大小成正比,通常1GB內存的機器上能夠打開的文件數大約是10萬左右 30 # 咱們來看看360M內存的VPS能夠打開的文件句柄數是多少: 31 # $ cat /proc/sys/fs/file-max 32 # 輸出 34336 33 # 32000 < 34336,即併發鏈接總數小於系統能夠打開的文件句柄總數,這樣就在操做系統能夠承受的範圍以內 34 # 因此,worker_connections 的值需根據 worker_processes 進程數目和系統能夠打開的最大文件總數進行適當地進行設置 35 # 使得併發總數小於操做系統能夠打開的最大文件數目 36 # 其實質也就是根據主機的物理CPU和內存進行配置 37 # 固然,理論上的併發總數可能會和實際有所誤差,由於主機還有其餘的工做進程須要消耗系統資源。 38 # ulimit -SHn 65535 39 40 } 41 42 43 http { 44 #設定mime類型,類型由mime.type文件定義 45 include mime.types; 46 default_type application/octet-stream; 47 #設定日誌格式 48 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 49 '$status $body_bytes_sent "$http_referer" ' 50 '"$http_user_agent" "$http_x_forwarded_for"'; 51 52 access_log logs/access.log main; 53 54 #sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件, 55 #對於普通應用,必須設爲 on, 56 #若是用來進行下載等應用磁盤IO重負載應用,可設置爲 off, 57 #以平衡磁盤與網絡I/O處理速度,下降系統的uptime. 58 sendfile on; 59 #tcp_nopush on; 60 61 #鏈接超時時間 62 #keepalive_timeout 0; 63 keepalive_timeout 65; 64 tcp_nodelay on; 65 66 #開啓gzip壓縮 67 gzip on; 68 gzip_disable "MSIE [1-6]."; 69 70 #設定請求緩衝 71 client_header_buffer_size 128k; 72 large_client_header_buffers 4 128k; 73 74 75 #設定虛擬主機配置 76 server { 77 #偵聽80端口 78 listen 80; 79 #定義使用 www.nginx.cn訪問 80 server_name www.nginx.cn; 81 82 #定義服務器的默認網站根目錄位置 83 root html; 84 85 #設定本虛擬主機的訪問日誌 86 access_log logs/nginx.access.log main; 87 88 #默認請求 89 location / { 90 91 #定義首頁索引文件的名稱 92 index index.php index.html index.htm; 93 94 } 95 96 # 定義錯誤提示頁面 97 error_page 500 502 503 504 /50x.html; 98 location = /50x.html { 99 } 100 101 #靜態文件,nginx本身處理 102 location ~ ^/(images|javascript|js|css|flash|media|static)/ { 103 104 #過時30天,靜態文件不怎麼更新,過時能夠設大一點, 105 #若是頻繁更新,則能夠設置得小一點。 106 expires 30d; 107 } 108 109 #PHP 腳本請求所有轉發到 FastCGI處理. 使用FastCGI默認配置. 110 location ~ .php$ { 111 fastcgi_pass 127.0.0.1:9000; 112 fastcgi_index index.php; 113 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 114 include fastcgi_params; 115 } 116 117 #禁止訪問 .htxxx 文件 118 location ~ /.ht { 119 deny all; 120 } 121 122 } 123 }
#運行用戶
user
nobody
;
#啓動進程,一般設置成和cpu的數量相等
worker
_processes
1
;
#全局錯誤日誌及PID文件
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#工做模式及鏈接數上限
events
{
#epoll是多路複用IO(I/O Multiplexing)中的一種方式,
#僅用於linux2.6以上內核,能夠大大提升nginx的性能
use
epoll
;
#單個後臺worker process進程的最大併發連接數
worker
_connections
1024
;
# 併發總數是 worker_processes 和 worker_connections 的乘積
# 即 max_clients = worker_processes * worker_connections
# 在設置了反向代理的狀況下,max_clients = worker_processes * worker_connections / 4 爲何
# 爲何上面反向代理要除以4,應該說是一個經驗值
# 根據以上條件,正常狀況下的Nginx Server能夠應付的最大鏈接數爲:4 * 8000 = 32000
# worker_connections 值的設置跟物理內存大小有關
# 由於併發受IO約束,max_clients的值須小於系統能夠打開的最大文件數
# 而系統能夠打開的最大文件數和內存大小成正比,通常1GB內存的機器上能夠打開的文件數大約是10萬左右
# 咱們來看看360M內存的VPS能夠打開的文件句柄數是多少:
# $ cat /proc/sys/fs/file-max
# 輸出 34336
# 32000 < 34336,即併發鏈接總數小於系統能夠打開的文件句柄總數,這樣就在操做系統能夠承受的範圍以內
# 因此,worker_connections 的值需根據 worker_processes 進程數目和系統能夠打開的最大文件總數進行適當地進行設置
# 使得併發總數小於操做系統能夠打開的最大文件數目
# 其實質也就是根據主機的物理CPU和內存進行配置
# 固然,理論上的併發總數可能會和實際有所誤差,由於主機還有其餘的工做進程須要消耗系統資源。
# ulimit -SHn 65535
}
http
{
#設定mime類型,類型由mime.type文件定義
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 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,
#對於普通應用,必須設爲 on,
#若是用來進行下載等應用磁盤IO重負載應用,可設置爲 off,
#以平衡磁盤與網絡I/O處理速度,下降系統的uptime.
sendfile
on
;
#tcp_nopush on;
#鏈接超時時間
#keepalive_timeout 0;
keepalive
_timeout
65
;
tcp_nodelay
on
;
#開啓gzip壓縮
gzip
on
;
gzip
_disable
"MSIE [1-6]."
;
#設定請求緩衝
client_header_buffer
_size
128k
;
large_client_header
_buffers
4
128k
;
#設定虛擬主機配置
server
{
#偵聽80端口
listen
80
;
#定義使用 www.nginx.cn訪問
server_name
www
.
nginx
.
cn
;
#定義服務器的默認網站根目錄位置
root
html
;
#設定本虛擬主機的訪問日誌
access_log
logs
/
nginx
.
access
.
log
main
;
#默認請求
location
/
{
#定義首頁索引文件的名稱
index
index
.
php
index
.
html
index
.
htm
;
}
# 定義錯誤提示頁面
error
_page
500
502
503
504
/
50x.html
;
location
=
/
50x.html
{
}
#靜態文件,nginx本身處理
location
~
^
/
(
images
|
javascript
|
js
|
css
|
flash
|
media
|
static
)
/
{
#過時30天,靜態文件不怎麼更新,過時能夠設大一點,
#若是頻繁更新,則能夠設置得小一點。
expires
30d
;
}
#PHP 腳本請求所有轉發到 FastCGI處理. 使用FastCGI默認配置.
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
;
}
#禁止訪問 .htxxx 文件
location
~
/
.
ht
{
deny
all
;
}
}
}