重庆思庄Oracle、Redhat认证学习论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 127|回复: 0
打印 上一主题 下一主题

Nginx和Haproxy端口复用配置和容器镜像构建

[复制链接]
跳转到指定楼层
楼主
发表于 2024-12-2 22:45:48 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 梅钟园 于 2024-12-2 23:10 编辑

背景:
  最近网站打算使用SSL证书,曾经为 RH124、RH134、RH294、RH358、CL260、DO374 等环境准备的联网更新脚本仅支持 HTTP 协议,重构全部环境不太现实(当初未考虑SSL)。
  现在为兼容 HTTP 和 HTTPS,服务提供端决定使用端口复用以同时支持 HTTP 和 HTTPS,故而有此处内容分享。
  此处仅分享核心配置,是经过验证能够使用的配置,具体细节不作展示,有需要可以随时与我交流。

一、端口复用
  简单来说,同一端口根据请求协议(四层或七层)转发至不同后端服务,以实现根据不同协议访问同一端口时,能够到达不同后端服务。例如 18080/tcp 端口同时支持 http 和 https 访问。
  使用典型的 Nginx 和 Haproxy 均可以实现。

二、构建镜像
  此处使用Docker将所有业务组件均构建为容器,便于后续升级。此处默认读者已经熟悉容器技术、Dockerfile、docker-compose、Compose Files,下列仅列出核心配置。

base images:
  从官方镜像构建自定义基础镜像,必要时可以从其他地方编译安装以植入更多模块(如Nginx模块)。
  nginx-base:1.27.2 from nginx:1.27.2
  haproxy-base:3.0.6 from haproxy:3.0.6
