运维(27)-部署流量代理(Nginx+haproxy)

简介: 运维(27)-部署流量代理(Nginx+haproxy)

1. nginx

1.1 nginx安装

step1:查询当前可用版本

apt-cache madison nginx-full

step2:安装nginx

apt install -y nginx-full

1.2 配置nginx

步骤:

  • 修改nginx的主配置文件/etc/nginx/nginx.conf
  • worker_connections修改为 4096
  • http 模块,增加 real_ip 相关设置
  • 去掉server_names_hash_bucket_size、server_tokens、gzip_buffers、gzip_types 等行首原来的#注释
  • 增加 log_format配置。

nginx.conf示例如下:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
        worker_connections 4096;
        # multi_accept on;
}
http {
        ##
        # Basic Settings
        ##
        real_ip_header proxy_protocol;
        set_real_ip_from 10.0.0.0/8;
        set_real_ip_from 127.0.0.0/8;
        set_real_ip_from 172.16.0.0/12;
        set_real_ip_from 169.254.0.0/16;
        set_real_ip_from 192.168.0.0/16;
        set_real_ip_from 224.0.0.0/4;
        set_real_ip_from 240.0.0.0/4;
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        server_tokens off;
        server_names_hash_bucket_size 64;
        # server_name_in_redirect off;
        include /etc/nginx/mime.types;
        default_type application/octet-stream;
        ##
        # SSL Settings
        ##
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;
        ##
        # Logging Settings
        ##
        log_format  main  '$http_host $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;
        error_log /var/log/nginx/error.log;
        ##
        # Gzip Settings
        ##
        gzip on;
        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript ;
        ##
        # Virtual Host Configs
        ##
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

1.3 验证重启

保存上述配置文件并退出编辑,测试配置的正确性,确认没有问题后,重载Nginx服务:

① .测试nginx配置文件

nginx -t

② .若上一步结果显示 successful ,则重载nginx服务件

systemctl reload nginx.service

1.4 配置默认路由

步骤:

  • 进入 /etc/nginx/sites-enabled/目录
  • 删除 default文件
  • 重新创建 default404 文件

default404 文件内容如下:

server{
    listen 80 default_server;
    server_name  _;
    return       404;
}
server{
    listen 443 ssl default_server;
    server_name _;
    return 404;
    ssl_certificate            /var/http-ssl/xxx.crt;
    ssl_certificate_key        /var/http-ssl/xxx.key;
    ssl_session_cache          shared:SSL:1m;
    ssl_session_timeout        5m;
    ssl_ciphers                HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;
}

执行测试配置:

nginx -t

1.5 配置状态监听

/etc/nginx/sites-enabled/目录,创建 status 文件,内容如下:

server {
  listen 127.0.0.1:18080 default_server;
  access_log    off;
  location / {return 404;}
  location /nginx_status {
    stub_status  on;
  }
}

执行测试配置:

nginx -t

1.6 配置域名、证书和业务路由

/etc/nginx/sites-enabled/目录,创建 demo.xxx.cn 文件,内容如下:

server {
    listen       80;
    server_name  demo.xxx.cn;
    location ^~ /{
        return 301 https://demo.xxx.cn$uri;
    }
}
map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}
server {
    listen 443 ssl http2;
    server_name demo.xxx.cn;
    add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
    client_max_body_size 100M;
    ssl_certificate            /var/http-ssl/1_demo.xxx.cn_bundle.crt;
    ssl_certificate_key        /var/http-ssl/2_demo.xxx.cn.key;
    ssl_session_cache          shared:SSL:1m;
    ssl_session_timeout        5m;
    ssl_ciphers                HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;
    location / {
        return 301 /project-pc/;
        #default_type text/plain;
        #return 403 ;
    }
    # 静态验证
    location /MP_verify_fxxx.txt {
        return 200 "fxxx";
    }
    location /MP_verify_lxxx.txt {
        return 200 "lxxx";
    }
    location /WW_verify_Axxx.txt {
        default_type text/plain;
        return 200 "Axxx";
    }
    location /rbac/ {
        proxy_set_header  Upgrade $http_upgrade;
        proxy_set_header  Connection $connection_upgrade;
        proxy_set_header  Host $host;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Host $host;
        proxy_set_header  X-Forwarded-Proto https;
        proxy_pass        http://127.0.0.1:39401;
    }
    location /idm/ {
        proxy_set_header  Upgrade $http_upgrade;
        proxy_set_header  Connection $connection_upgrade;
        proxy_set_header  Host $host;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Host $host;
        proxy_set_header  X-Forwarded-Proto https;
        proxy_pass        http://127.0.0.1:39402;
    }
    location /sso/ {
        proxy_set_header  Host $host;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Host $host;
        proxy_set_header  X-Forwarded-Proto https;
        proxy_pass        http://127.0.0.1:39974;
    }
    # 普通静态,转发到根路径
    location /idm-web/ {
        proxy_set_header  Upgrade $http_upgrade;
        proxy_set_header  Connection $connection_upgrade;
        proxy_set_header  Host $host;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Host $host;
        proxy_set_header  X-Forwarded-Proto https;
        proxy_pass        http://127.0.0.1:39403/;
    }
    location /rbac-web/ {
        proxy_set_header  Upgrade $http_upgrade;
        proxy_set_header  Connection $connection_upgrade;
        proxy_set_header  Host $host;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Host $host;
        proxy_set_header  X-Forwarded-Proto https;
        proxy_pass        http://127.0.0.1:39404/;
    }
    # api转发
    location ~ ^/api-[a-z\-]*/ {
        proxy_set_header  Upgrade $http_upgrade;
        proxy_set_header  Connection $connection_upgrade;
        proxy_set_header  Host $host;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $remote_addr;
        proxy_set_header  X-Forwarded-Host $host;
        proxy_set_header  X-Forwarded-Proto https;
        proxy_pass        http://127.0.0.1:40001;
    }
    #web-basic
    location /basic/ {
        proxy_set_header  Host $host;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $remote_addr;
        proxy_set_header  X-Forwarded-Host $host;
        proxy_pass http://127.0.0.1:40017/;
    }
}

