一、引言
1.1 代理问题
客户端到底要将请求发送给哪台服务器。
发送给服务器1还是服务器2 |
---|
image.png |
1.2 负载均衡问题
如果所有客户端的请求都发送给了服务器1,那么服务器2将没有任何意义
负载均衡问题 |
---|
image.png |
1.3 资源优化
客户端发送的请求可能是申请动态资源的,也有申请静态资源,但是都是去Tomcat中获取的
静态资源访问 |
---|
image.png |
1.4 Nginx处理
在搭建集群后,使用Nginx |
---|
image.png |
二、Nginx概述
Nginx是由俄罗斯人研发的,应对Rambler的网站,并且2004年发布的第一个版本。
Nginx之父 |
---|
image.png |
Nginx的特点:
- 稳定性极强。 7*24小时不间断运行。
- Nginx提供了非常丰富的配置实例。
- 占用内存小,并发能力强。
三、Nginx的安装
3.1 安装Nginx
使用Docker-Compose安装,创建docker-compose.yml写入以下内容:
version: '3.1'
services:
nginx:
restart: always
image: daocloud.io/library/nginx:latest
container_name: nginx
ports:
- 80:80
[root@iz8vbdmrir2n6xqzrbd93hz ~]# cd /opt
[root@iz8vbdmrir2n6xqzrbd93hz opt]# ls
containerd docker_mysql_tomcat docker_ssm yangl
[root@iz8vbdmrir2n6xqzrbd93hz opt]# mkdir docker_nginx
[root@iz8vbdmrir2n6xqzrbd93hz opt]# cd docker_nginx/
[root@iz8vbdmrir2n6xqzrbd93hz docker_nginx]# vim docker-compose.yml
[root@iz8vbdmrir2n6xqzrbd93hz docker_nginx]# docker-compose-Linux-x86_64 up -d
Creating network "docker_nginx_default" with the default driver
Pulling nginx (daocloud.io/library/nginx:latest)...
latest: Pulling from library/nginx
6ec8c9369e08: Pull complete
d3cb09a117e5: Pull complete
7ef2f1459687: Pull complete
e4d1bf8c9482: Pull complete
795301d236d7: Pull complete
Digest: sha256:deb724a427ea79face617392a5a471fdcb4cdb57f971ee6b7e492b90fecb199f
Status: Downloaded newer image for daocloud.io/library/nginx:latest
Creating nginx ... done
测试:浏览器访问即可(80端口可以省略不写,云服务器记得去开放80端口)
3.2 Nginx的配置文件
关于Nginx的核心配置文件nginx.conf
[root@iz8vbdmrir2n6xqzrbd93hz docker_nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4d8af14af5cb daocloud.io/library/nginx:latest "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 0.0.0.0:80->80/tcp nginx
[root@iz8vbdmrir2n6xqzrbd93hz docker_nginx]# docker exec -it 4d8 bash
root@4d8af14af5cb:/# cd /etc/nginx
root@4d8af14af5cb:/etc/nginx# ls
conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf
root@4d8af14af5cb:/etc/nginx# cat nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
# 参数说明
# 以上统称为全局块,
# worker_processes的数值越大,Nginx的并发能力就越强(由运维人员修改)
# error_log 代表Nginx的错误日志存放的位置
events {
worker_connections 1024;
}
# 参数说明
# events块
# worker_connections的数值越大,Nginx的并发能力就越强(由运维人员修改)
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;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
# 参数说明
# http块
# include代表引入一个外部的文件 -> /mime.types中放着大量的媒体类型
# include /etc/nginx/conf.d/*.conf; -> 引入了conf.d目录下的以.conf为结尾的配置文件
root@4d8af14af5cb:/etc/nginx# ls
conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf
root@4d8af14af5cb:/etc/nginx# cd conf.d/
root@4d8af14af5cb:/etc/nginx/conf.d# ls
default.conf
root@4d8af14af5cb:/etc/nginx/conf.d# cat default.conf
# 参数说明
# server块
# listen: 代表Nginx监听的端口号
# localhost:代表Nginx接收请求的ip
server {
listen 80;
listen [::]:80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# 参数说明
# location块
# root:将接收到的请求根据/usr/share/nginx/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 /usr/share/nginx/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;
#}
}
3.3 修改docker-compose文件
为了方便修改Nginx配置,修改docker-compose.yml文件,设置 volumes ,重新启动
version: '3.1'
services:
nginx:
restart: always
image: daocloud.io/library/nginx:latest
container_name: nginx
ports:
- 80:80
volumes:
- /opt/docker_nginx/conf.d/:/etc/nginx/conf.d
root@4d8af14af5cb:/etc/nginx/conf.d# exit
exit
[root@iz8vbdmrir2n6xqzrbd93hz docker_nginx]# ls
docker-compose.yml
[root@iz8vbdmrir2n6xqzrbd93hz docker_nginx]# docker-compose-Linux-x86_64 down
Stopping nginx ... done
Removing nginx ... done
Removing network docker_nginx_default
[root@iz8vbdmrir2n6xqzrbd93hz docker_nginx]# vim docker-compose.yml
[root@iz8vbdmrir2n6xqzrbd93hz docker_nginx]# docker-compose-Linux-x86_64 build
nginx uses an image, skipping
[root@iz8vbdmrir2n6xqzrbd93hz docker_nginx]# docker-compose-Linux-x86_64 up -d
Creating network "docker_nginx_default" with the default driver
Creating nginx ... done
[root@iz8vbdmrir2n6xqzrbd93hz docker_nginx]# ls
conf.d docker-compose.yml
此时再使用浏览器访问则无法访问nginx.html页面了,此时需要在conf.d目录下创建default.conf文件,配置一个server,再重新启动即可
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
[root@iz8vbdmrir2n6xqzrbd93hz docker_nginx]# cd conf.d/
[root@iz8vbdmrir2n6xqzrbd93hz conf.d]# ls
[root@iz8vbdmrir2n6xqzrbd93hz conf.d]# vim default.conf
[root@iz8vbdmrir2n6xqzrbd93hz conf.d]# cd ..
[root@iz8vbdmrir2n6xqzrbd93hz docker_nginx]# docker-compose-Linux-x86_64 restart
Restarting nginx ... done
四、Nginx的反向代理【重点
】
4.1 正向代理和反向代理介绍
正向代理:
- 正向代理服务是由客户端设立的。
- 客户端了解代理服务器和目标服务器都是谁。
- 帮助咱们实现突破访问权限,提高访问的速度,对目标服务器隐藏客户端的ip地址。
正向代理 |
---|
image.png |
image.png反向代理:
- 反向代理服务器是配置在服务端的。
- 客户端是不知道访问的到底是哪一台服务器。
- 达到负载均衡,并且可以隐藏服务器真正的ip地址。
4.2 基于Nginx实现反向代理
准备一个目标服务器,启动了之前的tomcat服务器,使其能够访问到页面
[root@iz8vbdmrir2n6xqzrbd93hz docker_nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
818cd6a46ecb daocloud.io/library/nginx:latest "/docker-entrypoint.…" 3 hours ago Up 40 minutes 0.0.0.0:80->80/tcp nginx
[root@iz8vbdmrir2n6xqzrbd93hz docker_nginx]# cd ..
[root@iz8vbdmrir2n6xqzrbd93hz opt]# ls
containerd docker_mysql_tomcat docker_nginx docker_ssm yangl
[root@iz8vbdmrir2n6xqzrbd93hz opt]# cd docker_mysql_tomcat/
[root@iz8vbdmrir2n6xqzrbd93hz docker_mysql_tomcat]# ls
docker-compose.yml mysql_data tomcat_logs tomcat_webapps
[root@iz8vbdmrir2n6xqzrbd93hz docker_mysql_tomcat]# cd tomcat_webapps/
[root@iz8vbdmrir2n6xqzrbd93hz tomcat_webapps]# ls
[root@iz8vbdmrir2n6xqzrbd93hz tomcat_webapps]# mkdir ROOT
[root@iz8vbdmrir2n6xqzrbd93hz tomcat_webapps]# cd ROOT/
[root@iz8vbdmrir2n6xqzrbd93hz ROOT]# vim index.html
[root@iz8vbdmrir2n6xqzrbd93hz ROOT]# cd ..
[root@iz8vbdmrir2n6xqzrbd93hz tomcat_webapps]# cd ..
[root@iz8vbdmrir2n6xqzrbd93hz docker_mysql_tomcat]# docker-compose-Linux-x86_64 up -d
Starting tomcat ... done
Starting mysql ... done
然后修改nginx的default.conf配置文件,使其通过Nginx访问到tomcat服务器。
server {
listen 80;
listen [::]:80;
server_name localhost;
# 基于反向代理访问到Tomcat服务器
location / {
proxy_pass http://39.98.132.196:8080/;
}
}
[root@iz8vbdmrir2n6xqzrbd93hz docker_mysql_tomcat]# cd ..
[root@iz8vbdmrir2n6xqzrbd93hz opt]# ls
containerd docker_mysql_tomcat docker_nginx docker_ssm yangl
[root@iz8vbdmrir2n6xqzrbd93hz opt]# cd docker_nginx/
[root@iz8vbdmrir2n6xqzrbd93hz docker_nginx]# ls
conf.d docker-compose.yml
[root@iz8vbdmrir2n6xqzrbd93hz docker_nginx]# cd conf.d/
[root@iz8vbdmrir2n6xqzrbd93hz conf.d]# ls
default.conf
[root@iz8vbdmrir2n6xqzrbd93hz conf.d]# vim default.conf
[root@iz8vbdmrir2n6xqzrbd93hz docker_nginx]# docker-compose-Linux-x86_64 restart
Restarting nginx ... done
4.3 关于Nginx的location路径映射
优先级关系如下:
# 1. 直接匹配
location = / {
# 精准匹配,主机名后面不能带任何的字符串
}
# 2. 通用匹配
location /xxx {
# 匹配所有以/xxx开头的路径
}
# 3. 正则匹配
location ~ /xxx {
# 匹配所有以/xxx开头的路径
}
# 4. 匹配开头路径
location ^~ /images/ {
# 匹配所有以/images开头的路径
}
# 5. 匹配后缀
location ~* \.(gif|jpg|png)$ {
# 匹配以gif或者jpg或者png为结尾的路径
}
# 6. 全部通配
location / {
# 匹配全部路径
}
网友评论