示例:nginx-base:1.27.2 Dockerfile 如下;根据自己需要,改时区,安装小工具等。
可以再创建一个
  1. FROM nginx:1.27.2
  2. ENV TZ="Asia/Shanghai"
  3. RUN mkdir -p /etc/nginx/certs.d ; \
  4.   rm -rf /etc/localtime ; \
  5.   ln -sv /usr/share/zoneinfo/Asia/shanghai /etc/localtime ; \
  6.   apt update && apt install -y iproute2 lsof telnet traceroute tree curl vim && \
  7.   apt-get clean && rm -rf /var/lib/apt/lists/*
  8. CMD ["nginx","-g","daemon off;"]
复制代码
None 镜像清理
  1. docker rmi $(docker images -f 'dangling=true' -q)
复制代码

apps images:
  从自定义的基础镜像再次构建应用镜像。
  nginx-webapp:n1.27.2-v1.0 from nginx-base:1.27.2
  haproxy-balances:h3.0.6-v1.0 from haproxy-balances:3.0.6
  nginx-balances:n1.27.2-v1.0 from nginx-base:1.27.2

nginx-webapp:n1.27.2-v1.0 from nginx-base:1.27.2
nginx-webapp:n1.27.2-v1.0 镜像构建材料(建议配置文件和证书通过挂载方式使用,应用镜像只构建网站源代码即可,不要包含太多其他信息,更不要包含机密信息
  说明:主要构建网站源代码,nginx-webapp 将同时支持 80/tcp HTTP 访问,443/tcp HTTPS 访问,也可以分成两个镜像分别支持 HTTP 和 HTTPS。
  重要配置:nginx-webapp 启用 proxy_protocol ,并且配置允许从上游代理获取源IP。这一配置的上游代理服务器必须也同样启用这一功能,如上游代理为 nginx,则必须也启用 proxy_protocol,若为 Haproxy,必须 send_proxy。
                  否则整体网站无法访问。
  1. nginx-webapp/
  2. ├── Dockerfile
  3. ├── config-nginx
  4. │   ├── conf.d
  5. │   │   └── vhost.conf
  6. │   └── nginx.conf
  7. └── html
复制代码
nginx.conf -> 容器 /etc/nginx/nginx.conf
  1. user  nginx;
  2. worker_processes  auto;

  3. error_log  /var/log/nginx/error.log notice;
  4. pid        /var/run/nginx.pid;


  5. events {
  6.     worker_connections  1024;
  7. }

  8. http {
  9.     include       /etc/nginx/mime.types;
  10.     default_type  application/octet-stream;

  11.     log_format  main  '$proxy_protocol_addr - $remote_addr - $remote_user [$time_local] "$request" '
  12.                       '$status $body_bytes_sent "$http_referer" '
  13.                       '"$http_user_agent" "$http_x_forwarded_for"';

  14.     access_log  /var/log/nginx/access.log  main;

  15.     sendfile        on;
  16.     #tcp_nopush     on;

  17.     keepalive_timeout  65;

  18.     # 压缩设置
  19.     gzip on;
  20.     gzip_disable "msie6";
  21.     gzip_vary on;
  22.     gzip_proxied any;
  23.     gzip_comp_level 6;
  24.     gzip_buffers 16 8k;
  25.     gzip_types text/plain text/css application/javascript application/json application/xml application/rss+xml image/svg+xml;
  26.     map $status $status_name {
  27.         100 "Continue";
  28.         101 "Switching Protocols";
  29.         102 "Processing";
  30.         103 "Early Hints";
  31.         200 "OK";
  32.         201 "Created";
  33.         202 "Accepted";
  34.         203 "Non-Authoritative Information";
  35.         204 "No Content";
  36.         205 "Reset Content";
  37.         206 "Partial Content";
  38.         300 "Multiple Choices";
  39.         301 "Moved Permanently";
  40.         302 "Found";
  41.         303 "See Other";
  42.         304 "Not Modified";
  43.         305 "Use Proxy";
  44.         307 "Temporary Redirect";
  45.         308 "Permanent Redirect";
  46.         400 "Bad Request";
  47.         401 "Unauthorized";
  48.         403 "Forbidden";
  49.         404 "Not Found";
  50.         405 "Method Not Allowed";
  51.         406 "Not Acceptable";
  52.         407 "Proxy Authentication Required";
  53.         408 "Request Timeout";
  54.         409 "Conflict";
  55.         410 "Gone";
  56.         411 "Length Required";
  57.         412 "Precondition Failed";
  58.         413 "Payload Too Large";
  59.         414 "URI Too Long";
  60.         415 "Unsupported Media Type";
  61.         416 "Range Not Satisfiable";
  62.         417 "Expectation Failed";
  63.         418 "I'm a teapot";
  64.         429 "Too Many Requests";
  65.         500 "Internal Server Error";
  66.         501 "Not Implemented";
  67.         502 "Bad Gateway";
  68.         503 "Service Unavailable";
  69.         504 "Gateway Timeout";
  70.         505 "HTTP Version Not Supported";
  71.         default "Unknown";
  72.     }

  73.     include /etc/nginx/conf.d/*.conf;
  74. }
复制代码
conf.d/vhost.conf -> 容器 /etc/nginx/conf.d/vhost.conf
  1. server {
  2.     listen       80 <font color="#ff0000">proxy_protocol</font>;
  3.     server_name nginx-webapp;
  4.     root /usr/share/nginx/html;
  5.     access_log /var/log/nginx/access_backend_http.log main;
  6.     error_log /var/log/nginx/error_backend.log warn;
  7.     # proxy_protocol docs: https://nginx.org/en/docs/stream/ngx_stream_realip_module.html

  8.     # 重写策略 特殊链接同时支持 http 和 https
  9.     location / {
  10.         root /usr/share/nginx/html;
  11.         index  index.html index.htm;
  12.     }
  13.     # 获取源 IP 配置 适用于代理负载均衡器 CDN 环境
  14.     set_real_ip_from 172.16.0.0/16;
  15.     set_real_ip_from 172.17.0.0/16;
  16.     set_real_ip_from 172.18.0.0/16;
  17.     set_real_ip_from 172.19.0.0/16;
  18.     set_real_ip_from 127.0.0.1/32;
  19.     #real_ip_header X-Forwarded-For;
  20.     #上游还有代理 端口复用专用配置
  21.     real_ip_header proxy_protocol;
  22.     proxy_set_header X-Real-IP       $proxy_protocol_addr;
  23.     proxy_set_header X-Forwarded-For $proxy_protocol_addr;
  24.     real_ip_recursive on;
  25.     #常规配置
  26.     #real_ip_header X-Forwarded-For;
  27.     #real_ip_recursive on;
  28.     #proxy_set_header X-Real-IP $remote_addr;
  29.     #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  30.     # 安全头部设置
  31.     add_header X-Frame-Options "SAMEORIGIN";
  32.     #add_header X-XSS-Protection "1; mode=block";
  33.     #add_header X-Content-Type-Options "nosniff";
  34.     #add_header Referrer-Policy "strict-origin-when-cross-origin";
  35.     #add_header Content-Security-Policy "default-src 'self'; style-src 'self';" always;
  36.     #add_header Content-Security-Policy "default-src 'self'";
  37.     # XSS
  38.     add_header X-XSS-Protection "1; mode=block";
  39.     # 点击劫持
  40.     add_header X-Frame-Options "DENY";
  41.     # 文件上传限制
  42.     client_max_body_size 400M;

  43.     # 请求超时配置
  44.     client_body_timeout 10s;
  45.     client_header_timeout 10s;
  46.     send_timeout 10s;

  47.     # 基本网络优化
  48.     sendfile on;
  49.     tcp_nopush on;
  50.     tcp_nodelay on;
  51.     keepalive_timeout 65;

  52.     # 压缩设置
  53.     gzip on;
  54.     gzip_disable "msie6";
  55.     gzip_vary on;
  56.     gzip_proxied any;
  57.     gzip_comp_level 6;
  58.     gzip_buffers 16 8k;
  59.     gzip_types text/plain text/css application/javascript application/json application/xml application/rss+xml image/svg+xml;

  60.     # 静态文件缓存优化
  61.     location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2|ttf|eot|otf|ttc|map)$ {
  62.         root /usr/share/nginx/html;
  63.         expires 6M;
  64.         add_header Cache-Control "public";
  65.         access_log off;
  66.     }

  67.     # 禁止访问 .git 目录
  68.     location ~ /\.git {
  69.         root /usr/share/nginx/html;
  70.         deny all;
  71.         return 404;
  72.     }

  73.     # 禁止访问特定文件
  74.     location ~* \.(htaccess|git|env)$ {
  75.         root /usr/share/nginx/html;
  76.         deny all;
  77.         return 404;
  78.     }
  79.     # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  80.     #
  81.     #location ~ \.php$ {
  82.     #    proxy_pass   http://127.0.0.1;
  83.     #}

  84.     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  85.     #
  86.     #location ~ \.php$ {
  87.     #    root           html;
  88.     #    fastcgi_pass   127.0.0.1:9000;
  89.     #    fastcgi_index  index.php;
  90.     #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  91.     #    include        fastcgi_params;
  92.     #}

  93.     # deny access to .htaccess files, if Apache's document root
  94.     # concurs with nginx's one
  95.     #
  96.     location ~ /\.ht {
  97.         root /usr/share/nginx/html;
  98.         deny  all;
  99.     }

  100.     # ERROR 404
  101.     error_page 404 /404.html;
  102.     location = /404.html {
  103.         root /usr/share/nginx/html;
  104.         internal;
  105.         sub_filter '{{ status_code }}' $status;
  106.         sub_filter "{{ status_name }}" $status_name;
  107.         sub_filter_once off;
  108.     }

  109.     # ERROR 403
  110.     error_page 403 /403.html;
  111.     location = /403.html {
  112.         root /usr/share/nginx/html;
  113.         internal;
  114.         sub_filter '{{ status_code }}' $status;
  115.         sub_filter "{{ status_name }}" $status_name;
  116.         sub_filter_once off;
  117.     }

  118.     # ERROR 40x
  119.     error_page 401 402 400 405 406 407 408 409 410 411 412 413 414 415 416 417 418 429 /40x.html;
  120.     location = /40x.html {
  121.         root /usr/share/nginx/html;
  122.         internal;
  123.         sub_filter '{{ status_code }}' $status;
  124.         sub_filter "{{ status_name }}" $status_name;
  125.         sub_filter_once off;
  126.     }

  127.     # ERROR 50x
  128.     error_page 500 501 502 503 504 505 /50x.html;
  129.     location = /50x.html {
  130.         root /usr/share/nginx/html;
  131.         internal;
  132.         sub_filter '{{ status_code }}' $status;
  133.         sub_filter "{{ status_name }}" $status_name;
  134.         sub_filter_once off;
  135.     }
  136. }

  137. server {
  138.     listen 443 ssl proxy_protocol;
  139.     http2 on;
  140.     server_name nginx-webapp;
  141.     root /usr/share/nginx/html;
  142.     access_log /var/log/nginx/access_backend_https.log main;
  143.     error_log /var/log/nginx/error_backend_https.log warn;
  144.     # proxy_protocol docs: https://nginx.org/en/docs/stream/ngx_stream_realip_module.html

  145.     location / {
  146.         root /usr/share/nginx/html;
  147.         index  index.html index.htm;
  148.     }

  149.     # SSL 证书配置
  150.     ssl_certificate /etc/nginx/certs.d/server.pem;
  151.     ssl_certificate_key /etc/nginx/certs.d/server.key;

  152.     # SSL 其他配置
  153.     #ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  154.     #ssl_ciphers  HIGH:!aNULL:!MD5;
  155.     ssl_protocols TLSv1.2 TLSv1.3;
  156.     ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';
  157.     ssl_prefer_server_ciphers on;
  158.     ssl_session_cache shared:SSL:10m;
  159.     ssl_session_timeout 1h;
  160.     ssl_stapling off;
  161.     ssl_stapling_verify off;
  162.     # add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;

  163.     # 获取源 IP 配置 适用于代理负载均衡器 CDN 环境
  164.     set_real_ip_from 172.16.0.0/16;
  165.     set_real_ip_from 172.17.0.0/16;
  166.     set_real_ip_from 172.18.0.0/16;
  167.     set_real_ip_from 172.19.0.0/16;
  168.     set_real_ip_from 127.0.0.1/32;
  169.     #real_ip_header X-Forwarded-For;
  170.     #上游还有代理 端口复用专用配置
  171.     real_ip_header proxy_protocol;
  172.     proxy_set_header X-Real-IP       $proxy_protocol_addr;
  173.     proxy_set_header X-Forwarded-For $proxy_protocol_addr;
  174.     real_ip_recursive on;
  175.     #常规配置
  176.     #real_ip_header X-Forwarded-For;
  177.     #real_ip_recursive on;
  178.     #proxy_set_header X-Real-IP $remote_addr;
  179.     #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  180.     # 安全头部设置
  181.     add_header X-Frame-Options "SAMEORIGIN";
  182.     #add_header X-XSS-Protection "1; mode=block";
  183.     #add_header X-Content-Type-Options "nosniff";
  184.     #add_header Referrer-Policy "strict-origin-when-cross-origin";
  185.     #add_header Content-Security-Policy "default-src 'self'";
  186.     # XSS
  187.     add_header X-XSS-Protection "1; mode=block";
  188.     # 点击劫持
  189.     add_header X-Frame-Options "DENY";
  190.     # 文件上传限制
  191.     client_max_body_size 400M;

  192.     # 请求超时配置
  193.     client_body_timeout 10s;
  194.     client_header_timeout 10s;
  195.     send_timeout 10s;

  196.     # 基本网络优化
  197.     sendfile on;
  198.     tcp_nopush on;
  199.     tcp_nodelay on;
  200.     keepalive_timeout 65;

  201.     # 压缩设置
  202.     gzip on;
  203.     gzip_disable "msie6";
  204.     gzip_vary on;
  205.     gzip_proxied any;
  206.     gzip_comp_level 6;
  207.     gzip_buffers 16 8k;
  208.     gzip_types text/plain text/css application/javascript application/json application/xml application/rss+xml image/svg+xml;

  209.     # 静态文件缓存优化
  210.     location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2|ttf|eot|otf|ttc|map)$ {
  211.         root /usr/share/nginx/html;
  212.         expires 6M;
  213.         add_header Cache-Control "public";
  214.         access_log off;
  215.     }

  216.     # 特殊文件访问控制
  217.     location ~* \.(htaccess|git|env)$ {
  218.         root /usr/share/nginx/html;
  219.         deny all;
  220.         return 404;
  221.     }

  222.     location ~ /\.ht {
  223.         root /usr/share/nginx/html;
  224.         deny  all;
  225.         return 404;
  226.     }

  227.     # ERROR 404
  228.     error_page 404 /404.html;
  229.     location = /404.html {
  230.         root /usr/share/nginx/html;
  231.         internal;
  232.         sub_filter '{{ status_code }}' $status;
  233.         sub_filter "{{ status_name }}" $status_name;
  234.         sub_filter_once off;
  235.     }

  236.     # ERROR 403
  237.     error_page 403 /403.html;
  238.     location = /403.html {
  239.         root /usr/share/nginx/html;
  240.         internal;
  241.         sub_filter '{{ status_code }}' $status;
  242.         sub_filter "{{ status_name }}" $status_name;
  243.         sub_filter_once off;
  244.     }

  245.     # ERROR 40x
  246.     error_page 401 402 400 405 406 407 408 409 410 411 412 413 414 415 416 417 418 429 /40x.html;
  247.     location = /40x.html {
  248.         root /usr/share/nginx/html;
  249.         internal;
  250.         sub_filter '{{ status_code }}' $status;
  251.         sub_filter "{{ status_name }}" $status_name;
  252.         sub_filter_once off;
  253.     }

  254.     # ERROR 50x
  255.     error_page 500 501 502 503 504 505 /50x.html;
  256.     location = /50x.html {
  257.         root /usr/share/nginx/html;
  258.         internal;
  259.         sub_filter '{{ status_code }}' $status;
  260.         sub_filter "{{ status_name }}" $status_name;
  261.         sub_filter_once off;
  262.     }
  263. }
复制代码

三、核心配置
3.1 Nginx 端口复用配置
  nginx-balances:n1.27.2-v1.0 from nginx-base:1.27.2
  重要配置:nginx-balances 必须启用 proxy_protocol: on ,以适应 nginx-webapp
nginx.conf -> /etc/nginx/nginx.conf
  1. user  nginx;
  2. worker_processes  auto;

  3. error_log  /var/log/nginx/error.log notice;
  4. pid        /var/run/nginx.pid;


  5. events {
  6.     worker_connections  1024;
  7. }


  8. # docs: https://nginx.org/en/docs/stream/ngx_stream_core_module.html
  9. stream {
  10.      # docs: https://nginx.org/en/docs/stream/ngx_stream_log_module.html
  11.      # log_format stream '$remote_addr [$time_local] '
  12.      #            '$protocol $status $bytes_sent $bytes_received '
  13.      #            '$session_time "$upstream_addr" '
  14.      #            '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
  15.      # custom
  16.     log_format stream '{"@access_time":"$time_iso8601",'
  17.         '"clientip":"$remote_addr",'
  18.         '"pid":$pid,'
  19.         '"pro":"$protocol",'
  20.         '"ssl_pro": "$ssl_preread_protocol",'
  21.         '"pro":"$protocol",'
  22.         '"stus":$status,'
  23.         '"sent":$bytes_sent,'
  24.         '"recv":$bytes_received,'
  25.         '"sess_time":$session_time,'
  26.         '"up_addr":"$upstream_addr",'
  27.         '"up_sent":$upstream_bytes_sent,'
  28.         '"up_recv":$upstream_bytes_received,'
  29.         '"up_conn_time":$upstream_connect_time,'
  30.         '"up_resp_time":"$upstream_first_byte_time",'
  31.         '"up_sess_time":$upstream_session_time}';

  32.     upstream http {
  33.         server nginx-webapp:80;
  34.     }

  35.     upstream https {
  36.         server nginx-webapp:443;
  37.     }

  38.     map $ssl_preread_protocol $upstream {
  39.         default http;
  40.         "TLSv1.0" https;
  41.         "TLSv1.1" https;
  42.         "TLSv1.2" https;
  43.         "TLSv1.3" https;
  44.     }

  45.     server {
  46.         listen 18080;
  47.         proxy_pass $upstream;
  48.         proxy_connect_timeout 3s;
  49.         proxy_timeout 5s;
  50.         ssl_preread on;
  51.         proxy_protocol on;
  52.         access_log /var/log/nginx/stream_access.log stream;
  53.         error_log /var/log/nginx/stream_error.log notice;
  54.     }
  55. }

  56. http {
  57.     include       /etc/nginx/mime.types;
  58.     default_type  application/octet-stream;

  59.     log_format  main  '$proxy_protocol_addr - $remote_addr - $remote_user [$time_local] "$request" '
  60.                       '$status $body_bytes_sent "$http_referer" '
  61.                       '"$http_user_agent" "$http_x_forwarded_for"';

  62.     access_log  /var/log/nginx/access.log  main;

  63.     sendfile        on;
  64.     #tcp_nopush     on;

  65.     keepalive_timeout  65;

  66.     # 压缩设置
  67.     gzip on;
  68.     gzip_disable "msie6";
  69.     gzip_vary on;
  70.     gzip_proxied any;
  71.     gzip_comp_level 6;
  72.     gzip_buffers 16 8k;
  73.     gzip_types text/plain text/css application/javascript application/json application/xml application/rss+xml image/svg+xml;
  74.     map $status $status_name {
  75.         100 "Continue";
  76.         101 "Switching Protocols";
  77.         102 "Processing";
  78.         103 "Early Hints";
  79.         200 "OK";
  80.         201 "Created";
  81.         202 "Accepted";
  82.         203 "Non-Authoritative Information";
  83.         204 "No Content";
  84.         205 "Reset Content";
  85.         206 "Partial Content";
  86.         300 "Multiple Choices";
  87.         301 "Moved Permanently";
  88.         302 "Found";
  89.         303 "See Other";
  90.         304 "Not Modified";
  91.         305 "Use Proxy";
  92.         307 "Temporary Redirect";
  93.         308 "Permanent Redirect";
  94.         400 "Bad Request";
  95.         401 "Unauthorized";
  96.         403 "Forbidden";
  97.         404 "Not Found";
  98.         405 "Method Not Allowed";
  99.         406 "Not Acceptable";
  100.         407 "Proxy Authentication Required";
  101.         408 "Request Timeout";
  102.         409 "Conflict";
  103.         410 "Gone";
  104.         411 "Length Required";
  105.         412 "Precondition Failed";
  106.         413 "Payload Too Large";
  107.         414 "URI Too Long";
  108.         415 "Unsupported Media Type";
  109.         416 "Range Not Satisfiable";
  110.         417 "Expectation Failed";
  111.         418 "I'm a teapot";
  112.         429 "Too Many Requests";
  113.         500 "Internal Server Error";
  114.         501 "Not Implemented";
  115.         502 "Bad Gateway";
  116.         503 "Service Unavailable";
  117.         504 "Gateway Timeout";
  118.         505 "HTTP Version Not Supported";
  119.         default "Unknown";
  120.     }

  121.     include /etc/nginx/conf.d/*.conf;
  122. }
复制代码


3.1 Haproxy 端口复用配置
  haproxy-balances:h3.0.6-v1.0 from haproxy-balances:3.0.6
  重要配置:Haproxy-balances 必须启用 send-proxy ,以适应 nginx-webapp

  1. global
  2.     log /dev/stdout local0
  3.     maxconn 2000
  4.     user haproxy
  5.     group haproxy
  6.     daemon

  7. defaults
  8.     log     global
  9.     option  httplog
  10.     option  dontlognull
  11.     timeout http-request    30s
  12.     timeout queue           10s
  13.     timeout connect         10s
  14.     timeout client          1m
  15.     timeout server          1m
  16.     timeout http-keep-alive 10s
  17.     timeout check           10s
  18. #frontend stats
  19. #    bind *:18081 ssl crt /var/lib/haproxy/certs.d/server.pem
  20. #    bind *:18081
  21. #    bind *:18081 ssl crt /var/lib/haproxy/certs.d/server.pem
  22. #    mode http
  23. #    stats enable
  24. #    stats uri /haproxy_stats
  25. #    stats realm Haproxy\ Statistics
  26. #    stats auth admin:password
  27. #    stats show-node
  28. #    stats show-legends
  29. #    stats hide-version

  30. frontend main
  31.     bind *:18080
  32.     mode tcp
  33.     option tcplog
  34.     log-format %ft\ %b/%s
  35.     tcp-request inspect-delay 5s
  36.     # acl is_ssh req.payload(0,3) -m bin 535348
  37.     # acl is_rdp req.payload(0,3) -m bin 030000
  38.     # acl is_telnet req.payload(0,4) -m bin 54656c6e
  39.     # acl is_smb req.payload(0,4) -m bin 4d5a9000
  40.     # acl is_ftp_user req.payload(0,4) -m bin 55534552
  41.     # acl is_ftp_pass req.payload(0,4) -m bin 50415353
  42.     # acl is_smtp_helo req.payload(0,4) -m bin 48454c4f
  43.     # acl is_pop3_user req.payload(0,4) -m bin 55534552
  44.     # acl is_pop3_pass req.payload(0,4) -m bin 50415353
  45.     # acl is_imap_login req.payload(0,5) -m bin 4c4f4749
  46.     # acl is_dns_query req.payload(0,2) -m bin 0000
  47.     # acl is_ldap req.payload(0,4) -m bin 30303030

  48.    acl is_https req.payload(0,3) -m bin 160301
  49.                                        # HTTPS
  50.     acl is_http req.payload(0,3) -m bin 474554 504f53 505554 44454c 4f5054 484541 434f4e 545241
  51.                                        #  GET   POST   PUT   DELETE OPTIONS HEAD  CONNECT TRACE
  52.     #acl is_https req.ssl_hello_type 1
  53.     tcp-request content accept if is_https
  54.     # tcp-request content accept if { req.ssl_hello_type 1 }
  55.     tcp-request content accept if is_http
  56.     use_backend backend_http if is_http
  57.     use_backend backend_https if is_https
  58.     default_backend backend_http

  59. backend backend_http
  60.     mode tcp
  61.     balance roundrobin
  62.     server backend1 nginx-webapp:80 maxconn 10 check inter 3s send-proxy

  63. backend backend_https
  64.     mode tcp
  65.     balance roundrobin
  66.     server backend1 nginx-webapp:443 maxconn 10 check inter 3s send-proxy
复制代码

四、docker-compose compose file
compose file 示例
  1. name: nginx-scripts-app
  2. # docker-compose v20.3
  3. services:
  4.   nginx-webapp:
  5.     image: nginx-webapp:n1.27.2-v1.0
  6.     expose:
  7.       - "80"
  8.       - "443"
  9.     volumes:
  10.       - ./certs.d:/etc/nginx/certs.d:ro
  11.       - ./nginx-webapp/conf.d:/etc/nginx/conf.d
  12.       - ./nginx-webapp/nginx.conf:/etc/nginx/nginx.conf
  13.       - ./nginx-webapp/html/:/usr/share/nginx/html
  14.       - ./nginx-webapp/logs/:/var/log/nginx
  15.     networks:
  16.       - nginx-scripts

  17.   haproxy-balances:
  18.     image: haproxy-balances:h3.0.6-v1.0
  19.     expose:
  20.       - "18080"
  21.     ports:
  22.       - "18081:18080"
  23.     volumes:
  24.       - ./haproxy-balances/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg
  25.     networks:
  26.       - nginx-scripts

  27.   nginx-balances:
  28.     image: nginx-webapps:1.27.2
  29.     expose:
  30.       - "80"
  31.       - "443"
  32.     ports:
  33.       - "18080:18080"
  34.     volumes:
  35.       #- ./certs.d:/etc/nginx/certs.d:ro
  36.       - ./nginx-balances/conf.d:/etc/nginx/conf.d
  37.       - ./nginx-balances/nginx.conf:/etc/nginx/nginx.conf
  38.       - ./nginx-balances/html/:/usr/share/nginx/html
  39.       - ./nginx-balances/logs/:/var/log/nginx
  40.     networks:
  41.       - nginx-scripts

  42. networks:
  43.   nginx-scripts:
  44.     driver: bridge
  45.     ipam:
  46.       driver: default
  47.       config:
  48.         - subnet: 172.18.0.0/16
  49.           ip_range: 172.18.18.0/24
  50.           gateway: 172.18.18.254
复制代码


                               
登录/注册后可看大图
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 支持支持 反对反对
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|重庆思庄Oracle、Redhat认证学习论坛 ( 渝ICP备12004239号-4 )

GMT+8, 2024-12-22 01:03 , Processed in 0.191772 second(s), 20 queries .

重庆思庄学习中心论坛-重庆思庄科技有限公司论坛

© 2001-2020

快速回复 返回顶部 返回列表