目錄
- 1. nginx.cof組態檔說明
- 2. nginx多服務配置實體
- 3. 高可用負載均衡實作
1. nginx.cof組態檔說明
# 運行用戶
user nobody;
# 啟動行程,通常設定成和cpu的數量相等
worker_processes 1;
# 全域錯誤日志及PID檔案
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
# 作業模式及連接數上限
events {
# epoll是多路復用IO(I/O Multiplexing)中的一種方式,僅用于linux2.6以上內核,可以大大提高nginx的性能
use epoll;
# 單個后臺worker process行程的最大并發鏈接數
worker_connections 1024;
# 并發總數是 worker_processes 和 worker_connections 的乘積
# 即 max_clients = worker_processes * worker_connections
# worker_connections 值的設定跟物理記憶體大小有關
# 因為并發受IO約束,max_clients的值須小于系統可以打開的最大檔案數
# 而系統可以打開的最大檔案數和記憶體大小成正比,一般1GB記憶體的機器上可以打開的檔案數大約是10萬左右
# 所以,worker_connections 的值需根據 worker_processes 行程數目和系統可以打開的最大檔案總數進行適當地進行設定
# 使得并發總數小于作業系統可以打開的最大檔案數目
# 其實也就是根據主機的物理CPU和記憶體進行配置
# 當然,理論上的并發總數可能會和實際有所偏差,因為主機還有其他的作業行程需要消耗系統資源,
# ulimit -SHn 65535
}
http {
# 設定mime型別,型別由mime.type檔案定義
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 指令指定 nginx 是否呼叫 sendfile 函式(zero copy 方式)來輸出檔案,
# 對于普通應用,必須設為 on,
# 如果用來進行下載等應用磁盤IO重負載應用,可設定為 off,
# 以平衡磁盤與網路I/O處理速度,降低系統的uptime.
sendfile on;
#tcp_nopush on;
# 連接超時時間
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
# 開啟gzip壓縮
gzip on;
gzip_disable "MSIE [1-6].";
# 設定請求緩沖
client_header_buffer_size 128k;
large_client_header_buffers 4 128k;
# 設定虛擬主機配置
server {
# 監聽80埠
listen 80;
# 定義使用 www.nginx.cn訪問
server_name www.nginx.cn;
# 定義服務器的默認網站根目錄位置
root html;
# 設定本虛擬主機的訪問日志
access_log logs/nginx.access.log main;
# 默認請求
location / {
# 定義首頁索引檔案的名稱
index index.php index.html index.htm;
}
# 定義錯誤提示頁面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
# 靜態檔案,nginx自己處理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
# 過期30天,靜態檔案不怎么更新,過期可以設大一點,
# 如果頻繁更新,則可以設定得小一點,
expires 30d;
}
# PHP 腳本請求全部轉發到 FastCGI處理. 使用FastCGI默認配置.
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# 禁止訪問 .htxxx 檔案
location ~ /.ht {
deny all;
}
}
}
- main:用于進行nginx全域資訊的配置
- events:用于nginx作業模式的配置
- http:用于進行http協議資訊的一些配置
- server:用于進行服務器訪問資訊的配置
- location:用于進行訪問路由的配置
- upstream:用于進行負載均衡的配置
down:表示單前的server暫時不參與負載.
weight:默認為1.weight越大,負載的權重就越大,
max_fails:允許請求失敗的次數默認為1.當超過最大次數時,回傳proxy_next_upstream 模塊定義的錯誤.
fail_timeout: max_fails次失敗后,暫停的時間,Nginx基于連接探測,如果發現后端例外,在單位周期為fail_timeout設定的時間中達到max_fails次數,這個周期次數內,如果后端同一個節點不可用,那么把節點標記為不可用,并等待下一個周期(同樣時常為fail_timeout)再一次去請求,判斷是否連接是否成功,
backup:其它所有的非backup機器down或者忙的時候,請求backup機器,所以這臺機器壓力會最輕,
upstream server { # 定義負載均衡設備的Ip及設備狀態
ip_hash;
server 10.0.0.11:9090 down;
server 10.0.0.11:8080 weight=2;
server 10.0.0.11:6060;
server 10.0.0.11:7070 backup;
}
2. nginx多服務配置實體
nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream minio-server {
# weight:默認為1,weight越大,負載的權重就越大,
# backup:其它所有的非backup機器down或者忙的時候,請求backup機器,所以這臺機器壓力會最輕,
# max_fails:允許請求失敗的次數默認為1,當超過最大次數時,回傳 proxy_next_upstream 模塊定義的錯誤,
# fail_timeout:Nginx基于連接探測,如果發現后端例外,在單位周期為fail_timeout設定的時間中達到max_fails次數,這個周期次數內,如果后端同一個節點不可用,那么把節點標記為不可用,并等待下一個周期(同樣時常為fail_timeout)再一次去請求,判斷是否連接是否成功,
server 192.168.199.140:9000 weight=2 max_fails=3 fail_timeout=10s;
server 192.168.199.141:9000 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $remote_addr;
# 傳輸檔案快取大小及單次請求大小
client_body_buffer_size 10M;
client_max_body_size 1G;
# 宕機檢測,如果設定時間內無回應,則直接切換到其它服務
proxy_connect_timeout 4;
proxy_send_timeout 4;
proxy_read_timeout 4;
proxy_pass http://minio-server;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
3. 高可用負載均衡實作
此配置下 nginx + keepalived 在多臺服務器上搭建,可以實作高可用負載均衡,
keepalived安裝及組合nginx配置負載實作高可用
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/278386.html
標籤:其他
下一篇:初識HTML02