02 haproxy安装配置

2.1 安装haproxy

查询当前可用版本:

apt-cache madison haproxy

安装nginx:

apt install -y haproxy

2.2 配置haproxy

修改haproxy配置文件 /etc/haproxy/haproxy.cfg ,示例如下:

global
  log /dev/log  local0
  log /dev/log  local1 notice
  chroot /var/lib/haproxy
  stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
  stats socket 127.0.0.1:14567
  stats timeout 30s
  user haproxy
  group haproxy
  daemon
  # Default SSL material locations
  ca-base /etc/ssl/certs
  crt-base /etc/ssl/private
  # See: https://ssl-config.mozilla.org/#server=haproxy&server-version=2.0.3&config=intermediate
        ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
        ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
        ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets
defaults
        option  dontlognull
        timeout connect 5s
        timeout client  120s
        timeout server  120s
listen stats
    bind 0.0.0.0:8181
    mode http
    stats enable
    stats hide-version
    stats uri /
listen swarm-dashboard
    bind 127.0.0.1:8080
    mode tcp
    option      tcp-check
    balance     roundrobin
    server swarm-dashboard_b_1 172.16.3.8:8080 check inter 2000 rise 2 fall 3
    server swarm-dashboard_b_2 172.16.3.9:8080 check inter 2000 rise 2 fall 3
    server swarm-dashboard_b_3 172.16.3.10:8080 check inter 2000 rise 2 fall 3
listen nacos
    bind 127.0.0.1:8848
    mode tcp
    option      tcp-check
    balance     roundrobin
    server nacos_b_1 172.16.3.8:8848 check inter 2000 rise 2 fall 3
    server nacos_b_2 172.16.3.9:8848 check inter 2000 rise 2 fall 3
    server nacos_b_3 172.16.3.10:8848 check inter 2000 rise 2 fall 3
listen idm
    bind 127.0.0.1:39402
    mode tcp
    option      tcp-check
    balance     roundrobin
    server idm_1 172.16.3.8:39402 check inter 2000 rise 2 fall 3
    server idm_2 172.16.3.9:39402 check inter 2000 rise 2 fall 3
    server idm_3 172.16.3.10:39402 check inter 2000 rise 2 fall 3
listen iam-web
    bind 127.0.0.1:39403
    mode tcp
    option      tcp-check
    balance     roundrobin
    server iam-web_1 172.16.3.8:39403 check inter 2000 rise 2 fall 3
    server iam-web_2 172.16.3.9:39403 check inter 2000 rise 2 fall 3
    server iam-web_3 172.16.3.10:39403 check inter 2000 rise 2 fall 3
