標籤:二進位流 故障 pps 消耗cpu 虛擬 bucket 後端服務 action 錯誤記錄檔
nginx配置如下:
#指定nginx進程運行使用者以及使用者組
user www www;
#nginx要開啟的進程數為8
worker_processes 8;
#全域錯誤記錄檔檔案
#debug輸出日誌最為詳細,而crit輸出日誌最少/var/log目錄是linux下的日誌存放目錄
error_log /var/log/nginx/nginx_error.log crit;
#指定進程id的儲存位置
pid /var/run/nginx.pid;
#綁定worker進程和CPU,linux核心2.4以上可用
worker_rlimit_nofile 51200;
#nginx的工作模式及串連輸上線
events {
#nginx工作模式,epoll是linux平台下的高效模式
use epoll;
#定義nginx每個進程的最大串連數為51200,一般網上都配置65535,根據張宴大神的建議51200即可
worker_connections 51200;
}
http {
#實現對設定檔所包含的檔案的設定
include mime.types;
#設定預設類型為二進位流
default_type application/octet-stream;
server_names_hash_bucket_size 128;
#指定來自用戶端要求標頭的headerbuffer大小,設定為32KB
client_header_buffer_size 32k;
#指定用戶端請求中較大的訊息頭的緩衝最大數量和大小,這裡是4個32KB
large_client_header_buffers 4 32k;
#上傳檔案大小
client_max_body_size 356m;
#nginx的HttpLog模組指定,指定nginx日誌的輸出格式,輸出格式為access
log_format access ‘$remote_addr - $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" "$http_x_forwarded_for"‘;
#access日誌存在未知
access_log /var/log/nginx/access.log access;
#開啟高效模式檔案傳輸模式,將tcp_nopush和tcp_nodely兩個指另設定為on,用於防止網路阻塞。
sendfile on;
tcp_nopush on;
tcp_nodely on;
#設定用戶端串連保持活動的逾時時間
keepalive_timeout 65;
server_tokens off;
#用戶端請求主體讀取緩衝
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_send_timeout 60;
proxy_read_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
#fastcgi_connect_timeout 300;
#fastcgi_send_timeout 300;
#fastcgi_read_timeout 300;
#fastcgi_buffer_timeout 300;
#fastcgi_buffers 4 64k;
#fastcgi_busy_buffers_size 128k;
#fastcgi_temp_file_write_size 128k;
#開啟gzip
gzip on;
#允許壓縮的最小位元組數
gzip_min_length 1k;
#4個單位為16k的記憶體作為壓縮結果流緩衝
gzip_buffers 4 16k;
#設定識別HTTP協議版本,預設是1.1
gzip_http_version 1.1;
#gzip壓縮比,可在1~9中設定,1壓縮比最小,速度最快,9壓縮比最大,速度最慢,消耗CPU
gzip_comp_level 2;
#壓縮的類型
gzip_types text/plain application/x-javascript text/css application/xml;
#讓前端的快取服務器混村經過的gzip壓縮的頁面
gzip_vary on;
#負載平衡
upstream localhost.com {
#每個請求按照ip的hash結果分配,同一個ip的訪客固定訪問一個後端伺服器,可解決動態網頁session共用問題。
ip_hash;
server 127.0.0.1:8080;
server 127.0.0.1:8082;
}
禁止通過ip訪問
server {
server_name _;
return 404;
}
server {
listen 88;
server_name www.test.com;# 對應你的網域名稱
charset utf-8; #設定編碼為utf-8
index index.html index.htm index.jsp index.do index.action;
#指定虛擬機器主機根目錄為/var/www/
root /var/www/;
if ( -d $request_filename)
{
rewrite ^/(.*)([^/])$ http://$host/$1$2/permanent;
}
#方法1:將jsp等動態檔案交給的localhost.com處理,此方法用的不多
location ~ \.(jsp|jspx|do|action)(\/.*)?$ {
index index.jsp;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost.com;
}
#方法2:將所有請求交給localhost.com處理,靜態檔案通過額外定義location交給nginx處理。
location / {
#當502或504時,將請求轉寄到負載平衡中正常server中
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://localhost.com;
proxy_redirect off;
proxy_set_header Host $host;
#若nginx為最前端時,後端獲得X-Real-IP傳遞的ip即為實際ip,若nginx不是最前端時,實際ip為X-Forwarded-For值。
proxy_set_header X-Forwarded-For $remote_addr;
}
#靜態檔案交給nginx處理
location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{
root /var/www;
expires 30d;
}
#靜態檔案交給nginx處理
location ~ .*\.(js|css)?$
{
root /var/www;
expires 1h;
}
location /media {
#指定後端伺服器地址和連接埠
proxy_pass http://localhost.com;
#proxy_next_upstream 容錯移轉待空
proxy_redirect off;
#後端伺服器擷取使用者的主機名稱或真實IP地址
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#用戶端請求主體緩衝區最大值
client_max_body_size 10m;
#用戶端請求主體緩衝區大小
client_body_buffer_size 128k;
#與後端伺服器串連的逾時時間
proxy_connect_timeout 90;
#後端伺服器的資料回傳時間90s,90s內未傳回,nginx將中斷連線
proxy_send_timeout 90;
#nginx從代理的後端伺服器擷取資訊的時間90s
proxy_read_timeout 90;
#緩衝區大小預設等於proxy_buffers設定的大小
proxy_buffer_size 4k;
#設定緩衝區的數量和大小
proxy_buffers 4 32k;
#設定系統很忙時可以使用的proxy_buffers的大小,官方推薦位proxy_buffersX2
proxy_busy_buffers_size 64k;
#指定proxy緩衝臨時檔案的大小
proxy_temp_file_write_size 64k;
}
location /files/ {
#靜止外部存取
internal;
#
alias /home/nfs/media/;
}
}
# HTTPS server配置
server {
listen 443;
server_name www.test.com; # 對應你的網域名稱
root /var/www/webapps;
ssl on;
ssl_certificate /usr/local/nginx/conf/test.crt;
ssl_certificate_key /usr/local/nginx/conf/test_nopass.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
}
nginx配置最佳化+負載平衡+動靜分離詳解