Nginx 配置实践
2016-03-29 00:00
567 查看
摘要: Nginx的配置网上很多,但实践下来才知道其中真相
nginx 一般用作请求转发,用作服务器集群的负载均衡
典型的高并发集群是 nginx+tomcat(多个)
nginx可以高效处理对静态文件的请求,tomcat 负责动态请求
配置范例:
负载均衡和热备份
粘性session的负载均衡,需要安装对应的模块,即重新编译安装,ip_hash 也可以做到粘性session 效果但两者本质不同
nginx 重新编译安装需要把需要的模块一次性配置好,可以指定安装目录
nginx 负载均衡和热备份是两种配置,负载均衡也可以实现热备份的效果,对于宕机,后台集群的某台机器的tomcat挂掉,nginx不会继续向其转发,如果是tomcat正常,api 报错,则nginx 识别正常
nginx监控
nginx 本身自带监控,简单的页面,研究过taobao 开发的负载监控,发现编译安装不成功
nginx 转发情况可以通过开启日志监控查看
ip限制请求次数
此配置可以达到限制ip访问频率的效果,但没有返回自定义状态码
总结:nginx常用与负载均衡,可以根据业务需要制定多种负载均衡策略,可以识别网络爬虫,制定防采集策略
对于热备份,常用其他方式实现。
nginx 一般用作请求转发,用作服务器集群的负载均衡
典型的高并发集群是 nginx+tomcat(多个)
nginx可以高效处理对静态文件的请求,tomcat 负责动态请求
配置范例:
#user nobody; worker_processes 1; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; 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; gzip on; proxy_connect_timeout 600; #nginx跟后端服务器连接时超时时间 proxy_read_timeout 600; ##连接成功后,后端服务器响应时间(代理接收超时) proxy_send_timeout 600; #后端服务器数据回传时间(代理发送超时) proxy_buffer_size 64k; ##设置代理服务器(nginx)保存用户头信息的缓冲区大小 proxy_buffers 4 64k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置 proxy_busy_buffers_size 128k; # proxy_temp_file_write_size 128k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传 upstream youyiskyweb { sticky; #粘性session #server 172.16.129.121:1337; #server 172.16.129.155:1337; server 171.121.14.198:1337; server 171.121.14.215:1337; } upstream youyiskyapi { #server 171.121.14.213:8080; server 171.121.14.214:8080; #server 171.121.14.215:8080; } limit_req_status 599; #返回状态吗 limit_req_zone $binary_remote_addr zone=allips:10m rate=60r/s; #限制ip请求频率 server { listen 80; server_name youyisky; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://youyiskyweb; limit_req zone=allips burst=5 nodelay; #root html; #index index.html index.htm; } location /nginx_status { stub_status on; access_log off; #allow SOME.IP.ADD.RESS; #deny all; } location /htnewsroom { proxy_pass http://youyiskyapi; #limit_req zone=allips burst=5 nodelay; } location /user_portrait { proxy_pass http://171.121.14.213:8080/zk_image/user_portrait; } # Directives # check #syntax: *check interval=milliseconds [fall=count] [rise=count] #[timeout=milliseconds] [default_down=true|false] #[type=tcp|http|ssl_hello|mysql|ajp|fastcgi]* #default: *none, if parameters omitted, default parameters are #interval=30000 fall=5 rise=2 timeout=1000 default_down=true type=tcp* # context: *upstream* #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443; # server_name localhost; # ssl on; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_timeout 5m; # ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
负载均衡和热备份
粘性session的负载均衡,需要安装对应的模块,即重新编译安装,ip_hash 也可以做到粘性session 效果但两者本质不同
nginx 重新编译安装需要把需要的模块一次性配置好,可以指定安装目录
nginx 负载均衡和热备份是两种配置,负载均衡也可以实现热备份的效果,对于宕机,后台集群的某台机器的tomcat挂掉,nginx不会继续向其转发,如果是tomcat正常,api 报错,则nginx 识别正常
nginx监控
nginx 本身自带监控,简单的页面,研究过taobao 开发的负载监控,发现编译安装不成功
nginx 转发情况可以通过开启日志监控查看
ip限制请求次数
此配置可以达到限制ip访问频率的效果,但没有返回自定义状态码
总结:nginx常用与负载均衡,可以根据业务需要制定多种负载均衡策略,可以识别网络爬虫,制定防采集策略
对于热备份,常用其他方式实现。
相关文章推荐
- nginx代理指定目录
- 访问Nginx发生SSL connection error的一种情况
- Nginx+Naxsi部署专业级Web应用防火墙
- CentOS 6.2实战部署Nginx+MySQL+PHP
- nginx中http核心模块的配置指令2
- nginx中http核心模块的配置指令3
- nginx中http核心模块的配置指令4
- nginx中http的fastcgi模块的配置指令1
- Nginx 学习笔记(一)
- [游戏资讯]《极品飞车16:亡命狂飙》配置需求公布
- 配置文件神器 typesafe conf 用法简介
- 配置文件神器 typesafe conf 用法简介
- 网站502与504错误分析
- 用zabbix监控nginx_status状态
- 路由器备份配置
- 路由器的基本配置
- 路由器基础配置及传输协议
- 经典配置 Vlan篇