历史版本2 :Nginx.conf配置手册 返回文档
编辑时间: 内容长度:图片数:目录数: 修改原因:

Nginx 部署后,需要修改 nginx.conf 才能使用,帆软提供了通用配置供大家使用,常规情况下只需要更改ip和端口即可使用,需要有 https 需求的,此文档里也会进行提供,供大家参考。

目录:

一、http(常用配置)编辑

1.1 Nginx 通用配置文件

点击下载:nginx-通用版.conf

下载后修改文件内节点ip和端口,重命名为 nginx.conf,然后替换原始的/usr/nginx/conf/nginx.conf

1.2 配置文件详解

#用户或者用户组 默认为 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 处理速度,降低系统的u ptime.

    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;

        #其它s erver 参数说明:

        #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编辑

2.1 配置要求

要求 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。

2.2 配置文件详解

#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;

        }

    }

}