AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1054065
Accepted
campos
campos
Asked: 2021-02-18 18:09:41 +0800 CST2021-02-18 18:09:41 +0800 CST 2021-02-18 18:09:41 +0800 CST

负载平衡的反向代理的最佳设置是什么?

  • 772

我正在使用反向代理和负载平衡。在我的场景中,我有三台服务器。第一台服务器是代理。第二个和第三个是使用 swarm 的 docker 服务器。端口 2020 用于 Apache 服务的两个副本。2021 端口用于 Nginx 服务的两个副本。平衡是根据副本完成的。由于我是 Nginx 的新手,我想知道以下设置是否足够。是否有可能改进此配置?安全吗?

proxy_config.conf:

##########
# Apache #
##########

  upstream apache {
        least_conn;

        #Container replicas
        server 192.168.0.4:2020;
        server 192.168.0.5:2020;
  }
  server {
        listen 80;
        server_name host.apache.domain.com;

        location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://apache;
        }
   }

#########
# Nginx #
#########

  upstream nginx {
        least_conn;

        #Container replicas
        server 192.168.0.4:2021;
        server 192.168.0.5:2021;
  }

  server {
        listen 80;
        server_name host.nginx.domain.com;

        location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://nginx;
        }
   }

在这种特定情况下,我使用了以下内容:

    location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://nginx;
    }

如果我只使用下面的配置,难道还不够吗?

    location / {
            proxy_pass http://nginx;
    }

我阅读了 Nginx 文档,但我需要更多示例。

load-balancing nginx reverse-proxy
  • 1 1 个回答
  • 362 Views

1 个回答

  • Voted
  1. Best Answer
    suchislife
    2021-02-20T14:43:40+08:002021-02-20T14:43:40+08:00

    Digital Ocean 提供了一个免费的 NGINX 配置工具,带有代码注释和示例供您学习。绝对玩这个。我希望它在我学习 NGINX 的时候还存在。

    访问nginxconfig.io

    下面的带有安全标头代码的 SSL 反向代理服务器是用它生成的。您绝对可以在学习过程中对其进行自定义

    # Generated by nginxconfig.io
    # https://www.digitalocean.com/community/tools/nginx?domains.0.php.php=false&domains.0.reverseProxy.reverseProxy=true&domains.0.routing.index=index.html&domains.0.routing.fallbackHtml=true&global.https.ocspCloudflare=false&global.https.ocspOpenDns=false&global.tools.modularizedStructure=false
    
    user                 www-data;
    pid                  /run/nginx.pid;
    worker_processes     auto;
    worker_rlimit_nofile 65535;
    
    events {
        multi_accept       on;
        worker_connections 65535;
    }
    
    http {
        charset                utf-8;
        sendfile               on;
        tcp_nopush             on;
        tcp_nodelay            on;
        server_tokens          off;
        log_not_found          off;
        types_hash_max_size    2048;
        types_hash_bucket_size 64;
        client_max_body_size   16M;
    
        # MIME
        include                mime.types;
        default_type           application/octet-stream;
    
        # Logging
        access_log             /var/log/nginx/access.log;
        error_log              /var/log/nginx/error.log warn;
    
        # SSL
        ssl_session_timeout    1d;
        ssl_session_cache      shared:SSL:10m;
        ssl_session_tickets    off;
    
        # Diffie-Hellman parameter for DHE ciphersuites
        ssl_dhparam            /etc/nginx/dhparam.pem;
    
        # Mozilla Intermediate configuration
        ssl_protocols          TLSv1.2 TLSv1.3;
        ssl_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;
    
        # OCSP Stapling
        ssl_stapling           on;
        ssl_stapling_verify    on;
        resolver               8.8.8.8 8.8.4.4 valid=60s;
        resolver_timeout       2s;
    
        # Connection header for WebSocket reverse proxy
        map $http_upgrade $connection_upgrade {
            default upgrade;
            ""      close;
        }
    
        # Load configs
        include /etc/nginx/conf.d/*.conf;
    
        # example.com
        server {
            listen                               443 ssl http2;
            listen                               [::]:443 ssl http2;
            server_name                          example.com;
            root                                 /var/www/example.com/public;
    
            # SSL
            ssl_certificate                      /etc/letsencrypt/live/example.com/fullchain.pem;
            ssl_certificate_key                  /etc/letsencrypt/live/example.com/privkey.pem;
            ssl_trusted_certificate              /etc/letsencrypt/live/example.com/chain.pem;
    
            # security headers
            add_header X-Frame-Options           "SAMEORIGIN" always;
            add_header X-XSS-Protection          "1; mode=block" always;
            add_header X-Content-Type-Options    "nosniff" always;
            add_header Referrer-Policy           "no-referrer-when-downgrade" always;
            add_header Content-Security-Policy   "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
            add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    
            # . files
            location ~ /\.(?!well-known) {
                deny all;
            }
    
            # index.php fallback
            location ~ ^/api/ {
                try_files $uri $uri/ /index.php?$query_string;
            }
    
            # reverse proxy
            location / {
                proxy_pass                         http://127.0.0.1:3000;
                proxy_http_version                 1.1;
                proxy_cache_bypass                 $http_upgrade;
    
                # Proxy headers
                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-Proto $scheme;
                proxy_set_header X-Forwarded-Host  $host;
                proxy_set_header X-Forwarded-Port  $server_port;
    
                # Proxy timeouts
                proxy_connect_timeout              60s;
                proxy_send_timeout                 60s;
                proxy_read_timeout                 60s;
            }
    
            # favicon.ico
            location = /favicon.ico {
                log_not_found off;
                access_log    off;
            }
    
            # robots.txt
            location = /robots.txt {
                log_not_found off;
                access_log    off;
            }
    
            # assets, media
            location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
                expires    7d;
                access_log off;
            }
    
            # svg, fonts
            location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff2?)$ {
                add_header Access-Control-Allow-Origin "*";
                expires    7d;
                access_log off;
            }
    
            # gzip
            gzip            on;
            gzip_vary       on;
            gzip_proxied    any;
            gzip_comp_level 6;
            gzip_types      text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;
        }
    
        # subdomains redirect
        server {
            listen                  443 ssl http2;
            listen                  [::]:443 ssl http2;
            server_name             *.example.com;
    
            # SSL
            ssl_certificate         /etc/letsencrypt/live/example.com/fullchain.pem;
            ssl_certificate_key     /etc/letsencrypt/live/example.com/privkey.pem;
            ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
            return                  301 https://example.com$request_uri;
        }
    
        # HTTP redirect
        server {
            listen      80;
            listen      [::]:80;
            server_name .example.com;
    
            # ACME-challenge
            location ^~ /.well-known/acme-challenge/ {
                root /var/www/_letsencrypt;
            }
    
            location / {
                return 301 https://example.com$request_uri;
            }
        }
    }
    
    • 1

相关问题

  • 网络负载平衡服务器无法相互通信

  • 用于网络监控的路由/代理 SNMP 陷阱(或 Netflow、通用 UDP 等)的解决方案?

  • 防止拒绝服务攻击的最佳技术是什么?

  • 添加备份互联网连接需要哪些硬件?

  • 什么是最有效的 Windows 负载平衡解决方案?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve