1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
user www www; // 使用的用戶和組 worker_processes 8; // 指定的工作衍生進程數(shù)(一般等于cpu總核數(shù)或總核數(shù)的2倍) error_log logs/nginx_error.log crit; // 指定錯誤日志存放的路徑,錯誤日志記錄級別分別選項為:debug,info,notice,warn,error,crit #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid nginx.pid; // 指定pid文件存放的路徑 #pid logs/nginx.pid; worker_rlimit_nofile 65535; // 一個nginx進程打開的最多文件描述符數(shù)目,理論值是最多打開的文件數(shù)(系統(tǒng)ulimit -n)與nginx進程數(shù)相除,但是nginx分配請求并不均勻,所以在這里建議和ulimit 值保持一致 events { use epoll; // 使用網(wǎng)絡(luò)I/O模型,Linux系統(tǒng)推薦采用epoll模型,FreeBSD系統(tǒng)推薦采用kqueue模型 worker_connections 51200; // 允許的連接數(shù) } # 設(shè)定http服務(wù)器,利用它的反向代理功能提供負載均衡支持 http { include mime.types; // 設(shè)定mime類型,類型由mime.type文件定義 default_type application/octet-stream; charset utf-8; // 設(shè)置使用的字符集,如果一個網(wǎng)站有多種字符集,請不要隨便設(shè)置,應(yīng)該讓程序員在HTML代碼中通過Meta標(biāo)簽設(shè)置 ssi on; // 頁面靜態(tài)化的一個大問題是登陸用戶訪問如果靜態(tài)化,大部分頁面內(nèi)容需要緩存但是用戶登陸的個人信息是動態(tài)的,ssi用來解決頁面部分緩存問題 ssi_silent_errors on; // 默認是off,開啟后在處理SSI文件出錯時不輸出錯誤提示:"[an error occurred while processing the directive]" ssi_types text/shtml; // 默認是ssi_types text/html,所以如果需要htm和html支持,則不需要設(shè)置這句,如果需要shtml支持,則需要設(shè)置 #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指令指定nginx是否調(diào)用sendfile函數(shù)來輸出文件,對于普通應(yīng)用設(shè)為on,如果用來進行下載等應(yīng)用磁盤IO重負載應(yīng)用,可設(shè)置為off,以平衡磁盤與網(wǎng)絡(luò)I/O處理速度,降低系統(tǒng)的負載,如果圖片顯示不正常把這個改成off sendfile on; #tcp_nopush on; // 防止網(wǎng)絡(luò)阻塞 #tcp_nodelay on; # autoindex off; // 開啟目錄列表訪問,合適下載服務(wù)器,默認關(guān)閉 server_names_hash_bucket_size 128; // 服務(wù)器名字的hash表大小 client_header_buffer_size 32k; // 上傳文件大小限制 large_client_header_buffers 4 32k; // 設(shè)定請求緩存 client_max_body_size 300m; // 設(shè)定請求緩存 # (與php-fpm有關(guān)) 優(yōu)化的上傳支持,可以加速對大POST 請求的處理速度,包括文件上傳。優(yōu)化是通過將請求體已寫入一個臨時文件,然后fastcgi 協(xié)議傳遞文件名而不是請求體到來實現(xiàn)的 client_body_in_file_only clean; client_body_temp_path /dev/shm 1 2; # 這個參數(shù)設(shè)置比較大時,使用firefox或ie提交一個小于512K的圖片訪問都會正常,注釋改指令模式大小是操作系統(tǒng)頁面大小的兩倍,8K或16K ,一般提交的圖片大于512K,提交的內(nèi)容會寫入到臨時的文件,不會出現(xiàn)任何問題。當(dāng)取消了目錄訪問權(quán)限(autoindex off;),如果提交的圖片大于512K 都會返回500 Internal Server Error錯誤 client_body_buffer_size 512k; proxy_connect_timeout 5; // 后端服務(wù)器連接的超時時間_發(fā)起握手等候響應(yīng)超時時間(代理連接超時) proxy_read_timeout 60; // 連接成功后_等候后端服務(wù)器響應(yīng)時間_其實已經(jīng)進入后端的排隊之中等候處理(后端服務(wù)器處理請求的時間) proxy_send_timeout 5; // 后端服務(wù)器數(shù)據(jù)回傳時間_就是在規(guī)定時間之內(nèi)后端服務(wù)器必須傳完所有的數(shù)據(jù) proxy_buffer_size 16k; // 該指令設(shè)置緩沖區(qū)大小,從代理后端服務(wù)器取得的第一部分的響應(yīng)內(nèi)容,會放到這里,小的響應(yīng)header通常位于這部分響應(yīng)內(nèi)容里邊.(保存用戶頭信息的緩沖區(qū)大小) proxy_buffers 4 64k; // 該指令設(shè)置緩沖區(qū)的大小和數(shù)量,從被代理的后端服務(wù)器取得的響應(yīng)內(nèi)容,會放置到這里. 默認情況下,一個緩沖區(qū)的大小等于內(nèi)存頁面大小,可能是4K也可能是8K,這取決于平臺 proxy_busy_buffers_size 128k; // 有處在busy狀態(tài)的buffer size加起來不能超過proxy_busy_buffers_size,控制同時傳輸?shù)娇蛻舳说腷uffer數(shù)量的 proxy_temp_file_write_size 128k; // # 臨時文件寫入大小 # nginx和cgi之間的超時時間 fastcgi_connect_timeout 90; fastcgi_send_timeout 90; fastcgi_read_timeout 90; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; # 開啟gzip壓縮 gzip on; gzip_min_length 1k; gzip_buffers 4 16k; # 對http/1.1協(xié)議的請求才會進行壓縮,如果使用了反向代理,那么nginx和后端的upstream server服務(wù)器是使用的1.0協(xié)議通信 gzip_http_version 1.1; gzip_comp_level 9; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; # nginx緩存目錄(在location段落下需要配合proxy_store on 開啟緩存機制 include proxy.conf 處理的詳細規(guī)則 if (!-e $request_filename)){proxy_pass http://192.168.10.10;} proxy_temp_path /data/wwwroot/proxy_temp_dir; #proxy_cache_path /data/wwwroot/cache.hxage.com levels=1:2 keys_zone=cache.hxage.com:3000m inactive=1y max_size=80G; # Nginx 內(nèi)部重定向規(guī)則會被啟動,當(dāng)URL 指向一個目錄并且在最后沒有包含“/”時,Nginx 內(nèi)部會自動的做一個301 重定向,這時會有兩種情況 # 1、server_name_in_redirect on(默認),URL 重定向為:server_name 中的第一個域名+ 目錄名+ /; # 2、server_name_in_redirect off,URL 重定向為:原URL 中的域名+ 目錄名+ / server_name_in_redirect off; server_tokens off; # sub filter # include sub_filter.conf; # null hostname server { listen 80 default; return 444; access_log off; } server { listen 5566; server_name localhost; index index.html index.htm index.shtml index.php; location ~ ^/status/ { stub_status on; access_log off; } } #---------------- Vhost --------------------# include vhost/*.conf; } |
# 以下時阿里云主機上的一段nginx.conf配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
user www www; worker_processes auto; error_log /alidata/log/nginx/error.log crit; pid /alidata/server/nginx/logs/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 65535; events { use epoll; worker_connections 65535; } http { include mime.types; default_type application/octet-stream; #charset gb2312; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 8m; sendfile on; tcp_nopush on; keepalive_timeout 15; 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; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; gzip_disable msie6; #limit_zone crawler $binary_remote_addr 10m; log_format '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; include /alidata/server/nginx/conf/vhosts/*.conf; } |