Nginx 配置反向代理、负载均衡
2023-09-27 14:29:28 时间
概述
反向代理服务器架设在服务器端,通过缓冲经常被请求的页面来缓解服务器的工作量,将客户机请求转发给内部网络上的目标服务器;并将从服务器上得到的结果返回给 Internet 上请求连接的客户端,此时代理服务器与目标主机一起对外表现为一个服务器。
反向代理主要应用场景
许多大型 web 网站都用到反向代理。除了可以防止外网对内网服务器的恶性攻击、缓存以减少服务器的压力和访问安全控制之外,还可以进行负载均衡,将用户请求分配给多个服务器。
使用 Nginx
反向代理 Tomcat
我们使用 Docker 来安装和运行 Nginx,docker-compose.yml
配置如下:
version: '3.1'
services:
nginx:
restart: always
image: nginx
container_name: nginx
ports:
- 80:80
volumes:
- ./conf/nginx.conf:/etc/nginx/nginx.conf
- ./wwwroot:/usr/share/nginx/wwwroot
启动 Tomcat
容器
启动两个 Tomcat
容器,映射端口为 9090
和 9091
,docker-compose.yml
如下:
version: '3'
services:
tomcat1:
image: tomcat
container_name: tomcat1
ports:
- 9090:8080
tomcat2:
image: tomcat
container_name: tomcat2
ports:
- 9091:8080
配置 Nginx
反向代理
修改 /usr/local/docker/nginx/conf
目录下的 nginx.conf
配置文件:
user nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# 配置一个代理即 tomcat1 服务器
upstream tomcatServer1 {
server 192.168.50.136:9090;
}
# 配置一个代理即 tomcat2 服务器
upstream tomcatServer2 {
server 192.168.50.136:9091;
}
# 配置一个虚拟主机
server {
listen 80;
server_name tomcat1.myapp.com;
location / {
# 域名 tomcat1.myapp.com 的请求全部转发到 tomcatServer1 即 tomcat1 服务上
proxy_pass http://tomcatServer1;
# 欢迎页面,按照从左到右的顺序查找页面
index index.jsp index.html index.htm;
}
}
server {
listen 80;
server_name tomcat2.myapp.com;
location / {
# 域名 tomcat2.myapp.com 的请求全部转发到 tomcatServer2 即 tomcat2 服务上
proxy_pass http://tomcatServer2;
index index.jsp index.html index.htm;
}
}
}
注意:新版 Nginx 的 upstream
配置中的名称不可以有下划线(_
),否则会报 400
错误
配置 Nginx
负载均衡
修改 /usr/local/docker/nginx/conf
目录下的 nginx.conf
配置文件:
user nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# 配置代理
upstream tomcatServer {
server 192.168.50.136:9090 weight=10;
server 192.168.50.136:9091 weight=10;
}
# 配置一个虚拟主机
server {
listen 80;
server_name tomcat.myapp.com;
location / {
# 域名 tomcat.myapp.com 的请求全部转发到 tomcatServer 服务上
proxy_pass http://tomcatServer;
# 欢迎页面,按照从左到右的顺序查找页面
index index.jsp index.html index.htm;
}
}
}
相关文章
- Confluence 6 配置 简易信息聚合(RSS)
- Centos环境下手动设置-网络参数配置-网络挨排错顺序-设置网卡为上网模式的设定
- CentOS 配置使用 EPEL YUM 源
- spring 配置
- nginx配置反向代理
- 在idea中配置 gitignore忽略文件不起作用(三)
- react配置proxy代理的两种方式
- Linux 下 Nginx 反向代理 负载均衡配置
- tengine、nginx配置正向代理,其他内网机器通过代理访问外网,支持https
- centos7 开启端口防火墙配置(如开启3306或者80端口)
- Nginx反向代理和负载均衡——个人配置
- idea 和 WebStorm 配置 http代理 并更换主题
- YARP+AgileConfig 5分钟实现一个支持配置热更新的代理网关
- 【未解决】根据torch版本和cuda版本等来配置安装 torch_geometric工具包 Try using the full path with constructor syntax.
- 【DevOps】docker安装nexus3,配置nginx代理(更新于2022.04.30)
- 【Nginx】第十六节 配置反向代理
- Nginx反向代理和负载均衡的配置
- 配置免安装版JAVA1.7的环境变量
- nginx:配置简单的代理&root和alias区别
- .NET Core 2.0 Console(控制台)项目 Microsoft.Extensions.Logging NLog配置使用
- linux之vim配置
- nginx代理配置
- Mybatis非mapper代理配置
- 如何在Win10系统中正确配置WinDbg?WinDbg配置方法说明
- nginx常用代理配置
- Nginx 负载均衡配置和策略
- 如何在 CentOS 7 用 cPanel 配置 Nginx 反向代理