1. 程式人生 > 實用技巧 >Nginx簡單配置

Nginx簡單配置

user  www;    #工作程序使用者,預設是nobody
worker_processes  1;    #工作程序數量,一般等於cpu核數

#error_log  logs/error.log;    #定義錯誤日誌路徑,後面可以跟錯誤級別
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;    #nginx程序執行檔案地址


events {
    accept_mutex on;    #防止程序驚群效應,預設開啟
    use epoll;    #
I/O多路複用,事件驅動模型 worker_connections 1024; #單個程序最大連線數 } http { 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 on; #開啟高效檔案傳輸模式,如果提供下載服務建議設定為off #tcp_nopush on; #設定資料包中傳送所有標頭檔案,而不是一個一個傳送,防止網路阻塞 #keepalive_timeout 0; keepalive_timeout 65; #客戶端連線保持時間,超過這個時間斷開連線
keepalive_requests 10240; #HTTP長連線最多處理完成的最大連線數,達到最大請求書好歐關閉連線 tcp_nodelay on; #防止網路阻塞 client_header_buffer_size 4k; #客戶端請求頭部的緩衝區大小 open_file_cache max=102400 inactive=20s; #開啟檔案指定快取,max指定快取數量,inactive指定檔案未請求多久刪除快取 open_file_cache_valid 30s; #多久檢查快取有效資訊 open_file_cache_min_uses 1; #指定open_file_cache中inactive引數時間內檔案最少使用次數 client_header_timeout 15; #設定請求頭的超時時間 client_body_timeout 15; #設定請求內容的超時時間 reset_timedout_connection on; #關閉不響應的客戶端連線 send_timeout 15; #響應客戶端超時時間 server_tokens off; #關閉錯誤頁面中的nginx版本數字 client_max_body_size 10m; #上傳檔案大小限制 #fastcgi fastcgi_connect_timeout 600; #連線後端FastCGI超時時間 fastcgi_send_timeout 600; #向FastCGI傳送請求的超時時間 fastcgi_read_timeout 600; #接收FastCGI應答的超時時間 fastcgi_buffer_size 64K; #讀取FastCGI應答第一部分緩衝區大小 fastcgi_buffers 4 64k; #本地需要多少和多大的緩衝區緩衝FastCGI的應答請求 fastcgi_busy_buffers_size 128k; #系統繁忙時的buffer,一般是fastcgi_buffers的兩倍 fastcgi_temp_file_write_size 128k; #在寫入fastcgi_temp_path時資料塊的大小,預設是fastcgi_buffers的兩倍 fastcgi_temp_path /usr/local/nginx/nginx_tmp; #FastCGI伺服器臨時接收目錄 fastcgi_intercept_errors on; #是否允許nginx使用error_page處理錯誤資訊 gzip on; #開啟gzip壓縮功能 gzip_min_length 1k; #允許壓縮的頁面最小位元組數 gzip_buffers 4 32k; #壓縮緩衝區大小,表示申請4個單位為32K的記憶體作為壓縮結果流快取 gzip_http_version 1.1; #壓縮版本 gzip_comp_level 6; #壓縮比例 gzip_types text/css text/xml application/javascript; #指定壓縮型別 gzip_vary on; #讓前端的快取伺服器快取經過GZIP壓縮頁面 #虛擬主機配置 server { listen 80; #監聽埠 server_name www.ng.com ng.com; #虛擬主機域名 charset utf-8; #預設編碼 #access_log logs/host.access.log main; #請求日誌路徑及格式 # location / { root html; #網站根目錄 index index.php index.html index.htm; #首頁手機檔名稱 } #error_page 404 /404.html; #404錯誤頁面地址 # redirect server error pages to the static page /50x.html #錯誤頁面重定向 # error_page 500 502 503 504 /50x.html; #500 502 503 504錯誤頁面 location = /50x.html { root html; #59x.html所在目錄 } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # 將php請求轉發到fastcgi處理 location ~ \.php$ { root /usr/local/nginx/html; #根目錄 fastcgi_pass 127.0.0.1:9000; #將請求轉發給哪個伺服器的哪個埠 fastcgi_index index.php; #指定預設首頁檔案 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #指令碼檔案請求路徑 include fastcgi_params; #載入更多fastcgi_params對映 } #圖片等檔案快取時間 location ~* \.(ico|jpe?g|gif|png|bmp|swf|flv)$ { expires 30d; access_log off; } #js css檔案快取時間 location ~* \.(js|css)$ { expires 7d; log_not_found off; access_log off; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # 精緻訪問.ht開標頭檔案 #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }