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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
NGINX 优化 配置文件
#参考文章  http://www.ha97.com/5194.html    http://www.cnblogs.com/dawnlee/p/5142724.html
  
#运行用户
user nginx;
  
#进程文件pid
pid pid /nginx .pid;
  
#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
error_log  /var/log/nginx/error .log info;
  
#优化worker processes
#nginx是多进程的而不是多线程的,对于进程相关的配置我们需要如下优化。首先看一下服务器的处理器数。
worker_processes 2;
worker_rlimit_nofile 100000;
  
#工作模式与连接数上限
events {
  
#单个进程最大连接数(最大连接数=连接数*进程数)   
     worker_connections 2048;
     multi_accept on;  #必须打开
  
#参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
     use epoll;
}
http {
#引用其他配置文件
     include       mime.types; #文件扩展名与文件类型映射表
     include       conf.d /blog .conf;
     include      conf.d /bugfix .conf;
     include      conf.d /me .conf;
     charset utf-8;  #默认编码
     default_type  application /octet-stream ; #默认文件类型
     fastcgi_intercept_errors on;   #开启自定义404页面
  
#日志格式
     log_format  main   '$remote_addr - $remote_user [$time_local] "$request" '
                       '$status $body_bytes_sent "$http_referer" '
                       '"$http_user_agent" "$http_x_forwarded_for"' ;
  
  
  
#LOGS 禁用或者优化access_log大流量访问时,较大的访问会导致访问日志对磁盘的读写非常大。如果不需要日志的话,可以禁用掉。
#access_log off;
#log_not_found off;
#或者打开缓冲(定义访问日志)
access_log logs /access .log main buffer=32k;
  
  
tcp_nopush on;  #防止网络阻塞
tcp_nodelay on;  #防止网络阻塞
keepalive_timeout 120;  #长连接超时时间,单位是秒
  
#其他
default_type text /html ;
charset UTF-8;
server_tokens off; 
sendfile on; 
tcp_nopush on; 
tcp_nodelay on;
error_log logs /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; 
  
  
#开启高效文件传输模式
gzip  on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text /plain  text /css  application /json  application /x-javascript  text /xml  application /xml  application /xml +rss text /javascript ;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable  "MSIE [1-6]\." ;
  
#优化输出缓存 FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度
fastcgi_buffers 256 16k;
fastcgi_buffer_size 128k;
fastcgi_connect_timeout 3s;
fastcgi_send_timeout 120s;
fastcgi_read_timeout 120s;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
  
#缓存
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_temp_path  /tmp/nginx ;
proxy_cache_path  /tmp/nginx/cache  levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
  
  
  
#负载均衡
upstream  test .com {
#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
server 192.168.80.121:80 weight=3;
server 192.168.80.122:80 weight=2;
server 192.168.80.123:80 weight=3;
}
  
#对 "/" 启用反向代理
location / {
     proxy_pass http: //127 .0.0.1:88;
     proxy_redirect off;
     proxy_set_header Host $host;
     proxy_set_header X-Real-IP $remote_addr; #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  
#以下是一些反向代理的配置,可选。
     client_max_body_size 10m;  #允许客户端请求的最大单文件字节数
     client_body_buffer_size 128k;  #缓冲区代理缓冲用户端请求的最大字节数,
     proxy_connect_timeout 90;  #nginx跟后端服务器连接超时时间(代理连接超时)
     proxy_send_timeout 90;  #后端服务器数据回传时间(代理发送超时)
  
  
#使用ip访问返回500错误
server
{
listen 80 default;
return  500;
}
  
server {
#80 端口跳转443 端口   
     if  ($server_port = 80){
     return  301 https: // $server_name$request_uri;}
  
     if  ($scheme = http){
     return  301 https: // $server_name$request_uri;}
     error_page 497 https: // $server_name$request_uri;
  
  
listen 80;  #监听端口
listen 443 ssl;  #开启ssl端口
server_name www. test .com  test .com; #域名可以有多个,用空格隔开
index index.html index.htm index.php;  #支持的首页类型
root  /data/nginx/html #网站根目录
  
#ssl 配置 (此处以腾讯云申请的证书给出的配置为例子)
       ssl_certificate www. test .com.crt;   #此文件为你申请的ssl证书
       ssl_certificate_key www. test .com.key;   #此文件为你申请的ssl key
       ssl_session_timeout 5m;
       ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
       ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
       ssl_prefer_server_ciphers on;
  
     client_header_buffer_size 2048k;
     large_client_header_buffers 4 2048k;
  
#开启php转发
      location ~ .*\.(php|php5)?$ {
             fastcgi_pass   127.0.0.1:9000;
             fastcgi_index  index.php;
             fastcgi_param  SCRIPT_FILENAME   /data/nginx/html $fastcgi_script_name;
             include        fastcgi_params;
         }  
  
location / {
     root  /data/nginx ;
     index index.php;
      proxy_cache cache_one ; #使用Web缓存区cache_one,已在nginx.conf的缓存配置中命名的
      proxy_ignore_headers  "Cache-Control"  "Expires"  "Set-Cookie" ; #不处理后端服务器返回的指定响应头
     
      #对不同HTTP状态码缓存设置不同的缓存时间
          proxy_cache_valid 200 304 12h ;
          proxy_cache_valid 301 302 1m ;
          proxy_cache_valid any 1m ;
      #设置Web缓存的Key值,Nginx根据Key值md5哈希存储缓存,这里根据"域名,URI,参数"组合成Key
          proxy_cache_key $host$uri$is_args$args;
}
  
#图片缓存时间设置
     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
         root /你的网站目录
         expires 10d;
     }
#JS和CSS缓存时间设置
     location ~ .*\.(js|css)?$ {   
         root /你的网站目录
         expires 1h;
     }
  
  
#使用cdn后 访问日志中获取真实ip
set_real_ip_from *.*.*.*;   #前端主机的ip段或主机ip
real_ip_header    X-Real-IP;
real_ip_header    X-Forwarded-For;
  
#允许后台登录的ip
     location ^~  /admin  {
         allow 61.171.93.119;
         deny all;
             fastcgi_pass   127.0.0.1:9000;
             fastcgi_index  index.php;
             fastcgi_param        SCRIPT_FILENAME    /usr/local/nginx/html/admin $fastcgi_script_name;
             include        fastcgi_params ;
     }
  
     #对于下载目录中的文件大小使用Mb来显示,显示主机文件时间
     location ~*  /novel/ (lnmp|urban|doc|fantasy|thorugh) {
         #允许列出目录中内容
         autoindex on;
  
         #默认为off,显示的文件时间为GMT时间。
         #改为on后,显示的文件时间为文件的服务器时间
         autoindex_localtime on;
  
         #默认为on,显示出文件的确切大小,单位是bytes。
         #改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
         autoindex_exact_size off;
  
#本地动静分离反向代理配置
#所有jsp的页面均交由tomcat或resin处理
     location ~ .(jsp|jspx| do )?$ {
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_pass http: //127 .0.0.1:8080;
     }
  
#所有静态文件由nginx直接读取不经过tomcat或resin
     location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
         { expires 15d; }
  
     location ~ .*.(js|css)?$
         { expires 1h; }}
     }
}