Nginx代理服务

Nginx 作为代理服务可以实现很多的协议代理, 我们主要以 http 代理为主
正向代理(内部上⽹) 客户端<-->代理->服务端
反向代理 客户端->代理<-->服务端

代理区别
区别在于代理的对象不⼀样
正向代理代理的对象是客户端
反向代理代理的对象是服务端

Nginx代理配置语法

1. Nginx 代理配置语法
Syntax: proxy_pass URL;
Default: —
Context: location, if in location, limit_except
http://localhost:8000/uri/
http://192.168.56.11:8000/uri/
http://unix:/tmp/backend.socket:/uri/
proxy_pass 代理后⾯url加/与不加/ 区别
加/ 绝对代理代理
不加/相对路径代理
www.a.com 代理到 proxy_pass http://www.b.com 访问www.a.com/index.html 实际内容
www.b.com/index.html
www.a.com 代理到 proxy_pass http://www.b.com/ 访问www.a.com/index.html 实际内容
www.b.com/

2.类似于 nopush 缓冲区
//尽可能收集所有头请求,
Syntax: proxy_buffering on | off;
Default: 
proxy_buffering on;
Context: http, server, location
//扩展:
proxy_buffer_size
proxy_buffers
proxy_busy_buffer_size

3.跳转重定向
Syntax: proxy_redirect default;
proxy_redirect off;proxy_redirect redirect replacement;
Default: proxy_redirect default;
Context: http, server, location

proxy_redirect 是nginx中⽤于重定向代理请求的指令。在正向代理或反向代理模式下,很多情况下需要对响应中的URL进⾏修改,使得它们能够正确的映射到客户端或服务端。
常⻅的场景包括:
负载均衡:多个服务器分担相同的请求,每台服务器返回的相对地址可能不同。
URL重写:在客户端请求URI时,使⽤路径、查询字符串或⽚段修改URI。

语法:
proxy_redirect default replacement [flag];

default 参数是被替换的URL,默认情况下nginx不对在响应中出现的 default 字符串进⾏替换。 replacement
参数是指重定向⽬标URL。当响应中出现 default 字符串时,nginx将其替换为 replacement 所指定的
URL。 flag 参数是可选参数,表示对URL的重新定向⽅式,可以为以下参数之⼀。
off :表示禁⽤代理重定向,默认是 off ,即不进⾏重定向。
default :使⽤默认⽅式修改URL。默认值。
replace :修改所有出现的字符串,⽽不是仅修改⾸个出现的字符串。

例如:
proxy_redirect http://localhost:8080/ http://www.example.com/;

上述指令将响应中所有出现 http://localhost:8080/ 的字符串替换为 http://www.example.com/ 。
需要注意的是, proxy_redirect 并不能⾃动修改响应中的HTML⽂本,也不能修改响应中的JavaScript、CSS、
XML等嵌⼊式资源中的URL。如果需要修改HTML⽂本中的URL,可以使⽤ sub_filter 指令进⾏替换。

4.头信息
Syntax: proxy_set_header field value;
Default: proxy_set_header Host $proxy_host;
 proxy_set_header Connection close;
Context: http, server, location
//扩展:
proxy_hide_header
proxy_set_body

5.代理到后端的 TCP 连接超时
Syntax: proxy_connect_timeout time;
Default: proxy_connect_timeout 60s;
Context: http, server, location

//扩展
proxy_read_timeout //以及建⽴
proxy_send_timeout //服务端请求完, 发送给客户端时间

6. Proxy 常⻅配置项具体配置如下:
[root@Nginx ~]# vim /etc/nginx/proxy_params
proxy_redirect default;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;

proxy_buffer_size 32k;
proxy_buffering on;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
proxy_max_temp_file_size 256k;

//具体location实现
location / {
 proxy_pass http://127.0.0.1:8080;
 include proxy_params;
}

Nginx正向代理示例

10.1.106.70 服务器 static_server.conf
//配置10.1.106.70 访问限制,仅允许同⽹段访问
location ~ .*\.(jpg|gif|png)$ {
 allow 10.1.106.0/24;
 deny all;
 root /soft/code/images;
}