listen rbac
    bind 127.0.0.1:39401
    mode tcp
    option      tcp-check
    balance     roundrobin
    server rbac_1 172.16.3.8:39401 check inter 2000 rise 2 fall 3
    server rbac_2 172.16.3.9:39401 check inter 2000 rise 2 fall 3
    server rbac_3 172.16.3.10:39401 check inter 2000 rise 2 fall 3
listen rbac-web
    bind 127.0.0.1:39404
    mode tcp
    option      tcp-check
    balance     roundrobin
    server rbac-web_1 172.16.3.8:39404 check inter 2000 rise 2 fall 3
    server rbac-web_2 172.16.3.9:39404 check inter 2000 rise 2 fall 3
    server rbac-web_3 172.16.3.10:39404 check inter 2000 rise 2 fall 3
listen sso
    bind 127.0.0.1:39974
    mode tcp
    option      tcp-check
    balance     roundrobin
    server sso_b_1 172.16.3.8:39974 check inter 2000 rise 2 fall 3
    server sso_b_2 172.16.3.9:39974 check inter 2000 rise 2 fall 3
    server sso_b_3 172.16.3.10:39974 check inter 2000 rise 2 fall 3

2.3 验证haproxy

保存上述配置文件并退出编辑,测试配置的正确性,确认没有问题后,重启服务:

测试配置文件是否有问题:

haproxy -c -f /etc/haproxy/haproxy.cfg

若上一步结果显示Configuration file is valid ,则重启服务:

systemctl restart haproxy.service


目录
相关文章
|
6天前
|
监控 应用服务中间件 测试技术
确保正则表达式在 Nginx 代理中的准确性和稳定性
【10月更文挑战第19天】总之,正则表达式在 Nginx 代理中具有重要作用,但要确保其准确性和稳定性需要付出一定的努力和关注。通过以上方法的综合运用,我们可以提高正则表达式配置的可靠性,为用户提供更好的服务体验。
|
20天前
|
负载均衡 应用服务中间件 Linux
nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件,很全
这篇博客文章详细介绍了Nginx的下载、安装、配置以及使用,包括正向代理、反向代理、负载均衡、动静分离等高级功能,并通过具体实例讲解了如何进行配置。
88 4
nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件,很全
|
6天前
|
应用服务中间件 API nginx
使用正则表达式实现 Nginx 代理
【10月更文挑战第19天】在不断发展的互联网技术中,掌握正则表达式在 Nginx 代理中的应用是非常重要的。不断探索和实践,将有助于我们在实际工作中更好地运用这一技术,提升项目的质量和效率。
|
6天前
|
缓存 负载均衡 应用服务中间件
Nginx 实现一个端口代理多个前后端服务
【10月更文挑战第19天】Nginx 的强大功能不仅限于此,它还可以与其他技术和工具相结合,为我们的应用提供更强大的支持和保障。在不断发展的互联网时代,掌握 Nginx 的使用技巧将为我们的工作和生活带来更多的便利和效益。
|
24天前
|
存储 缓存 监控
|
24天前
|
存储 缓存 负载均衡
Nginx代理缓存机制
【10月更文挑战第2天】
55 4
|
24天前
|
前端开发 JavaScript 应用服务中间件
使用nginx部署网站
使用nginx部署网站
|
25天前
|
JavaScript 应用服务中间件 nginx
nginx部署vue项目
本文介绍了将Vue项目部署到Nginx的步骤,包括构建Vue项目、上传dist文件夹到服务器、安装Nginx、配置Nginx代理静态文件以及重启Nginx,确保了Vue应用可以通过域名或IP地址访问。
59 1
|
27天前
|
前端开发 JavaScript 应用服务中间件
linux安装nginx和前端部署vue项目(实际测试react项目也可以)
本文是一篇详细的教程,介绍了如何在Linux系统上安装和配置nginx,以及如何将打包好的前端项目(如Vue或React)上传和部署到服务器上,包括了常见的错误处理方法。
70 0
linux安装nginx和前端部署vue项目(实际测试react项目也可以)
|
28天前
|
Kubernetes 应用服务中间件 nginx
k8s基础使用--使用k8s部署nginx服务
本文介绍了Kubernetes中核心概念Deployment、Pod与Service的基本原理及应用。Pod作为最小调度单元,用于管理容器及其共享资源;Deployment则负责控制Pod副本数量,确保其符合预期状态;Service通过标签选择器实现Pod服务的负载均衡与暴露。此外,还提供了具体操作步骤,如通过`kubectl`命令创建Deployment和Service,以及如何验证其功能。实验环境包括一台master节点和两台worker节点,均已部署k8s-1.27。
109 1