#user nobody; worker_processes 8; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; worker_rlimit_nofile 65535; events { worker_connections 65535; } http { #websocket 需要加下这个 map $http_upgrade $connection_upgrade { default upgrade; '' close; } include mime.types; default_type application/octet-stream; charset utf-8; #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; #tcp_nopush on; #长连接超时时间,单位是秒 #keepalive_timeout 0; keepalive_timeout 65; #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。 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模块设置 gzip on; #开启gzip压缩输出 gzip_min_length 10k; #最小压缩文件大小 gzip_buffers 4 16k; #压缩缓冲区 gzip_http_version 1.1; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0) gzip_comp_level 5; #压缩等级 gzip_types text/plain application/x-javascript text/css application/xml application/json; #压缩类型,默认就已经包含textml,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。 gzip_vary on; #定义一个名为allips的limit_req_zone用来存储session,大小是10M内存, #以$binary_remote_addr 为key,限制平均每秒的请求为20个, #1M能存储16000个状态,rete的值必须为整数, #如果限制两秒钟一个请求,可以设置成30r/m limit_req_zone $binary_remote_addr zone=allips:10m rate=30r/s; limit_req_zone $binary_remote_addr zone=static:10m rate=1r/s; server { listen 8080; server_name localhost; root /home/json; #charset koi8-r; #access_log logs/host.access.log main; #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /Home/json; location = / { root /home/json/; proxy_pass http://ngintest.com; proxy_http_version 1.1; #限制每ip每秒不超过20个请求,漏桶数burst为5 #brust的意思就是,如果第1秒、2,3,4秒请求为19个, #第5秒的请求为25个是被允许的。 #但是如果你第1秒就25个请求,第2秒超过20的请求返回503错误。 #nodelay,如果不设置该选项,严格使用平均速率限制请求数, #第1秒25个请求时,5个请求放到第2秒执行, #设置nodelay,25个请求将在第1秒执行。 #limit_req zone=allips nodelay; } #JS和CSS缓存时间设置,图片缓存时间设置 location ~ .(gif|jpg|png|js|css)$ { proxy_pass http://ngintest.com; valid_referers blocked *.ttlsa.com server_names ~.google. ~.baidu.; #none|blocked none规则实现了允许空referer访问 if ($invalid_referer) { #rewrite ^/ http://abc.com/error.html; return 403; } expires 1d; #limit_req zone=static burst=3 nodelay; } #location ~ (Home/index) #{ #proxy_pass http://ngintest.com; #limit_req zone=static burst=12 nodelay; #} location ~ (Login|Home|Platform){ proxy_pass http://ngintest.com; limit_req zone=allips burst=12 nodelay; } location / { proxy_pass http://ngintest.com; proxy_http_version 1.1; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } } #服务器集群# upstream ngintest.com{ #ip_hash; server localhost:8118 weight=1 max_fails=2 fail_timeout=1s; server localhost:8228 weight=1 max_fails=2 fail_timeout=1s; #server 192.168.2.212:8989 weight=1 max_fails=2 fail_timeout=1s; } }