Nginx配置文件nginx.conf詳解


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM