1. 概述
1.1 應用場景
nginx.conf 檔案是 Nginx 的主配置檔案,它包含了 Nginx 伺服器的全局配置和各個模組的配置指令。
因此在部署好 Nginx 後,需要修改 nginx.conf 檔案才能正常使用。
1.2 解決思路
本文提供了兩種配置方式:
通用配置,標準即 HTTP 情況下只需要更改 IP 和埠即可使用。
HTTPS 負載均衡,需要一些配置的其他要求。
2. HTTP(常用配置)
2.1 Nginx 通用配置檔案
Nginx通用配置:nginx-通用版.conf
下載後修改檔案內節點 IP 和埠,重新命名為 nginx.conf,然後更換原始的/usr/nginx/conf/nginx.conf
2.2 配置檔案詳解
#使用者或者使用者組 預設為nobody
#user root;
#允許進程數量,建議設定為cpu核心數或者auto自動檢查,注意Windows伺服器上雖然可以啟動多個processes,但是實際只會用其中一個
worker_processes auto;
#自動根據CPU數目綁定CPU親緣性,nginx1.9.10及以上版本才能用
#worker_cpu_affinity auto;
#error日誌存放位置
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;
#預設檔案類型,預設為text/plain
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確定了Nginx是否儲存存取日誌,將這個設定為關閉可以降低磁碟IO而提升速度
access_log off;
#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 65s;
#types_hash_max_size越小,消耗的記憶體就越小,但散列key的衝突率可能上升
types_hash_max_size 2048;
#開啟gzip壓縮
#gzip on;
#gzip_disable "MSIE [1-6].";
#設定請求緩衝
client_header_buffer_size 512k;
large_client_header_buffers 4 512k;
#允許使用者最大上傳資料大小,根據業務需求調整上傳檔案大小限制
client_max_body_size 100M;
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=15 fail_timeout=300s;
server 10.120.133.96:8075 max_fails=15 fail_timeout=300s;
keepalive 300;
#其它server參數說明:
#down 標記伺服器掛掉
#backup 備份伺服器,當主伺服器(例如上面的95和96)不可用時才加入伺服器;
#weight=number 權重,預設為1
#內建負載均衡策略有ip hash、輪詢、加權輪詢(設定server的weight值)
#ip_hash;
#↓====================主動健康檢查模組配置====================↓#
## interval:向後端傳送的健康檢查包的間隔,單位ms。
## fall(fall_count): 如果連續失敗次數達到fall_count,伺服器就被認為是down
## rise(rise_count): 如果連續成功次數達到rise_count,伺服器就被認為是up。
## timeout: 後端健康請求的逾時時間,單位ms。
## type:健康檢查包的類型,現在支援tcp、udp、http類型
#check interval=2000 rise=3 fall=5 timeout=5000 type=http;
# 檢查請求, 7-16之前的persist版本,只能使用 /webroot/decision/system/info HTTP;
#check_http_send "GET /webroot/decision/system/health HTTP/1.0\r\n\r\n"; # 檢查請求
#check_http_expect_alive http_2xx http_3xx; #該指令指定HTTP答覆的成功狀態,預設認為2XX和3XX的狀態是健康的。
#↑====================主動健康檢查模組配置====================↑#
}
upstream WBS.com {
server 10.120.133.95:38888 max_fails=15 fail_timeout=300s;
server 10.120.133.96:38888 max_fails=15 fail_timeout=300s;
#這裏必須使用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 /status {
# healthcheck_status html;
#}
#↑====================主動健康檢查模組監測頁面====================↑#
#根/下的請求預設轉發到的位置,即埠後面跟的請求全部轉發到如下反向代理
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
#不宜太長或者太短,配合max_fails和fail_timeout設定
#併發量高的情況下,伺服器壓力過大時容易出現逾時,可以考慮max_fails和fail_timeout設定大一點,減少nginx錯誤踢出節點的機率
proxy_connect_timeout 75;
#讀取逾時,預設60s,如果在逾時時間內伺服器未傳回任何資料,視為逾時。如果沒有大數據量計算或匯出的範本,則建議配置不超過100s,如果有大數據量計算或匯出的範本,則根據最長耗時的範本時間進行配置。
proxy_read_timeout 400;
#寫入逾時,預設60s,如果在逾時時間內伺服器未收到資料表示逾時,視為逾時。如果沒有大數據量計算或匯出的範本,則建議配置不超過100s,如果有大數據量計算或匯出的範本,則根據最長耗時的範本時間進行配置。
proxy_send_timeout 400;
}
#定義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 75;
proxy_read_timeout 400;
proxy_send_timeout 400;
#升級目標為$http_upgrade 值實際為websocket
proxy_set_header Upgrade $http_upgrade;
#Connection設定升級
proxy_set_header Connection "upgrade";
}
}
}
3. HTTPS
3.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。
3.2 配置檔案詳解
3.2.1 nginx.conf 檔案
#user root;
...
省略部分參考http配置檔案
...
worker_connections 1024;
}
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;
省略..
upstream FR.com {
省略..
}
upstream WBS.com {
省略..
}
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 75;
proxy_read_timeout 400;
proxy_send_timeout 400;
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 75;
proxy_read_timeout 400;
proxy_send_timeout 400;
proxy_pass http://FR.com;
}
}
}
3.2.2 server.xml 檔案
使用者工程若部署在 Tomcat 中,需參考本節內容修改server.xml檔案,若不是,可忽略本節內容。
修改%tomcat_home%\conf\server.xml,在 Engine 模組下配置一個 Valve :
<Valve className="org.apache.catalina.valves.RemoteIpValve"
remoteIpHeader="X-Forwarded-For"
protocolHeader="X-Forwarded-Proto"
protocolHeaderHttpsValue="https"/>
4. 注意事項
4.1 範本無法預覽,處於重新整理狀態
問題描述
使用者將 JAR 包從 2019-04-03 升級到 2019-05-29 後,任意範本無法正常預覽,一直處於重新整理狀態,但 Nginx 配置了 38888 埠後,可正常預覽。
原因分析
埋點時 38888 埠沒被轉發 (非叢集)導致的。
解決方案
使用者需在 Nginx 中代理 38888 埠,監聽 38888 埠,對請求中有 socket.io 的進行轉發,程式碼如下所示:
server {
listen 38888;
server_name 域名;
location /socket.io/ {
proxy_pass http://ip:38888;
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";
}
}
4.2 無線網路登入頁面空白
問題描述
叢集環境下有線網路可正常登入,無線網路登入頁面空白,但是節點可以正常登入。
原因分析
Nginx 無權限讀取資料流,報錯日誌資訊如下圖所示:
解決方案
修改 nginx.conf 中的使用者組配置,如下圖所示: