nginx.conf配置詳解析
#定義nginx運行用戶和用戶組
user nginx;
#nginx進程數,建議等於CPU核心數或者 auto
worker_processes auto;
#全局錯誤日誌定義類型:[ debug | info | notice | warn | error | crit] 建議調成 error 這樣只記錄錯誤日誌不佔用太多內存
error_log /var/log/nginx/error.log warn;
#進程文件
pid /var/run/nginx.pid;
#一個nginx進程打開的最多文件描述符數目,理論上應該是最多打開文件數(ulimit -n系統值)與nginx進程數目保持一致
worker_rlimit_nofile 665535;
#參考事件模型,use [ kuqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll 模型是linux2.6以上;如果跑在FreeBSD上就使用 kqueue
#單線程最大連接數(最大連接數=連接數*線程數)
events {
use epoll;
worker_connections 665535;
}
http {
include /etc/nginx/mime.types; #文件擴展名與文件類型映射表
default_type application/octet-stream; #默認文件類型
#charset utf-8; #默認編碼
##日誌格式設定(可放到server虛擬主機下局部設置)
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 /var/log/nginx/access.log access; #使用 log_format 指令定義一個 main 的格式
}
##Nginx配置跨域請求 Access-Control-Allow-Origin(可放到server虛擬主機下局部設置)
add_header Access-Control-Allow-Origin *; #(Origin),即接受所有跨域的請求
add_header Access-Control-Allow-Headers X-Requested-Wit,content-typeh; #相應頭類型
add_header Access-Control-Allow-Methods GET,POST,OPTIONS; #相應類型
sendfile on; #開啓高效文件傳輸模式,sendfile指定nginx是否調用sendfile函數來輸出文件,對於普通應用設爲 on,如果用來進行下載等應用磁盤IO重負載應用,可設置爲off,以平衡磁盤與網絡I/O處理速度,降低系統的負載。注意:如果圖片顯示不正常把這個改成off
tcp_nopush on; #防止網絡阻塞 連續發送包只有sendfile開啓了這個配置纔有用
tcp_nodelay on; #防止網絡阻塞
server_tokens off; #off 關閉隱藏版本信息
keepalive_timeout 65; #長連接超時時間,單位是秒
types_hash_max_size 2048; #爲了快速尋找到相應MIME type,Nginx使用散列表來存儲MIME type與文件擴展名
server_names_hash_bucket_size 256; #服務器名字的hash表大小
client_header_buffer_size 256k; #客戶請求頭 緩衝大小
large_client_header_buffers 4 256k; #設定請求緩
client_max_body_size 50m; #設定請求體大小
#autoindex off; #開啓目錄列表訪問,合適下載服務器,默認關閉
##FastCGI相關參數是爲了改善網站的性能:減少資源佔用,提高訪問速度
fastcgi_connect_timeout 300; #指定連接到後端FastCGI的超時時間
fastcgi_send_timeout 300; #指定連接到後端FastCGI的超時時間
fastcgi_read_timeout 300; #指定接收FastCGI應答的超時時間
fastcgi_buffer_size 256k; #指定緩衝區大小
fastcgi_buffers 4 256k; #指定本地需要用多少和多大的緩衝區來緩衝FastCGI的應答請求(以把這個值設置爲“16 16k”、“4 64k”等)
fastcgi_busy_buffers_size 256k; #默認值是fastcgi_buffers的兩倍
fastcgi_temp_file_write_size 256k; #表示在寫入緩存文件時使用多大的數據塊,默認值是fastcgi_buffers的兩倍
##gzip模塊設置
gzip on; #開啓gzip壓縮輸出
gzip_min_length 1k; #最小壓縮文件大小
gzip_buffers 4 16k; #壓縮緩衝區
gzip_http_version 1.0; #壓縮版本(默認1.1,前端如果是squid2.5請使用1.0)
gzip_comp_level 6; #壓縮等級
gzip_types text/plain application/x-javascript text/css application/xml image/jpeg image/gif image/png image/webp application/javascript; #壓縮類型,默認就已經包含text/html,所以下面就不用再寫了,寫上去也不會有問題,但是會有一個warn
gzip_vary on;
#access_log off;
#limit_zone crawler $binary_remote_addr 10m; #開啓限制IP連接數的時候需要使用
##open_file_cache 提高靜態緩存效率
open_file_cache max=655350 inactive=600s; #設置緩存中的最大元素數;定義一個時間,如果在此期間未訪問該元素,則從該緩存中刪除該元素,時間60s
open_file_cache_min_uses 1; #最小訪問次數設置爲1次或更多次
open_file_cache_valid 80s; #定義時間段(以秒爲單位),之後將重新驗證open_file_cache中的元素
open_file_cache_errors on; #啓用錯誤緩存
##upstream負載均衡模塊配置
upstream blog.ha97.com {
server 192.168.10.121:80 weight=3; #指定負載的IP和端口,設定權重
server 192.168.10.12:80 weight=2;
server 192.168.10.13:80 weight=3 down; #將當前主機標記爲不可用(維護時使用)
}
include /etc/nginx/conf.d/*.conf;
}
nginx.conf配置文件(微調參數後可直接使用)
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
worker_rlimit_nofile 665535;
events {
use epoll;
worker_connections 665535;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log access;
}
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With,content-type;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
keepalive_timeout 65;
types_hash_max_size 2048;
server_names_hash_bucket_size 256;
client_header_buffer_size 256k;
large_client_header_buffers 4 256k;
client_max_body_size 50m;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 256k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css application/xml image/jpeg image/gif image/png image/webp application/javascript;
gzip_vary on;
#access_log off;
open_file_cache max=655350 inactive=600s;
open_file_cache_min_uses 1;
open_file_cache_valid 80s;
open_file_cache_errors on;
upstream blog.ha97.com {
server 192.168.10.11:80 weight=3;
server 192.168.10.12:80 weight=2;
server 192.168.10.13:80 weight=3 down;
}
include /etc/nginx/conf.d/*.conf;
}