10.1.106.66 正向服务器配置
//配置正向代理
[root@Nginx ~]# cat /etc/nginx/conf.d/zy_proxy.conf
server {
 listen 80;
 resolver 10.1.106.66;
 location / {
 proxy_pass http://$http_host$request_uri;
 proxy_set_header Host $http_host;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }
}
//客户端使⽤SwitchySharp浏览器插件配置正向代理

Nginx反向代理示例

Nginx 反向代理配置实例
反向代理服务器: 10.1.106.70
/proxy代理
[root@proxy ~]# cat /etc/nginx/conf.d/proxy.conf
server {
 listen 80;
 server_name proxy.wingsredevsecops.top;
 index index.html;
 location / {
 proxy_pass http://10.1.106.66:8080;
 include proxy_params;
 }
}

服务端或者后端服务 10.1.1.06.66:8080
//WEB站点
[root@Nginx ~]# cat /etc/nginx/conf.d/backend.conf
server {
 listen 8080;
 server_name 10.1.106.66;
 index index.html;
 root /soft/code/backend;
}

Nginx负载均衡

提升吞吐率, 提升请求性能, 提⾼容灾

负载均衡按范围划分:GSLB全局负载均衡、SLB

Nginx`是⼀个典型的`SLB

负载均衡按层级划分: 分为四层负载均衡和七层负载均衡

Nginx 是⼀个典型的七层 SLB,同时也⽀持4层

Nginx负载均衡配置场景
Nginx 实现负载均衡⽤到了 proxy_pass 代理模块核⼼配置, 将客户端请求代理转发⾄⼀组 upstream 虚拟服务池

Nginx upstream 虚拟配置语法
Syntax: upstream name { ... }
Default: -
Context: http
//upstream例⼦
upstream backend {
 server backend1.example.com weight=5;
 server backend2.example.com:8080;
 server unix:/tmp/backend3;
 server backup1.example.com:8080 backup;
}
server {
 location / {
 proxy_pass http://backend;
 }
}

后端的节点 10.1.106.66
1.创建对应 html ⽂件
[root@Nginx ~]# mkdir /soft/{code1,code2,code3} -p
[root@Nginx ~]# cat > /soft/code1/index.html <<EOF
<html>
 <title> Code1</title>
 <body bgcolor="red">
 <h1> Code1-8081 </h1>
 </body>
</html>
EOF
[root@Nginx ~]# cat > /soft/code2/index.html <<EOF
<html>
 <title> Coder2</title>
 <body bgcolor="blue">
 <h1> Code1-8082</h1>
 </body>
</html>
EOF
[root@Nginx ~]# cat > /soft/code3/index.html <<EOF
<html>
 <title> Coder3</title>
 <body bgcolor="green">
 <h1> Code1-8083</h1>
 </body>
</html>
EOF

2.建⽴对应的 server.conf 配置⽂件
10.1.106.66

[root@Nginx ~]# cat > /etc/nginx/conf.d/server.conf <<EOF
server {
 listen 8081;
 root /soft/code1;
 index index.html;
}
server {
 listen 8082;
 root /soft/code2;
 index index.html;
}
server {
 listen 8083;
 root /soft/code3;
 index index.html;
}
EOF

3.配置 Nginx 反向代理
[root@Nginx ~]# cat > /etc/nginx/conf.d/upsteam_proxy.conf <<EOF
upstream node {
 server 10.1.106.66:8081;
 server 10.1.106.66:8082;
 server 10.1.106.66:8083;
}
server {
 server_name upstream.wingsredevsecops.top;
 listen 80;
 location / {
 proxy_pass http://node;
 include proxy_params;
 }
}
EOF

Nginx负载均衡状态配置

后端服务器在负载均衡调度中的状态

down 当前的server暂时不参与负载均衡
backup 预留的备份服务器
max_fails 允许请求失败的次数
fail_timeout 经过max_fails失败后, 服务暂停时间
max_conns 限制最⼤的接收连接数

backup
当⼀个后端服务器被标记为 backup 时,它将仅作为备⽤服务器使⽤。这意味着在正常情况下,Nginx 不会把请求转
发给它,除⾮所有的⾮备⽤服务器都不可⽤。如果所有的⾮备⽤服务器都不可⽤时,Nginx 才会将请求转发给
backup 服务器,让它来处理请求。
backup 的作⽤在于,当主要服务器发⽣故障或者出现⾼负载时,备⽤服务器可以代替它来处理请求,保证服务的可⽤
性和性能。

