[javascript] view plaincopy
#用戶 用戶組
user www www;
#工作進程,根據硬件調整,有人說幾核cpu,就配幾個,我覺得可以多一點
worker_processes 5;
#錯誤日誌
error_log logs/error.log;
#pid文件位置
pid logs/nginx.pid;
worker_rlimit_nofile 8192;
events {
#工作進程的最大連接數量,根據硬件調整,和前面工作進程配合起來用,儘量大,但是別把cpu跑到100%就行
worker_connections 4096;
}
http {
include conf/mime.types;
#反向代理配置,可以打開proxy.conf看看
include /etc/nginx/proxy.conf;
#fastcgi配置,可以打開fastcgi.conf看看
include /etc/nginx/fastcgi.conf;
default_type application/octet-stream;
#日誌的格式
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#訪問日誌
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
#根據實際情況調整,如果server很多,就調大一點
server_names_hash_bucket_size 128; # this seems to be required for some vhosts
#這個例子是fastcgi的例子,如果用fastcgi就要仔細看
server { # php/fastcgi
listen 80;
#域名,可以有多個
server_name domain1.com www.domain1.com;
#訪問日誌,和上面的級別不一樣,應該是下級的覆蓋上級的
access_log logs/domain1.access.log main;
root html;
location / {
index index.html index.htm index.php;
}
#所有php後綴的,都通過fastcgi發送到1025端口上
#上面include的fastcgi.conf在此應該是有作用,如果你不include,那麼就把fastcgi.conf的配置項放在這個下面。
location ~ .php$ {
fastcgi_pass 127.0.0.1:1025;
}
}
#這個是反向代理的例子
server { # simple reverse-proxy
listen 80;
server_name domain2.com www.domain2.com;
access_log logs/domain2.access.log main;
#靜態文件,nginx自己處理
location ~ ^/(p_w_picpaths|javascript|js|css|flash|media|static)/ {
root /var/www/virtual/big.server.com/htdocs;
#過期30天,靜態文件不怎麼更新,過期可以設大一點,如果頻繁更新,則可以設置得小一點。
expires 30d;
}
#把請求轉發給後臺web服務器,反向代理和fastcgi的區別是,反向代理後面是web服務器,fastcgi後臺是fasstcgi監聽進程,當然,協議也不一樣。
location / {
proxy_pass http://127.0.0.1:8080;
}
}
#upstream的負載均衡,weight是權重,可以根據機器配置定義權重。據說nginx可以根據後臺響應時間調整。後臺需要多個web服務器。
upstream big_server_com {
server 127.0.0.3:8000 weight=5;
server 127.0.0.3:8001 weight=5;
server 192.168.0.1:8000;
server 192.168.0.1:8001;
}
server {
listen 80;
server_name big.server.com;
access_log logs/big.server.access.log main;
location / {
proxy_pass http://big_server_com;
}
}
}
user www www; #運行NGINX所使用的用戶和組
worker_processes 4; #nginx進程數,建議按照cpu數目來指定,一般爲它的倍數,每個進程消耗約10M內存
error_log /data/logs/nginx/error.log crit;
pid /elain/apps/nginx/nginx.pid;
worker_rlimit_nofile 65535; #nginx能打開文件的最大句柄數,最好與ulimit -n的值保持一致,使用ulimit -SHn 65535 設置
events {
use epoll; #使用epoll的I/O模型
connections 20000; #每個進程允許的最多連接數
worker_connections 65535; #該值受系統進程最大打開文件數限制,需要使用命令ulimit -n 查看當前設置
maxclients=65535*2
}
http {
include mime.types; #mine.types內定義各文件類型映像
types {
text/html html;
p_w_picpath/gif gif;
p_w_picpath/jpeg jpg;
p_w_picpath/png png;
}
default_type application/octet-stream; #設置默認類型是二進制流,若未設置時,比如未加載PHP時,是不予解析,用瀏覽器訪問則出現下載窗口
server_names_hash_bucket_size 128; #不能帶單位!配置個主機時必須設置該值,否則無法運行Nginx或測試時不通過,該設置與server_names_hash_max_size 共同控制保存服務器名的HASH表,hash bucket size總是等於hash表的大小,並且是一路處理器緩存大小的倍數。若hash bucket size等於一路處理器緩存的大小,那麼在查找鍵的時候,最壞的情況下在內存中查找的次數爲2。第一次是確定存儲單元的地址,第二次是在存儲單元中查找鍵 值。若報出hash max size 或 hash bucket size的提示,則我們需要增加server_names_hash_max_size的值。
client_header_buffer_size 128k; #客戶端請求頭部的緩衝區大小,根據系統分頁大小設置,分頁大小可用命令getconf PAGESIZE取得
large_client_header_buffers 4 128k; #4爲個數,128k爲大小,默認是4k。申請4個128k。當http 的URI太長或者request header過大時會報414 Request URI too large或400 bad request,這是很有可能是cookie中寫入的值太大造成的,因爲header中的其他參數的size一般比較固定,只有cookie可能被寫入較 大的數據,這時可以調大上述兩個值,相應的瀏覽器中cookie的字節數上限會增大。
client_max_body_size 8m; #HTTP請求的BODY最大限制值,若超出此值,報413 Request Entity Too Large
open_file_cache max=65535 inactive=20s; #max指定緩存數量,建議和打開文件數一致,inactive是指經過多長時間文件沒被請求後刪除緩存。
open_file_cache_valid 30s; #指多長時間檢查一次緩存的有效信息
open_file_cache_min_uses 1; #open_file_cache指令中的inactive參數時間內文件的最少使用次數,如果超過這個數字,文件描述符一直是在緩存中打開的,如上例, 如果有一個文件在inactive時間內一次沒被使用,它將被移除。
server_tokens off; #關閉錯誤時Nginx版本顯示
#提高文件傳輸性能
sendfile on; #打開系統函數sendfile()支持
tcp_nopush on; #打開linux下TCP_CORK,sendfile打開時纔有效,作減少報文段的數量之用
keepalive_timeout 60; #keepalive超時時間
tcp_nodelay on; #打開TCP_NODELAY在包含了keepalive纔有效
fastcgi_connect_timeout 300; #指定連接到後端FastCGI的超時時間
fastcgi_send_timeout 300; #向FastCGI傳送請求的超時時間,這個值是指已經完成兩次握手後向FastCGI傳送請求的超時時間。
fastcgi_read_timeout 300; #接收FastCGI應答的超時時間,這個值是指已經完成兩次握手後接收FastCGI應答的超時時間。
fastcgi_buffer_size 64k; #這裏可以設置爲fastcgi_buffers指令指定的緩衝區大小
fastcgi_buffers 16 16k; #指定本地需要用多少和多大的緩衝區來緩衝FastCGI的應答
fastcgi_busy_buffers_size 128k; #建議爲fastcgi_buffers的兩倍
fastcgi_temp_file_write_size 128k; #在寫入fastcgi_temp_path時將用多大的數據塊,默認值是fastcgi_buffers的兩倍,設置上述數值設置太小時若負載上來時可能報 502 Bad Gateway
fastcgi_cache dingtm #開啓FastCGI緩存並且爲其制定一個名稱,有效降低CPU負載,並且防止502錯誤
fastcgi_cache_valid 200 302 1h; #指定應答代碼緩存時間爲1小時
fastcgi_cache_valid 301 1d; #1天
fastcgi_cache_valid any 1m; #其它爲1分鐘
fastcgi_cache_min_uses 1; #緩存在fastcgi_cache_path指令inactive參數值時間內的最少使用次數 f
gzip on; #打開GZIP壓縮,實時壓縮輸出數據流
gzip_min_length 1k; #從Content-Length中數值獲取驗證,小於1K會越壓越大
gzip_buffers 4 16k; #以16K爲單位4倍的申請內存做壓縮結果流緩存
gzip_http_version 1.1;
gzip_comp_level 3; #壓縮比率1-9,1壓縮比最小處理速度最快,9壓縮比最大但處理最慢且耗CPU
gzip_types text/plain application/x-javascript text/css application/xml; #壓縮類型
include vhosts/*.conf; #虛擬主機
}
#虛擬主機
server {
listen 80;
server_name <a href="http://www.elain.org/">www.elain.org</a>; #多域名用空格隔開
index index.php index.html index.shtml;
root /elain/data/htdocs/elain;
#limit_conn connlimit 20; #限制一個IP只能最多隻能發起20個連接,超過報 503 Service unavailable,可防止惡意連接
access_log /elain/logs/nginx/access_www.elain.org.log access;
error_log /elain/logs/nginx/error_www.elain.org.log;
location / {
ssi on; #WEB文檔根目錄打開SSI支持
ssi_types text/html;
ssi_silent_errors off; #處理SSI出錯時不提示
}
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {
access_log off;
expires 30d;
}
location ~ .*.(js|css)?$ {
expires 1h;
add_header Cache_Control private;
}
location ~ /.ht {
deny all;
}
location /NginxStatus { #設定查看Nginx狀態的地址
stub_status on;
access_log off;
auth_basic “NginxStatus”; #標識
auth_basic_user_file conf/.htpasswd; #網頁加密,提示登錄框,輸入用戶名和密碼可查看
}
location ~ .*.(php|php5)?$ { #匹配文件後綴php, php5
#fastcgi_pass unix:/tmp/php-cgi.sock; #SOCKET方式轉交fastcgi處理
fastcgi_pass 127.0.0.1:9000; #9000端口方式fastcgi
fastcgi_index index.php;
include fastcgi_params; #包含fastcgi配置
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}