配置文件解析
配置文件主要由四部分組成:
- main(全區(qū)設(shè)置)
- server(主機(jī)配置)
- upstream(負(fù)載均衡服務(wù)器設(shè)置)
- location(URL匹配特定位置設(shè)置)。
下面以默認(rèn)的配置文件來說明下具體的配置文件屬性含義:
#Nginx的worker進(jìn)程運(yùn)行用戶以及用戶組
#user nobody;
#Nginx開啟的進(jìn)程數(shù)
worker_processes 1;
#定義全局錯(cuò)誤日志定義類型掂铐,[debug|info|notice|warn|crit]
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#指定進(jìn)程ID存儲(chǔ)文件位置
#pid logs/nginx.pid;
#事件配置
events {
#use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
#epoll模型是Linux內(nèi)核中的高性能網(wǎng)絡(luò)I/O模型盒音,如果在mac上面侥袜,就用kqueue模型舱权。
use kqueue;
#每個(gè)進(jìn)程可以處理的最大連接數(shù),理論上每臺(tái)nginx服務(wù)器的最大連接數(shù)為worker_processes*worker_connections旗唁。理論值:worker_rlimit_nofile/worker_processes
worker_connections 1024;
}
#http參數(shù)
http {
#文件擴(kuò)展名與文件類型映射表
include mime.types;
#默認(rèn)文件類型
default_type application/octet-stream;
#日志相關(guān)定義
#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;
#防止網(wǎng)絡(luò)阻塞
#tcp_nopush on;
#客戶端連接超時(shí)時(shí)間逆皮,單位是秒
#keepalive_timeout 0;
keepalive_timeout 65;
#開啟gzip壓縮輸出
#gzip on;
#虛擬主機(jī)基本設(shè)置
server {
#監(jiān)聽的端口號(hào)
listen 80;
#訪問域名
server_name localhost;
#編碼格式宅粥,如果網(wǎng)頁格式與當(dāng)前配置的不同的話將會(huì)被自動(dòng)轉(zhuǎn)碼
#charset koi8-r;
#虛擬主機(jī)訪問日志定義
#access_log logs/host.access.log main;
#對(duì)URL進(jìn)行匹配
location / {
#訪問路徑,可相對(duì)也可絕對(duì)路徑
root html;
#首頁文件电谣,匹配順序按照配置順序匹配
index index.html index.htm;
}
#錯(cuò)誤信息返回頁面
#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;
}
#訪問URL以.php結(jié)尾則自動(dòng)轉(zhuǎn)交給127.0.0.1
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
#php腳本請(qǐng)求全部轉(zhuǎn)發(fā)給FastCGI處理
# 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;
#}
#禁止訪問.ht頁面
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
#第二個(gè)虛擬主機(jī)配置
# 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虛擬主機(jī)定義
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include servers/*;
}
反向代理實(shí)例
假設(shè)我現(xiàn)在需要本地訪問www.baidu.com;配置如下:
server {
#監(jiān)聽80端口
listen 80;
server_name localhost;
# individual nginx logs for this web vhost
access_log /tmp/access.log;
error_log /tmp/error.log ;
location / {
proxy_pass http://www.baidu.com;
}
驗(yàn)證結(jié)果:
image.png
可以看到秽梅,我在瀏覽器中使用localhost打開了百度的首頁...
負(fù)載均衡實(shí)例
下面主要驗(yàn)證最常用的三種負(fù)載策略。虛擬主機(jī)配置:
server {
#監(jiān)聽80端口
listen 80;
server_name localhost;
# individual nginx logs for this web vhost
access_log /tmp/access.log;
error_log /tmp/error.log ;
location / {
#負(fù)載均衡
#輪詢
#proxy_pass http://polling_strategy;
#weight權(quán)重
#proxy_pass http://weight_strategy;
#ip_hash
# proxy_pass http://ip_hash_strategy;
#fair
# proxy_pass http://fair_strategy;
#url_hash
# proxy_pass http://url_hash_strategy;
#重定向
#rewrite ^ http://localhost:8080;
}
輪詢策略
1剿牺、輪詢(默認(rèn))
# 每個(gè)請(qǐng)求按時(shí)間順序逐一分配到不同的后端服務(wù)器企垦,如果后端服務(wù)器down掉,能自動(dòng)剔除晒来。
upstream polling_strategy {
server test.com:8080; # 應(yīng)用服務(wù)器1
server test.com:8081; # 應(yīng)用服務(wù)器2
}
測(cè)試結(jié)果(通過端口號(hào)來區(qū)分當(dāng)前訪問):
8081:This is 8081
8080:This is 8080
8081:This is 8081
8080:This is 8080
權(quán)重策略
2钞诡、指定權(quán)重
指定輪詢幾率,weight和訪問比率成正比湃崩,用于后端服務(wù)器性能不均的情況荧降。
upstream weight_strategy {
server test.com:8080 weight=1; # 應(yīng)用服務(wù)器1
server test.com:8081 weight=9; # 應(yīng)用服務(wù)器2
}
測(cè)試結(jié)果:總訪問次數(shù)15次,根據(jù)上面的權(quán)重配置攒读,兩臺(tái)機(jī)器的訪問比重:2:13朵诫;滿足預(yù)期。
ip hash策略
3薄扁、IP綁定 ip_hash
#每個(gè)請(qǐng)求按訪問ip的hash結(jié)果分配剪返,這樣每個(gè)訪客固定訪問一個(gè)后端服務(wù)器,
#可以解決session的問題;在不考慮引入分布式session的情況下邓梅,
#原生HttpSession只對(duì)當(dāng)前servlet容器的上下文環(huán)境有效
upstream ip_hash_strategy {
ip_hash;
server glmapper.net:8080; # 應(yīng)用服務(wù)器1
server glmapper.net:8081; # 應(yīng)用服務(wù)器2
}
- iphash 算法:ip是基本的點(diǎn)分十進(jìn)制脱盲,將ip的前三個(gè)端作為參數(shù)加入hash函數(shù)。這樣做的目的是保證ip地址前三位相同的用戶經(jīng)過hash計(jì)算將分配到相同的后端server日缨。作者的這個(gè)考慮是極為可取的钱反,因此ip地址前三位相同通常意味著來著同一個(gè)局域網(wǎng)或者相鄰區(qū)域,使用相同的后端服務(wù)讓nginx在一定程度上更具有一致性匣距。
其他負(fù)載均衡策略
這里需要安裝三方插件
4诈铛、fair(第三方)
#按后端服務(wù)器的響應(yīng)時(shí)間來分配請(qǐng)求,響應(yīng)時(shí)間短的優(yōu)先分配墨礁。
upstream fair_strategy {
server glmapper.net:8080; # 應(yīng)用服務(wù)器1
server glmapper.net:8081; # 應(yīng)用服務(wù)器2
fair;
}
5幢竹、url_hash(第三方)
#按訪問url的hash結(jié)果來分配請(qǐng)求,使每個(gè)url定向到同一個(gè)后端服務(wù)器恩静,
#后端服務(wù)器為緩存時(shí)比較有效焕毫。
upstream url_hash_strategy {
server glmapper.net:8080; # 應(yīng)用服務(wù)器1
server glmapper.net:8081; # 應(yīng)用服務(wù)器2
hash $request_uri;
hash_method crc32;
}
第三方模塊安裝:編譯安裝
先構(gòu)建好目錄蹲坷,這不是必須的,只是個(gè)人喜歡有條有序的管理
cd ~/myapp # 存放我的應(yīng)用程序
mkdir bin # 存放編譯出來的可執(zhí)行命令
mkdir src # 存放源文件
mkdir etc # 存放配置文件
mkdir var # 存放 log 文件和 pid 文件
cd src
#下載 nginx 安裝包
wget http://nginx.org/download/nginx-1.13.12.tar.gz
# 下載 fair 模塊
git clone https://github.com/itoffshore/nginx-upstream-fair
# 解壓
tar zxvf nginx-1.13.12.tar.gz
cd nginx-1.13.12
# 安裝配置邑飒,在這里通過--add-module添加第三方模塊循签,比較重要,否則安裝會(huì)失敗
# 下面的配置三選一即可
# 比較完整的配置
./configure --prefix=/Users/ginkgo/myapp/etc/nginx --sbin-path=/Users/ginkgo/myapp/bin/nginx --conf-path=/Users/ginkgo/myapp/etc/nginx/nginx.conf --error-log-path=/Users/ginkgo/myapp/var/log/nginx/error.log --http-client-body-temp-path=/Users/ginkgo/myapp/var/lib/nginx/body --http-fastcgi-temp-path=/Users/ginkgo/myapp/var/lib/nginx/fastcgi --http-log-path=/Users/ginkgo/myapp/var/log/nginx/access.log --http-proxy-temp-path=/Users/ginkgo/myapp/var/lib/nginx/proxy --lock-path=/Users/ginkgo/myapp/var/lock/nginx.lock --pid-path=/Users/ginkgo/myapp/var/run/nginx.pid --with-debug --with-http_dav_module --with-http_flv_module --with-http_gzip_static_module --with-http_realip_module --with-http_stub_status_module --with-http_geoip_module --with-http_ssl_module --with-http_sub_module --with-ipv6 --with-mail --with-mail_ssl_module --with-openssl=/Users/ginkgo/myapp/src/openssl --add-module=/Users/ginkgo/myapp/src/nginx-upstream-fair
# --with-openssl=/Users/ginkgo/myapp/src/openssl 指定 OpenSSL 的庫路徑
# 學(xué)習(xí)用不著那么多模塊疙咸,用下面的就行县匠,或者你也可以移除更多的模塊
./configure --prefix=/Users/ginkgo/myapp/etc/nginx --sbin-path=/Users/ginkgo/myapp/bin/nginx --conf-path=/Users/ginkgo/myapp/etc/nginx/nginx.conf --error-log-path=/Users/ginkgo/myapp/var/log/nginx/error.log --http-client-body-temp-path=/Users/ginkgo/myapp/var/lib/nginx/body --http-fastcgi-temp-path=/Users/ginkgo/myapp/var/lib/nginx/fastcgi --http-log-path=/Users/ginkgo/myapp/var/log/nginx/access.log --http-proxy-temp-path=/Users/ginkgo/myapp/var/lib/nginx/proxy --lock-path=/Users/ginkgo/myapp/var/lock/nginx.lock --pid-path=/Users/ginkgo/myapp/var/run/nginx.pid --with-debug --with-http_gzip_static_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-mail --with-mail_ssl_module --with-openssl=/Users/ginkgo/myapp/src/openssl --add-module=/Users/ginkgo/myapp/src/nginx-upstream-fair
# 只保留 fair 模塊的配置
./configure --prefix=/Users/ginkgo/myapp/etc/nginx --sbin-path=/Users/ginkgo/myapp/bin/nginx --conf-path=/Users/ginkgo/myapp/etc/nginx/nginx.conf --error-log-path=/Users/ginkgo/myapp/var/log/nginx/error.log --http-log-path=/Users/ginkgo/myapp/var/log/nginx/access.log --pid-path=/Users/ginkgo/myapp/var/run/nginx.pid --with-debug --add-module=/Users/ginkgo/myapp/src/nginx-upstream-fair
# make 編譯 安裝
make && make install
cd /Users/ginkgo/myapp/bin
# -c 指定路徑的配置文件啟動(dòng),默認(rèn)/Users/ginkgo/myapp/etc/nginx/nginx.conf
sudo ./nginx -c /Users/ginkgo/myapp/etc/nginx/nginx.conf
# 這里會(huì)報(bào)錯(cuò)/lib/nginx/body文件夾找不到撒轮,那就創(chuàng)建一下乞旦,再啟動(dòng)
# 修改了配置文件,熱加載生效
sudo ./nginx -s reload
# 關(guān)閉
sudo ./nginx -s quit
重定向rewrite
location / {
#重定向
#rewrite ^ http://localhost:8080;
}
驗(yàn)證思路:本地使用localhost:80端口進(jìn)行訪問,根據(jù)nginx的配置题山,如果重定向沒有生效兰粉,則最后會(huì)停留在當(dāng)前l(fā)ocalhost:80這個(gè)路徑,瀏覽器中的地址欄地址不會(huì)發(fā)生改變顶瞳;如果生效了則地址欄地址變?yōu)閘ocalhost:8080玖姑。