max_conns
在配置 Nginx 的 upstream 时,如果⼀个后端服务器出现了性能瓶颈,可能会导致处理速度⽐较慢,甚⾄出现负载
过⾼的情况。为了避免这种情况,可以通过限制连接数来保护后端服务器。
当某个后端服务器的 `max_conns` 属性被设置为⼀个⾮零值时,Nginx 将限制与该服务器建⽴的并发连接数量。如
果已经存在达到最⼤连接数的活动连接时,进⼀步的连接将会被阻塞或拒绝,以保护后端服务器不会被过多的请求占
⽤。
需要注意的是,`max_conns` 参数并不是⼀个硬性的限制,它只是⼀个建议值。具体的上限取决于后端服务器的实际
性能和可⽤资源。如果后端服务器的负载过⾼,可能需要考虑增加服务器的数量或者提升服务器的硬件配置来提⾼处理
能⼒。
另外,对于 Nginx 的 `max_conns` 参数,可以在 `http`、`server` 和 `location` 块中进⾏配置,以适
应不同的场景需求。

Nginx负载均衡调度策略

RR轮询 按时间顺序逐⼀分配到不同的后端服务器(默认)
weight 加权轮询,weight值越⼤,分配到的访问⼏率越⾼
ip_hash 每个请求按访问IP的hash结果分配,这样来⾃同⼀IP的固定访问⼀个后端服务器
url_hash 按照访问URL的hash结果来分配请求,是每个URL定向到同⼀个后端服务器
least_conn 最少链接数,那个机器链接数少就分发
hash关键数值 hash⾃定义的key

# Nginx负载均衡权重轮询具体配置
upstream load_pass {
 server 10.1.106.66:8081;
 server 10.1.106.66:8082 weight=5;
 server 10.1.106.66:8083;
}

# Nginx负载均衡 ip_hash 具体配置
//如果客户端都⾛相同代理, 会导致某⼀台服务器连接过多
upstream load_pass {
 ip_hash;
 server 10.1.106.66:8081;
 server 10.1.106.66:8082;
 server 10.1.106.66:8083;
}
//如果出现通过代理访问会影响后端节点接收状态均衡

# Nginx负载均衡url_hash具体配置
upstream load_pass {
 hash $request_uri;
 server 10.1.106.66:8081;
 server 10.1.106.66:8082;
 server 10.1.106.66:8083;
}

//针对三台服务器添加相同⽂件
/soft/code1/url1.html url2.html url3.html
/soft/code2/url1.html url2.html url3.html
/soft/code3/url1.html url2.html url3.html

echo "url1 8081" > /soft/code1/url1.html
echo "url2 8081" > /soft/code1/url2.html
echo "url3 8081" > /soft/code1/url3.html

echo "url1 8082" > /soft/code2/url1.html
echo "url2 8082" > /soft/code2/url2.html
echo "url3 8082" > /soft/code2/url3.html

echo "url1 8083" > /soft/code3/url1.html
echo "url2 8083" > /soft/code3/url2.html
echo "url3 8083" > /soft/code3/url3.html

Nginx负载均衡TCP配置

# Nginx 四层代理仅能存在于 main 段
stream {
 upstream ssh_proxy {
 hash $remote_addr consistent;
 server 10.1.106.66:5522;
 }
 upstream mysql_proxy {
 hash $remote_addr consistent;
 server 10.1.106.66:3306;
 }
 server {
 listen 6666;
 proxy_connect_timeout 1s;
 proxy_timeout 300s;
 proxy_pass ssh_proxy;
 }
 server {
 listen 5555;
 proxy_connect_timeout 1s;
 proxy_timeout 300s;
 proxy_pass mysql_proxy;
 }
}

# 检查是否安装stream模块
[root@180-143 conf.d]# nginx -V 2>&1 |egrep -o "\-\-with-stream"
--with-stream
--with-stream
--with-stream
--with-stream

10.1.106.66 启动mysql
docker run -p 3306:3306 --name mysql -di -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7

Nginx动静分离

动静分离,通过中间件将动态请求和静态请求进⾏分离, 分离资源, 减少不必要的请求消耗, 减少请求延时。
好处: 动静分离后, 即使动态服务不可⽤, 但静态资源不会受到影响

