这里提供http和https两种通用配置及其详解,供大家参考。
一、http编辑
nginx通用配置文件:nginx-通用版.conf
配置文件详解:
#用户或者用户组 默认为nobody #user root; #允许进程数量,通常和集群总CPU个数一致,即节点数*服务器CPU核数,注意Windows上虽然可以启动多个processor,但是实际只会使用其中一个 #建议设置为cpu核心数或者auto自动检测 worker_processes auto; #日志文件存放位置 error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #PID存放位置 #pid logs/nginx.pid; #工作模式及连接数上限 events { #每个worker_processes的最大连接数 #windows上无论这里设置多大实际都只有1024 #并发数是 worker_processes 和 worker_connections 的乘积 worker_connections 1024; } http { #设定mime类型,类型由mime.type文件定义 include mime.types; default_type application/octet-stream;#默认文件类型,默认为text/plain #日志格式 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" $upstream_addr'; access_log logs/access.log main; #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件, #对于普通应用,必须设为 on, #如果用来进行下载等应用磁盘IO重负载应用,可设置为 off, #以平衡磁盘与网络I/O处理速度,降低系统的uptime. sendfile on; #tcp_nopush on; #http长连接(client <-> nginx)超时时间,请求完成之后连接保持的时间 #keepalive_timeout 0; keepalive_timeout 65; #开启gzip压缩 #gzip on; #gzip_disable "MSIE [1-6]."; #设定请求缓冲 #client_header_buffer_size 128k; #large_client_header_buffers 4 128k;
upstream FR.com { #max_fails=n fail_timeout=m 表示m时间内超时n次失败尝试,将会在接下来m时间内标记此节点不可用(m时间过后无论此节点是否启动都会被重新标记为可用状态) #其中fails为“失败尝试”,失败尝试由下面server配置中,proxy_next_upstream指令定义 server 10.120.133.95:8075 max_fails=10 fail_timeout=100s; server 10.120.133.96:8075 max_fails=10 fail_timeout=100s; #其它server参数说明: #down 标记服务器挂掉 #backup 备份服务器,当主服务器(例如上面的95和96)不可用时才加入服务器; #weight=number 权重,默认为1
#内置负载均衡策略有ip hash、轮询、加权轮询(设置server的weight值) #ip_hash; } upstream WBS.com { server 10.120.133.95:38888 max_fails=10 fail_timeout=100s; server 10.120.133.96:38888 max_fails=10 fail_timeout=100s; #这里必须使用ip_hash ip_hash; } server { listen 80; server_name localhost; #nginx默认不转发带下划线的header,比如请求的header中有_device_这个header,转发到负载服务器时默认会丢弃 #可以在http或者http -> server这两个上下文中加入一条属性 underscores_in_headers on; #charset koi8-r; #access_log logs/host.access.log main; #根/下的请求默认转发到的位置,即端口后面跟的请求全部转发到如下反向代理 location / { #对于HTTP代理,proxy_http_version指令应该设置为“1.1”,同时“Connection”头的值也应被清空(如下proxy_set_header Connection "") proxy_http_version 1.1; #定义转发到的服务器(组) proxy_pass http://FR.com; #设置nginx转发时转发到下一个服务器的条件,此处设置将未成功请求(proxy_next_upstream定义的40x, 50x等)传递到下一步进行处理 # 失败尝试说明:50x和429在此配置会被当作fail,error,timeout并 invalid_header无论配置与否都被认为fail,http_403 and http_404配置与否都不是fail。 # error:与后端服务器建立连接、传递请求、读取请求头发生的错误 # timeout:与后端服务器建立连接、传递请求、读取请求头超时,设置在proxy_next_upstream_timeout和proxy_next_upstream_tries(默认值都是0) # invalid_header:服务器返回非法或者为空 proxy_next_upstream http_500 http_502 http_503 http_504 http_403 http_404 http_429 error timeout invalid_header non_idempotent; #proxy_next_upstream_timeout 0; #proxy_next_upstream_tries 0; #重定向,参数off将在这个字段中禁止所有的proxy_redirect指令 proxy_redirect off; #请求头的一些设置,语法proxy_set_header [field] [value]; #$host为请求的主机名称(即nginx代理服务器), $server_port为主机端口(即nginx端口),如果有外网映射,这里应改写 外网地址:外网端口 形式(不要带协议) proxy_set_header Host $host:$server_port; #这里$remote_addr客户端ip地址 proxy_set_header X-Real-IP $remote_addr; #这里$proxy_add_x_forwarded_for是代理层级,如果由多层代理,这里就写client,proxy1,proxy2,这里应该是client即客户端ip proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#与后端服务器的连接不需要保持 proxy_set_header Connection ""; #NGINX会缓冲来自代理服务器的响应。响应存储在内部缓冲区中,并且在收到整个响应之前不会发送到客户端。 #缓冲有助于优化慢客户端的性能,如果响应从NGINX同步传递到客户端,则会浪费代理服务器时间。 #但是,当启用缓冲时,NGINX允许代理服务器快速处理响应,而NGINX将响应存储的时间与客户端下载它们所需的时间相同 #是否开启缓冲,默认开启 #proxy_buffering off; #设置缓冲区的大小为size #proxy_buffer_size 64k; #每个连接设置缓冲区的数量和大小,proxy_buffers [number] [size]; #proxy_buffers 32 64k; #当开启缓冲响应的功能以后,在没有读到全部响应的情况下,写缓冲到达一定大小时,nginx一定会向客户端发送响应,直到缓冲小于此值 #proxy_busy_buffers_size 64k; #连接到代理服务器超时时间,默认60s,不能超过75s #不宜太长或者太短,20为宜,配合max_fails和fail_timeout设置 #并发量高的情况下,服务器压力过大时容易出现超时,可以考虑max_fails和fail_timeout设置大一点,减少nginx错误踢出节点的几率 proxy_connect_timeout 20; #读取超时,默认60s,如果在超时时间内服务器未返回任何数据,视为超时。有些报表计算或者导出时后端需要花费几分钟甚至更多,因此需要设置长一点,建议1000s以上 proxy_read_timeout 1000; #写入超时,默认60s,如果在超时时间内服务器未收到数据表示超时 proxy_send_timeout 300; }
#定义404页面 #error_page 404 /404.html; # redirect server error pages to the static page /50x.html #定义50x页面 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { #此处为websocket端口,如果是集群部署,FineReport工程为38889,FineBI工程为48889 listen 38889; server_name localhost; location / { proxy_http_version 1.1; proxy_pass http://WBS.com; proxy_connect_timeout 20; proxy_read_timeout 1000; proxy_send_timeout 300;
#升级目标为$http_upgrade 值实际为websocket proxy_set_header Upgrade $http_upgrade; #Connection设置升级 proxy_set_header Connection "upgrade"; } } } |
二、https编辑
要求https的负载均衡(不限于nginx)的配置符合如下几项要求:
1、443监听,而且转发到后端只能是从443进来的请求;
2、tomcat或者其它服务器容器不做任何ssl配置;
3、负载均衡监听http的80端口并重定向到https443端口;
4、websocket只需要做38889/48889(FineReport工程为38889,FineBI工程为48889)的ssl监听;
5、负载均衡转发到tomcat前要设置请求头的X-Forwarded-Proto值为https。
配置文件详解:
#user root; worker_processes auto; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 2048; } http { #add_header access-control-allow-Origin *; add_header access-control-allow-Headers X-Requested-With; add_header access-control-allow-Methods GET,POST,OPTIONS; 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" $upstream_addr';
#access_log logs/access.log main;
sendfile on; #tcp_nopush on;
#keepalive_timeout 0; keepalive_timeout 65;
#gzip on;
upstream FR.com { server 192.168.5.232:8080 max_fails=10 fail_timeout=100s; server 192.168.5.233:8080 max_fails=10 fail_timeout=100s; server 192.168.5.235:8080 max_fails=10 fail_timeout=100s; }
upstream WBS.com { server 192.168.5.232:38888 max_fails=10 fail_timeout=100s; server 192.168.5.233:38888 max_fails=10 fail_timeout=100s; server 192.168.5.235:38888 max_fails=10 fail_timeout=100s; ip_hash; #keepalive 64; }
server { listen 80; server_name 192.168.5.232; underscores_in_headers on; #charset koi8-r;
#access_log logs/host.access.log main;
location / { rewrite ^(.*)$ https://$server_name$1 permanent; }
#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; } }
server { #FineReport工程为38889,FineBI工程为48889 listen 38889 ssl; server_name 192.168.5.232;
ssl_certificate /mnt/hgfs/share/keys/server.crt; ssl_certificate_key /mnt/hgfs/share/keys/server.key;
ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m;
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; ssl_prefer_server_ciphers on; location / {
proxy_request_buffering off; proxy_buffering off; proxy_connect_timeout 20; proxy_read_timeout 1000; proxy_send_timeout 300; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade";
proxy_pass http://WBS.com; } } server { listen 443 ssl; server_name 192.168.5.232;
#ssl on; ssl_certificate /mnt/hgfs/share/keys/server.crt; ssl_certificate_key /mnt/hgfs/share/keys/server.key;
ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; ssl_prefer_server_ciphers on;
#fastcgi_param HTTPS on; #attention!#
location / { proxy_http_version 1.1; proxy_set_header Connection "";
proxy_buffering off; proxy_next_upstream http_500 http_502 http_503 http_504 http_403 http_404 http_429 error timeout invalid_header non_idempotent; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 把 https 的协议告知 Tomcat,否则 Tomcat 可能认为是 http 的请求 proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 20; proxy_read_timeout 1000; proxy_send_timeout 300;
proxy_pass http://FR.com; } } } |