nginx 反向代理实例
程序员文章站
2022-05-06 21:00:23
...
一、nginx 反向代理实例
1、实现效果
通过nginx跳转到tomcat主页面上
2、nginx配置 URL:http://127.0.0.1:8000/index.html
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
server {
listen 8000;
server_name localhost;
location / {
root html;
proxy_pass http://127.0.0.1:8010;
index one.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
二、nginx 反向代理 实例
1、实现效果
使用nginx反向代理,根据访问的路径跳转到不同端口的服务中
2、nginx配置 URL:http://127.0.0.1:8000/bbb/index.html
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
server {
listen 8000;
server_name localhost;
location ~ /aaa/ {
proxy_pass http://127.0.0.1:8010;
}
location ~ /bbb/ {
proxy_pass http://127.0.0.1:8013;
}
}
}
三、nginx 负载均衡配置
1、实现效果
访问一个地址,负载均衡到每个服务器下
①轮询
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
upstream tomcatAll{
server 127.0.0.1:8010;
server 127.0.0.1:8013;
}
server {
listen 8000;
server_name localhost;
location / {
proxy_pass http://tomcatAll;
}
}
}
②weight
weight 代表权重,默认为1,权证越高被分配的客户端越多
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
upstream tomcatAll{
server 127.0.0.1:8010 weight=30;
server 127.0.0.1:8013 weight=5;
}
server {
listen 8000;
server_name localhost;
location / {
proxy_pass http://tomcatAll;
}
}
}
③ip_hash
ip_hash每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
upstream tomcatAll{
ip_hash;
server 127.0.0.1:8010;
server 127.0.0.1:8013;
}
server {
listen 8000;
server_name localhost;
location / {
proxy_pass http://tomcatAll;
}
}
}
④fair
按后端服务器的响应时间来分配请求,响应时间短的优先分配
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
upstream tomcatAll{
server 127.0.0.1:8010;
server 127.0.0.1:8013;
fair;
}
server {
listen 8000;
server_name localhost;
location / {
proxy_pass http://tomcatAll;
}
}
}
上一篇: Nginx配置实例——反向代理
下一篇: mysql授权的那些事