docker配合Nginx部署go應用
Nginx
什么是Nginx
?
Nginx
是一個 Web Server
,可以用作反向代理、負載均衡、郵件代理、TCP / UDP、HTT
P 服務器等等,它擁有很多吸引人的特性,例如:
- 以較低的內存占用率處理 10,000 多個並發連接(每10k非活動HTTP保持活動連接約2.5 MB )
- 靜態服務器(處理靜態文件)
- 正向、反向代理
- 負載均衡
- 通過OpenSSL 對 TLS / SSL 與 SNI 和 OCSP 支持
- FastCGI、SCGI、uWSGI 的支持
- WebSockets、HTTP/1.1 的支持
- Nginx + Lua
名詞解釋
正向代理
舉個例子,國內的用戶想要訪問 Google
時,會被阻擋。所以這個時候把請求發送到另外一個代理服務器(可以訪問 Google
的服務器)上,由其代為轉發請求和接收響應內容。
總結來說:正向代理 是一個位於客戶端和原始服務器(origin server
)之間的服務器,為了從原始服務器取得內容,客戶端向代理發送一個請求並指定目標(原始服務器),然后代理向原始服務器轉交請求並將獲得的內容返回給客戶端。客戶端必須要進行一些特別的設置才能使用正向代理。
正向代理的用途:
-
(1)訪問原來無法訪問的資源,如google
-
(2) 可以做緩存,加速訪問資源
-
(3)對客戶端訪問授權,上網進行認證
-
(4)代理可以記錄用戶訪問記錄(上網行為管理),對外隱藏用戶信息
反向代理
反向代理服務器位於用戶與目標服務器之間,但是對於用戶而言,反向代理服務器就相當於目標服務器,即用戶直接訪問反向代理服務器就可以獲得目標服務器的資源。同時,用戶不需要知道目標服務器的地址,也無須在用戶端作任何設定。反向代理服務器通常可用來作為Web加速,即使用反向代理作為Web服務器的前置機來降低網絡和服務器的負載,提高訪問效率。
反向代理的作用就比較多了,這里簡單列舉一下:
- (1)保護和隱藏原始資源服務器
- (2)加密和SSL加速
- (3)負載均衡
- (4)緩存靜態內容
- (5)壓縮
- (6)減速上傳
- (7)安全
- (8)外網發布
區別:
正向代理需要你主動設置代理服務器ip或者域名進行訪問,由設置的服務器ip或者域名去獲取訪問內容並返回;而反向代理不需要你做任何設置,直接訪問服務器真實ip或者域名,但是服務器內部會自動根據訪問內容進行跳轉及內容返回,你不知道它最終訪問的是哪些機器。
構建鏡像
Nginx鏡像
使用docker-composer
構建Nginx
,創建docker-compose.yml
:
version: '2'
services:
liz-nginx:
image: nginx
restart: always
ports:
- 8888:80
volumes:
- ./wwwroot:/usr/share/nginx/html
簡單介紹下:
- version:表示使用那個版本的compose。
- service:就是要定義的docker容器
- nginx:容器的名稱
- image:表示使用的鏡像。我放的ngixn,也就是docker官方鏡像上目前能找到最新版的鏡像。
- restart:設置為always,表明此容器應該在停止的情況下總是重啟,比如,服務器啟動時,這個容器就跟着啟動,不用手動啟動。
- ports:這個是容器自己運行的端口號和需要暴露的端口號。比如: - 8080:80,表示容器內運行着的端口是80,把端口暴露給8080端口,從外面訪問的是8080端口,就能自動映射到80端口上。
- volumes:這個是數據卷。表示數據、配置文件等存放的位置。
創建docker-compose.yml
,然后啟動。
# docker-compose up -d
Starting nginx_liz-nginx_1 ... done
然后訪問網址
因為我們把wwwroot
掛載到/usr/share/nginx/html
里面了,當時里面沒有放初始化的頁面。
在wwwroot
里面新建index.html
,然后再次訪問
成功了。
配置nginx.conf
然后嘗試把ngixn
需要的配置文件也通過文件掛載的方式加進去。
那么我們來簡單了解些nginx
中的配置文件nginx.conf
#定義Nginx運行的用戶和用戶組
#user nobody;
#nginx進程數,建議設置為等於CPU總核心數。
worker_processes 1;
#全局錯誤日志定義類型,[ debug | info | notice | warn | error | crit ]
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#進程文件
#pid logs/nginx.pid;
#工作模式與連接數上限
events {
#單個進程最大連接數(最大連接數=連接數*進程數)
worker_connections 1024;
}
#設定http服務器
http {
#文件擴展名與文件類型映射表
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函數來輸出文件,對於普通應用設為 on,如果用來進行下載等應用磁盤IO重負載應用,可設置為off,以平衡磁盤與網絡I/O處理速度,降低系統的負載。注意:如果圖片顯示不正常把這個改 成off。
sendfile on;
#防止網絡阻塞
#tcp_nopush on;
#長連接超時時間,單位是秒
#keepalive_timeout 0;
keepalive_timeout 65;
#開啟gzip壓縮輸出
#gzip on;
#虛擬主機的配置
server {
#監聽端口
listen 80;
#域名可以有多個,用空格隔開
server_name localhost;
#默認編碼
#charset utf-8;
#定義本虛擬主機的訪問日志
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# 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;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# 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 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;
# }
#}
}
創建ngixn.conf
user nginx;
# 指定使用 CPU 資源數量
worker_processes 1;
events {
# 連接數
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# 虛擬主機一配置
server {
# 指定端口
listen 80;
# 指定 IP (可以是域名)
server_name 127.0.0.1;
location / {
# 虛擬主機內的資源訪問路徑
root /usr/share/nginx/html;
# 首頁
index index.html index.htm;
}
}
}
然后把我們的nginx.conf
的配置文件給掛載進去
version: '2'
services:
liz-nginx:
image: nginx
restart: always
ports:
- 8888:80
volumes:
- ./conf/nginx.conf:/etc/nginx/nginx.conf
- ./wwwroot:/usr/share/nginx/html
然后啟動docker-compose up -d
再次訪問
server_name
對於其中的server_name
具體是如何使用的呢?
server name
為虛擬服務器的識別路徑。因此不同的域名會通過請求頭中的HOST
字段,匹配到特定的server
塊,轉發到對應的應用服務器中去。
我們重新配置nginx.conf
user nginx;
# 指定使用 CPU 資源數量
worker_processes 1;
events {
# 連接數
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
# 指定端口
listen 80;
# 指定 IP (可以是域名)
server_name www.liz.com;
location / {
# 虛擬主機內的資源訪問路徑
root /usr/share/nginx/html;
# 首頁
index indexa.html index.htm;
}
}
server {
# 指定端口
listen 80;
# 指定 IP (可以是域名)
server_name www.liz.*;
location / {
# 虛擬主機內的資源訪問路徑
root /usr/share/nginx/html;
# 首頁
index indexb.html index.htm;
}
}
}
上面的www.liz.com
指向的是indexa.html
,www.liz.*
指向的是indexb.html
。
然后在本機的hosts
中加入:
192.168.56.201 www.liz.com
192.168.56.201 www.liz.cn
192.168.56.201 www.liz.org
我虛機的ip是192.168.56.201
重啟,我們看下具體的訪問。
這樣通過域名加端口的訪問好奇怪。http
的請求默認的是80
,端口。我們只要把docker-compose
中配置的80
端口也映射到虛機的80
就好了。
version: '2'
services:
liz-nginx:
image: nginx
restart: always
ports:
- 8888:80
- 80:80
volumes:
- ./conf/nginx.conf:/etc/nginx/nginx.conf
- ./wwwroot:/usr/share/nginx/html
然后探討下如何使用nginx
部署go
項目
首先跑起來一個go
項目,這個之前已經做過了,可以參考https://www.cnblogs.com/ricklz/p/12860434.html
直接拿之前創建的鏡像,構建docker-compose.yml
。我就直接把nginx
和go
放到同一個docker-compose.yml
中了。
version: '2'
services:
liz-nginx:
image: nginx
container_name: liz.com
restart: always
ports:
- 8888:80
- 80:80
volumes:
- ./wwwroot:/usr/share/nginx/html
- ./conf/nginx.conf:/etc/nginx/nginx.conf
test-docker:
container_name: test-docker2
image: liz2019/test-docker-go-hub
restart: always
ports:
- 8020:8000
然后修改ngixn
的配置文件
user nginx;
# 指定使用 CPU 資源數量
worker_processes 1;
events {
# 連接數
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
# 指定端口
listen 80;
# 指定 IP (可以是域名)
server_name www.liz.com;
location / {
# 虛擬主機內的資源訪問路徑
root /usr/share/nginx/html;
# 首頁
index indexa.html index.htm;
}
}
server {
# 指定端口
listen 80;
# 指定 IP (可以是域名)
server_name www.liz.*;
location / {
# 虛擬主機內的資源訪問路徑
proxy_pass http://test-docker2:8000;
}
}
}
Nginx中的負載均衡
Load balancing across multiple application instances is a commonly used technique for optimizing resource utilization, maximizing throughput, reducing latency, and ensuring fault-tolerant configurations.
It is possible to use nginx as a very efficient HTTP load balancer to distribute traffic to several application servers and to improve performance, scalability and reliability of web applications with nginx.
nginx
中實現了三種的負載策略:
- 輪循(默認)
Nginx根據請求次數,將每個請求均勻分配到每台服務器 - 最少連接
將請求分配給連接數最少的服務器。Nginx會統計哪些服務器的連接數最少。 - IP Hash
綁定處理請求的服務器。第一次請求時,根據該客戶端的IP算出一個HASH值,將請求分配到集群中的某一台服務器上。后面該客戶端的所有請求,都將通過HASH算法,找到之前處理這台客戶端請求的服務器,然后將請求交給它來處理。
輪詢
upstream塊
upstream
定義了一個上游服務器的集群,便於反向代理中的proxy_pass
使用
http {
upstream myapp1 {
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
}
server
server
配置項指定了上游服務器的名字,這個名字可以是域名,IP地址端口,UNIX句柄等,在其后面也可以跟下列的參數
- weight=number:設置這台上游服務器的轉發權重,默認是1。
- max_fails=number:該選項與
fail_timeout
配合使用,指的是如果在fail_timeout
時間段內,如果向上游的服務器轉發次數超過number
,
則認為在當前的fail_timeout
時間段內這台上游服務器不可用。max_fails
的默認值是1,如果設置成0表示不檢查失敗次數。 - fail_timeout:表示該段時間內轉發失敗多少次后就認為上游服務器暫時不可用,用於優化反向代理功能。它與上游服務器建立連接的超時時間、讀取上游服務器的相應超時時間無關。
fail_timeout
默認的是10秒。 - down:表示所在的上游服務器永久下線。只在
ip_hash
才有用。 - hackup:在使用
ip_hash
配置時時它是無效的。它表示所在的上游服務器只是備份的服務器,只有在所有的非備份服務器都失效后,才會向所在的上游服務器轉發請求。
upstream myapp1 {
server srv1.example.com weight=2 max_fails=3 fail_timeout=15;
server srv2.example.com weight=3;
server srv3.example.com;
}
做個測試
version: '2'
services:
liz-nginx:
image: nginx
container_name: liz.com
restart: always
ports:
- 8888:80
- 80:80
volumes:
- ./wwwroot:/usr/share/nginx/html
- ./conf/nginx.conf:/etc/nginx/nginx.conf
test-docker1:
container_name: test-docker1
image: liz2019/test-docker-go-hub:v1.0
ports:
- 8010:8010
test-docker2:
container_name: test-docker2
image: liz2019/test-docker-go-hub:v2.0
ports:
- 8020:8020
test-docker3:
container_name: test-docker3
image: liz2019/test-docker-go-hub:v3.0
ports:
- 8030:8030
分別push
了三個版本的liz2019/test-docker-go-hub
鏡像,分別監聽不同的端口。
然后修改nginx.conf
,通過upstream
,實現三個go
項目的負載。
user nginx;
# 指定使用 CPU 資源數量
worker_processes 1;
events {
# 連接數
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream go-hub {
server test-docker1:8010;
server test-docker2:8020;
server test-docker3:8030;
}
server {
# 指定端口
listen 80;
# 指定 IP (可以是域名)
server_name www.liz.com;
location / {
# 虛擬主機內的資源訪問路徑
root /usr/share/nginx/html;
# 首頁
index indexa.html index.htm;
}
}
server {
# 指定端口
listen 80;
# 指定 IP (可以是域名)
server_name www.liz.*;
location / {
# 虛擬主機內的資源訪問路
proxy_pass http://go-hub;
}
}
}
連續請求三次,發現已經轉向了不同的服務中了,一個簡單的負載均衡就實現了。
ip_hash
當對后端的多台動態應用服務器做負載均衡時,ip_hash指令能夠將某個客戶端IP的請求通過哈希算法定位到同一台后端服務器上。這樣,當來自某個IP的用戶在后端Web服務器A上登錄后,再訪問該站點的其他URL,能夠保證其訪問的還是后端Web服務器A。
如果不采用ip_hash指令,假設來自某個IP的用戶在后端Web服務器A上登錄后,再訪問該站點的其他URL,有可能被定向到后端Web服務器B,C...上,由於用戶登錄后SESSION信息是記錄在服務器A上的,B,C...上沒有,這時就會提示用戶來登錄
upstream myapp1 {
ip_hash;
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
ip_hash
和weight
不可同時使用。如果upstream
中一台服務器不能使用了,不能直接刪除該配置。可以加上down
,確保轉發策略的一貫性。
最少連接
連接最少的. 最少連接允許在某些請求需要較長時間才能完成的情況下更公平地控制應用程序實例上的負載.
使用最少連接的負載平衡,nginx將嘗試不使繁忙的應用程序服務器因過多的請求而過載,而是將新的請求分配給不太繁忙的服務器.
upstream myapp1 {
least_conn;
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
參考
【正向代理與反向代理【總結】】https://www.cnblogs.com/anker/p/6056540.html
【使用docker-compose部署nginx】https://www.jianshu.com/p/1f6232d787d9
【使用 Docker Compose 部署 Nginx 配置虛擬主機】https://segmentfault.com/a/1190000022348558
【nginx快速入門之配置篇】https://zhuanlan.zhihu.com/p/31202053
【Nginx配置文件nginx.conf中文詳解】https://www.jianshu.com/p/3e2b9964c279
【nginx配置:server_name的作用】https://blog.csdn.net/Cheng_Kohui/article/details/82930464
【Nginx 配置常用參數,看這一篇就夠了】https://learnku.com/articles/36768
【Using nginx as HTTP load balancer】http://nginx0org.icopy.site/en/docs/http/load_balancing.html