基本的 (優化過的)配置javascript
咱們將修改的惟一文件是nginx.conf,其中包含Nginx不一樣模塊的全部設置。你應該可以在服務器的/etc/nginx目錄中找到nginx.conf。首先,咱們將談論一些全局設置,而後按文件中的模塊挨個來,談一下哪些設置可以讓你在大量客戶端訪問時擁有良好的性能,爲何它們會提升性能。css
高層的配置html
nginx.conf文件中,Nginx中有少數的幾個高級配置在模塊部分之上。java
- user www-data;
- pid /var/run/nginx.pid;
- worker_processes auto;
- worker_rlimit_nofile 100000;
user和pid應該按默認設置 – 咱們不會更改這些內容,由於更改與否沒有什麼不一樣。node
worker_processes 定義了nginx對外提供web服務時的worder進程數。最優值取決於許多因素,包括(但不限於)CPU核的數量、存儲數據的硬盤數量及負載模式。不能肯定的時候,將其設置爲可用的CPU內核數將是一個好的開始(設置爲「auto」將嘗試自動檢測它)。nginx
worker_rlimit_nofile 更改worker進程的最大打開文件數限制。若是沒設置的話,這個值爲操做系統的限制。設置後你的操做系統和Nginx能夠處理比「ulimit -a」更多的文件,因此把這個值設高,這樣nginx就不會有「too many open files」問題了。web
Events模塊json
events模塊中包含nginx中全部處理鏈接的設置。緩存
- events {
- worker_connections 2048;
- multi_accept on;
- use epoll;
- }
worker_connections設置可由一個worker進程同時打開的最大鏈接數。若是設置了上面提到的worker_rlimit_nofile,咱們能夠將這個值設得很高。安全
記住,最大客戶數也由系統的可用socket鏈接數限制(~ 64K),因此設置不切實際的高沒什麼好處。
multi_accept 告訴nginx收到一個新鏈接通知後接受盡量多的鏈接。
use 設置用於複用客戶端線程的輪詢方法。若是你使用Linux 2.6+,你應該使用epoll。若是你使用*BSD,你應該使用kqueue。想知道更多有關事件輪詢?看下維基百科吧(注意,想了解一切的話可能須要neckbeard和操做系統的課程基礎)
(值得注意的是若是你不知道Nginx該使用哪一種輪詢方法的話,它會選擇一個最適合你操做系統的)。
HTTP 模塊
HTTP模塊控制着nginx http處理的全部核心特性。由於這裏只有不多的配置,因此咱們只節選配置的一小部分。全部這些設置都應該在http模塊中,甚至你不會特別的注意到這段設置。
- http {
- server_tokens off;
- sendfile on;
- tcp_nopush on;
- tcp_nodelay on;
- }
server_tokens 並不會讓nginx執行的速度更快,但它能夠關閉在錯誤頁面中的nginx版本數字,這樣對於安全性是有好處的。
sendfile可讓sendfile()發揮做用。sendfile()能夠在磁盤和TCP socket之間互相拷貝數據(或任意兩個文件描述符)。Pre-sendfile是傳送數據以前在用戶空間申請數據緩衝區。以後用read()將數據從文件拷貝到這個緩衝區,write()將緩衝區數據寫入網絡。sendfile()是當即將數據從磁盤讀到OS緩存。由於這種拷貝是在內核完成的,sendfile()要比組合read()和write()以及打開關閉丟棄緩衝更加有效(更多有關於sendfile)
tcp_nopush 告訴nginx在一個數據包裏發送全部頭文件,而不一個接一個的發送
tcp_nodelay 告訴nginx不要緩存數據,而是一段一段的發送–當須要及時發送數據時,就應該給應用設置這個屬性,這樣發送一小塊數據信息時就不能當即獲得返回值。
- access_log off;
- error_log /var/log/nginx/error.log crit;
access_log設置nginx是否將存儲訪問日誌。關閉這個選項可讓讀取磁盤IO操做更快(aka,YOLO)。
error_log 告訴nginx只能記錄嚴重的錯誤。
- keepalive_timeout 10;
- client_header_timeout 10;
- client_body_timeout 10;
- reset_timedout_connection on;
- send_timeout 10;
keepalive_timeout 給客戶端分配keep-alive連接超時時間。服務器將在這個超時時間事後關閉連接。咱們將它設置低些可讓ngnix持續工做的時間更長。
client_header_timeout 和client_body_timeout 設置請求頭和請求體(各自)的超時時間。咱們也能夠把這個設置低些。
reset_timeout_connection告訴nginx關閉不響應的客戶端鏈接。這將會釋放那個客戶端所佔有的內存空間。
send_timeout 指定客戶端的響應超時時間。這個設置不會用於整個轉發器,而是在兩次客戶端讀取操做之間。若是在這段時間內,客戶端沒有讀取任何數據,nginx就會關閉鏈接。
- limit_conn_zone $binary_remote_addr zone=addr:5m;
- limit_conn addr 100;
limit_conn爲給定的key設置最大鏈接數。這裏key是addr,咱們設置的值是100,也就是說咱們容許每個IP地址最多同時打開有100個鏈接。
limit_conn_zone設置用於保存各類key(好比當前鏈接數)的共享內存的參數。5m就是5兆字節,這個值應該被設置的足夠大以存儲(32K*5)32byte狀態或者(16K*5)64byte狀態。
- include /etc/nginx/mime.types;
- default_type text/html;
- charset UTF-8;
include只是一個在當前文件中包含另外一個文件內容的指令。這裏咱們使用它來加載稍後會用到的一系列的MIME類型。
default_type設置文件使用的默認的MIME-type。
charset設置咱們的頭文件中的默認的字符集。
如下兩點對於性能的提高在偉大的WebMasters StackExchange中有解釋。
- gzip_disable "msie6";
# gzip_static on;
gzip_proxied any;
gzip_min_length 1000;
gzip_comp_level 4;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip是告訴nginx採用gzip壓縮的形式發送數據。這將會減小咱們發送的數據量。
gzip_disable爲指定的客戶端禁用gzip功能。咱們設置成IE6或者更低版本以使咱們的方案可以普遍兼容。
gzip_static告訴nginx在壓縮資源以前,先查找是否有預先gzip處理過的資源。這要求你預先壓縮你的文件(在這個例子中被註釋掉了),從而容許你使用最高壓縮比,這樣nginx就不用再壓縮這些文件了(想要更詳盡的gzip_static的信息,請點擊這裏)。
gzip_proxied容許或者禁止壓縮基於請求和響應的響應流。咱們設置爲any,意味着將會壓縮全部的請求。
gzip_min_length設置對數據啓用壓縮的最少字節數。若是一個請求小於1000字節,咱們最好不要壓縮它,由於壓縮這些小的數據會下降處理此請求的全部進程的速度。
gzip_comp_level設置數據的壓縮等級。這個等級能夠是1-9之間的任意數值,9是最慢可是壓縮比最大的。咱們設置爲4,這是一個比較折中的設置。
gzip_type設置須要壓縮的數據格式。上面例子中已經有一些了,你也能夠再添加更多的格式。
- # cache informations about file descriptors, frequently accessed files
# can boost performance, but you need to test those values
open_file_cache max=100000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
##
# Virtual Host Configs
# aka our settings for specific servers
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
open_file_cache打開緩存的同時也指定了緩存最大數目,以及緩存的時間。咱們能夠設置一個相對高的最大時間,這樣咱們能夠在它們不活動超過20秒後清除掉。
open_file_cache_valid 在open_file_cache中指定檢測正確信息的間隔時間。
open_file_cache_min_uses 定義了open_file_cache中指令參數不活動時間期間裏最小的文件數。
open_file_cache_errors指定了當搜索一個文件時是否緩存錯誤信息,也包括再次給配置中添加文件。咱們也包括了服務器模塊,這些是在不一樣文件中定義的。若是你的服務器模塊不在這些位置,你就得修改這一行來指定正確的位置。
一個完整的配置
- user www-data;
pid /var/run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 100000;
events {
worker_connections 2048;
multi_accept on;
use epoll;
}
http {
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
access_log off;
error_log /var/log/nginx/error.log crit;
keepalive_timeout 10;
client_header_timeout 10;
client_body_timeout 10;
reset_timedout_connection on;
send_timeout 10;
limit_conn_zone $binary_remote_addr zone=addr:5m;
limit_conn addr 100;
include /etc/nginx/mime.types;
default_type text/html;
charset UTF-8;
gzip on;
gzip_disable "msie6";
gzip_proxied any;
gzip_min_length 1000;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
open_file_cache max=100000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
實際項目中的配置
ngnx.conf
user www www; worker_processes auto; error_log /data/wwwlogs/error_nginx.log crit; pid /var/run/nginx.pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; multi_accept on; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 1024m; client_body_buffer_size 10m; sendfile on; tcp_nopush on; keepalive_timeout 120; server_tokens off; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; fastcgi_intercept_errors on; #Gzip Compression gzip on; gzip_buffers 16 8k; gzip_comp_level 6; gzip_http_version 1.1; gzip_min_length 256; gzip_proxied any; gzip_vary on; gzip_types text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml text/javascript application/javascript application/x-javascript text/x-json application/json application/x-web-app-manifest+json text/css text/plain text/x-component font/opentype application/x-font-ttf application/vnd.ms-fontobject image/x-icon; gzip_disable "MSIE [1-6]\.(?!.*SV1)"; #If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency. open_file_cache max=1000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; ######################## default ############################ server { listen 80; server_name _; access_log /data/wwwlogs/access_nginx.log combined; root /data/wwwroot/default/Blog; index index.html index.htm index.jsp; location /nginx_status { stub_status on; access_log off; allow 127.0.0.1; deny all; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ { expires 30d; access_log off; } location ~ .*\.(js|css)?$ { expires 7d; access_log off; } location ~ { proxy_pass http://127.0.0.1:8080; include proxy.conf; } location ~ /\.ht { deny all; } } ########################## vhost ############################# include vhost/*.conf; }
proxy.conf
proxy_connect_timeout 300s; proxy_send_timeout 900; proxy_read_timeout 900; proxy_buffer_size 32k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_redirect off; proxy_hide_header Vary; proxy_set_header Accept-Encoding ''; proxy_set_header Referer $http_referer; proxy_set_header Cookie $http_cookie; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
編輯完配置後,確認重啓nginx使設置生效。
- sudo service nginx restart