1.在 10.1.106.66 静态资源
[root@Nginx conf.d]# cat access.conf
server{
 listen 80;
 root /soft/code/access/;
 index index.html;
 location ~ .*\.(png|jpg|gif)$ {
 gzip on;
 root /soft/code/images;
 }
}
//准备⽬录, 以及静态相关图⽚
[root@Nginx ~]# wget -O /soft/code/images/nginx.png http://nginx.org/nginx.png

2.在 10.1.106.66 准备动态资源
[root@Nginx ~]# docker pull tomcat:9
[root@Nginx ~]# docker run -d -p8090:8080 tomcat:9
[root@Nginx soft]# docker exec -it competent_turing /bin/bash
root@28072b811532:/usr/local/tomcat# cd webapps
root@64bb55b5e1af:/usr/local/tomcat/webapps# mkdir -p ROOT
root@64bb55b5e1af:/usr/local/tomcat/webapps# cd ROOT
root@64bb55b5e1af:/usr/local/tomcat/webapps#/ROOT# cat > java_test.jsp <<EOF
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
 <HEAD>
 <TITLE>JSP Test Page</TITLE>
 </HEAD>
 <BODY>
 <%
 Random rand = new Random();
 out.println("<h1>Random number:</h1>");
 out.println(rand.nextInt(99)+100);
 %>
 </BODY>
</HTML>
EOF

4.在 10.1.106.70 配置负载均衡代理调度, 实现访问 jsp 和 png
upsteam_proxy.conf
upstream static {
 server 10.1.106.66:80;
}
upstream java {
 server 10.1.106.66:8090;
}
server {
 listen 80;
 server_name upstream.wingsredevsecops.top;
root /soft/code/access/ ;
 location / {
 root /soft/code/access/;
 index index.html;
 }
 location ~ .*\.(png|jpg|gif)$ {
 proxy_pass http://static;
 include proxy_params;
 }
 location ~ .*\.jsp$ {
 proxy_pass http://java;
 include proxy_params;
 }
}

5.在 10.1.1.06.70 proxy 代理上编写动静整合 html ⽂件
[root@Nginx ~]# cat /soft/code/access/mysite.html
<html lang="en">
<head>
 <meta charset="UTF-8" />
 <title>测试ajax和跨域访问</title>
 <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function(){
 $.ajax({
 type: "GET",
 url: "http://upstream.wingsredevsecops.top/java_test.jsp",
 success: function(data) {
 $("#get_data").html(data)
 },
 error: function() {
 alert("fail!!,请刷新再试!");
 }
 });
});
</script>
 <body>
 <h1>测试动静分离</h1>
 <img src="http://upstream.wingsredevsecops.top/nginx.png">
 <div id="get_data"></div>
 </body>
</html>

Nginx⼿机电脑应⽤案例

1.根据不同的浏览器, 以及不同的⼿机, 访问的效果都将不⼀样。
//通过浏览器来分别连接不同的浏览器访问不同的效果。
http {
...
 upstream firefox {
 server 10.1.106.66:80;
 }
 upstream chrome {
 server 10.1.106.66:8080;
 }
 upstream iphone {
 server 10.1.106.66:8080;
 }
 upstream android {
 server 10.1.106.66:8081;
 }
 upstream default {
 server 10.1.106.66:80;
 }
...
}
//server根据判断来访问不同的⻚⾯
server {
 listen 80;
 server_name www.wing.com;
 #safari浏览器访问的效果
 location / {
 if ($http_user_agent ~* "Safari"){
 proxy_pass http://dynamic_pools;
 } 
 #firefox浏览器访问效果
 if ($http_user_agent ~* "Firefox"){
 proxy_pass http://static_pools;
 }
 #chrome浏览器访问效果
 if ($http_user_agent ~* "Chrome"){
 proxy_pass http://chrome;
 }
 #iphone⼿机访问效果
 if ($http_user_agent ~* "iphone"){
 proxy_pass http://iphone;
 }
 #android⼿机访问效果
 if ($http_user_agent ~* "android"){
 proxy_pass http://and;
 }
 #其他浏览器访问默认规则
 proxy_pass http://dynamic_pools;
 include proxy.conf;
 }
 }
}
posted @ 2025-03-14 14:32  basickill  阅读(159)  评论(0)    收藏  举报