文章目录
安装YUM安装Docker安装
配置主配置文件Server配置文件Docker映射Nginx配置文件目录
反向代理正向代理概念反向代理概念创建DB和WEB容器验证DB和WEB容器基于Nginx反向代理Tomcatlocation匹配
负载均衡负载均衡策略
安装
YUM安装
参考:http://nginx.org/en/linux_packages.html#RHEL-CentOS
[root@nginx ~
]
[root@nginx ~
]
[nginx-stable
]
name
=nginx stable repo
baseurl
=http://nginx.org/packages/centos/
$releasever/
$basearch/
gpgcheck
=1
enabled
=1
gpgkey
=https://nginx.org/keys/nginx_signing.key
module_hotfixes
=true
[root@nginx ~
]
[root@nginx ~
]
Docker安装
[root@nginx ~
]
[root@nginx ~
]
[root@nginx simple-nginx
]
[root@nginx simple-nginx
]
version:
"3.8"
services:
nginx:
restart: always
image: nginx:latest
container_name: nginx01
ports:
- 80:80
[root@nginx simple-nginx
]
Starting nginx01
...
done
[root@nginx simple-nginx
]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
91630da77ad9 nginx:latest
"/docker-entrypoint.…" 54 seconds ago Up 15 seconds 0.0.0.0:80-
>80/tcp nginx01
[root@nginx simple-nginx
]
配置
主配置文件
[root@nginx ~
]
root@91630da77ad9:/
user nginx
;
worker_processes 1
;
error_log /var/log/nginx/error.log warn
;
pid /var/run/nginx.pid
;
events
{
worker_connections 1024
;
}
http
{
include /etc/nginx/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 /var/log/nginx/access.log main
;
sendfile on
;
keepalive_timeout 65
;
include /etc/nginx/conf.d/*.conf
;
}
root@91630da77ad9:/
Server配置文件
root@91630da77ad9:/
server
{
listen 80
;
listen
[::
]:80
;
server_name localhost
;
location /
{
root /usr/share/nginx/html
;
index index.html index.htm
;
}
error_page 500 502 503 504 /50x.html
;
location
= /50x.html
{
root /usr/share/nginx/html
;
}
}
root@91630da77ad9:/
Docker映射Nginx配置文件目录
[root@nginx ~
]
[root@nginx simple-nginx
]
[root@nginx simple-nginx
]
volumes:
- /opt/docker_simple_nginx/conf.d/:/etc/nginx/conf.d/
[root@nginx simple-nginx
]
Stopping nginx01
...
done
Removing nginx01
...
done
Removing network simple-nginx_default
[root@nginx simple-nginx
]
nginx uses an image, skipping
[root@nginx simple-nginx
]
Creating network
"simple-nginx_default" with the default driver
Creating nginx01
...
done
[root@nginx simple-nginx
]
反向代理
正向代理概念
正向代理:
1.正向代理服务是由客户端设立的
2.客户端了解代理服务器和目标服务器都是谁
3.帮助咱们实现突破访问权限,提高访问的速度,对目标服务器隐藏客户端的ip地址
反向代理概念
反向代理:
1.反向代理服务器是配置在服务端的
2.客户端不知道访问的到底是哪一台服务器
3.达到负载均衡,并且可以隐藏服务器真正的ip地址
创建DB和WEB容器
[root@nginx ~
]
[root@nginx ~
]
[root@nginx simple_db_web
]
[root@nginx simple_db_web
]
version:
"3.8"
services:
mysql:
restart: always
image: mysql:latest
container_name: mysql01
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: 123456
TZ: Asia/Shanghai
volumes:
- /opt/docker_simple_db_web/mysql_data:/var/lib/mysql
tomcat:
restart: always
image: tomcat:latest
container_name: tomcat01
ports:
- 8080:8080
environment:
TZ: Asia/Shanghai
volumes:
- /opt/docker_simple_db_web/tomcat_webapps:/usr/local/tomcat/webapps
- /opt/docker_simple_db_web/tomcat_logs:/usr/local/tomcat/logs
[root@nginx simple_db_web
]
Creating network
"simple_db_web_default" with the default driver
Creating mysql01
...
done
Creating tomcat01
...
done
[root@nginx simple_db_web
]
验证DB和WEB容器
[root@nginx ~
]
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "172.20.0.2",
[root@nginx ~
]
mysql
> show databases
;
+--------------------+
| Database
|
+--------------------+
| information_schema
|
| mysql
|
| performance_schema
|
| sys
|
+--------------------+
4 rows
in set (0.01 sec
)
mysql
>
[root@nginx ~
]
[root@nginx tomcat_webapps
]
[root@nginx tomcat_webapps
]
[root@nginx tomcat_webapps
]
Hello World
!
[root@nginx tomcat_webapps
]
基于Nginx反向代理Tomcat
[root@nginx ~
]
[root@nginx ~
]
server
{
listen 80
;
server_name localhost
;
location /
{
proxy_pass http://13.13.100.100:8080/
;
}
}
[root@nginx ~
]
[root@nginx simple-nginx
]
Restarting nginx01
...
done
[root@nginx ~
]
success
[root@nginx ~
]
Hello World
!
[root@nginx ~
]
location匹配
参考:https://www.jianshu.com/p/64b2cf647663
负载均衡
Nginx为我们默认提供了三种负载均衡的策略:
1.轮询:
将客户端发起的请求,平均分配给每一台服务器
2.权重:
会将客户端的请求,根据服务器的权重值不同,分配不同的数量
3.ip_hash:
基于发起请求的客户端的ip地址不同,他始终会将请求发送到指定的服务器上
就是说如果这个客户端的请求的ip地址不变,那么处理请求的服务器将一直是同一个
负载均衡策略
轮询
upstream my_server
{
server ncthz.top:8080
;
server ncthz.top:8081
;
}
server
{
listen 80
;
listen
[::
]:80
;
server_name localhost
;
location /
{
proxy_pass http://my_server/
;
}
}
权重
upstream my_server
{
server ncthz.top:8080 weight
=10
;
server ncthz.top:8081 weight
=2
;
}
server
{
listen 80
;
listen
[::
]:80
;
server_name localhost
;
location /
{
proxy_pass http://my_server/
;
}
}
IP_Hash
upstream my_server
{
ip_hash
;
server ncthz.top:8080
;
server ncthz.top:8081
;
}
server
{
listen 80
;
listen
[::
]:80
;
server_name localhost
;
location /
{
proxy_pass http://my_server/
;
}
}