1 #定義Nginx運行的用戶 2 #user nobody; 3 4 #nginx啓動進程數,建議設置爲等於CPU總核心數。 5 worker_processes 1; 6 7 #全局錯誤日誌定義類型,[ debug | info | notice | warn | error | crit ] 8 #error_log logs/error.log; 9 #error_log logs/error.log notice; 10 #error_log logs/error.log info; 11 12 #進程文件 13 #pid logs/nginx.pid; 14 15 #工做模式與鏈接數上限 16 events { 17 #use epoll;#參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本內核中的高性能網絡I/O模型,若是跑在FreeBSD上面,就用kqueue模型。 18 worker_connections 1024;#單個進程最大鏈接數(最大鏈接數=鏈接數*進程數) 19 } 20 21 #設定http服務器,利用它的反向代理功能提供負載均衡支持 22 http { 23 include mime.types;#文件擴展名與文件類型映射表,由mime.type文件定義 24 default_type application/octet-stream;#默認文件類型 25 #charset utf-8; #默認編碼 26 #server_names_hash_bucket_size 128; #服務器名字的hash表大小 27 28 #client_header_buffer_size 32k; #設定請求緩衝 29 #large_client_header_buffers 4 64k; #設定請求緩衝 30 31 #client_max_body_size 8m; #設定請求緩 32 sendfile on; #開啓高效文件傳輸模式,sendfile指令指定nginx是否調用sendfile函數來輸出文件,對於普通應用設爲 on,若是用來進行下載等應用磁盤IO重負載應用,可設置爲off,以平衡磁盤與網絡I/O處理速度,下降系統的負載。注意:若是圖片顯示不正常把這個改爲off。 33 #autoindex on; #開啓目錄列表訪問,合適下載服務器,默認關閉。 34 #tcp_nopush on; #防止網絡阻塞 35 #tcp_nodelay on; #防止網絡阻塞 36 keepalive_timeout 65; #長鏈接超時時間,單位是秒 37 38 #FastCGI相關參數是爲了改善網站的性能:減小資源佔用,提升訪問速度。下面參數看字面意思都能理解。 39 #fastcgi_connect_timeout 300; 40 #fastcgi_send_timeout 300; 41 #fastcgi_read_timeout 300; 42 #fastcgi_buffer_size 64k; 43 #fastcgi_buffers 4 64k; 44 #fastcgi_busy_buffers_size 128k; 45 #fastcgi_temp_file_write_size 128k; 46 47 #gzip模塊設置 48 #gzip on; #開啓gzip壓縮輸出 49 #gzip_min_length 1k; #最小壓縮文件大小 50 #gzip_buffers 4 16k; #壓縮緩衝區 51 #gzip_http_version 1.0; #壓縮版本(默認1.1,前端若是是squid2.5請使用1.0) 52 #gzip_comp_level 2; #壓縮等級 53 #gzip_types text/plain application/x-javascript text/css application/xml; 54 #壓縮類型,默認就已經包含textml,因此下面就不用再寫了,寫上去也不會有問題,可是會有一個warn。 55 #gzip_vary on; 56 #limit_zone crawler $binary_remote_addr 10m; #開啓限制IP鏈接數的時候須要使用 57 58 59 #日誌格式設定 60 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 61 # '$status $body_bytes_sent "$http_referer" ' 62 # '"$http_user_agent" "$http_x_forwarded_for"'; 63 64 #定義本虛擬主機的訪問日誌 65 #access_log logs/access.log main; 66 67 #設定負載均衡的服務器列表 68 upstream blog.ha97.com { 69 #upstream的負載均衡,weight是權重,能夠根據機器配置定義權重。weigth參數表示權值,權值越高被分配到的概率越大。 70 #server 192.168.80.121:80 weight=3; 71 #server 192.168.80.122:80 weight=2; 72 #server 192.168.80.123:80 weight=3; 73 } 74 75 #虛擬主機的配置 76 server { 77 listen 80;#監聽端口 78 server_name www.ha97.com ha97.com;#域名能夠有多個,用空格隔開 79 80 #默認請求,匹配任何查詢,由於全部請求都以 / 開頭 81 location / { 82 root html;#定義服務器的默認網站根目錄位置 83 index index.html index.htm;#定義首頁索引文件的名稱 84 } 85 86 # 定義錯誤提示頁面 87 error_page 500 502 503 504 /50x.html; 88 location = /50x.html { 89 root html; 90 } 91 92 #本地動靜分離反向代理配置 93 #全部jsp的頁面均交由tomcat或resin處理 94 location ~ .(jsp|jspx|do)?$ { 95 proxy_set_header Host $host; 96 proxy_set_header X-Real-IP $remote_addr; 97 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 98 proxy_pass http://127.0.0.1:8080; 99 } 100 101 #全部靜態文件由nginx直接讀取不通過tomcat或resin 102 location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { 103 expires 15d; #過時15天,靜態文件不怎麼更新,過時能夠設大一點,若是頻繁更新,則能夠設置得小一點。 104 } 105 #JS和CSS緩存時間設置 106 location ~ .*.(js|css)?$ { 107 expires 1h; 108 } 109 #圖片緩存時間設置 110 location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ { 111 expires 10d; 112 } 113 114 #PHP 腳本請求所有轉發到 FastCGI處理. 使用FastCGI默認配置. 115 location ~ \.php$ { 116 root /root; 117 fastcgi_pass 127.0.0.1:9000; 118 fastcgi_index index.php; 119 fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name; 120 include fastcgi_params; 121 } 122 123 #設定查看Nginx狀態的地址 124 location /NginxStatus { 125 stub_status on; 126 access_log on; 127 auth_basic "NginxStatus"; 128 auth_basic_user_file confpasswd; 129 #htpasswd文件的內容能夠用apache提供的htpasswd工具來產生。 130 } 131 132 #禁止訪問 .htxxx 文件 133 location ~ /\.ht { 134 deny all; 135 } 136 137 #對 "/" 啓用反向代理 138 location / { 139 proxy_pass http://127.0.0.1:88; 140 proxy_redirect off; 141 proxy_set_header X-Real-IP $remote_addr; 142 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#後端的Web服務器能夠經過X-Forwarded-For獲取用戶真實IP 143 #如下是一些反向代理的配置,可選。 144 proxy_set_header Host $host; 145 client_max_body_size 10m; #容許客戶端請求的最大單文件字節數 146 client_body_buffer_size 128k; #緩衝區代理緩衝用戶端請求的最大字節數, 147 proxy_connect_timeout 90; #nginx跟後端服務器鏈接超時時間(代理鏈接超時) 148 proxy_send_timeout 90; #後端服務器數據回傳時間(代理髮送超時) 149 proxy_read_timeout 90; #鏈接成功後,後端服務器響應時間(代理接收超時) 150 proxy_buffer_size 4k; #設置代理服務器(nginx)保存用戶頭信息的緩衝區大小 151 proxy_buffers 4 32k; #proxy_buffers緩衝區,網頁平均在32k如下的設置 152 proxy_busy_buffers_size 64k; #高負荷下緩衝大小(proxy_buffers*2) 153 proxy_temp_file_write_size 64k; #設定緩存文件夾大小,大於這個值,將從upstream服務器傳 154 } 155 156 } 157 }