haproxy 與 nginx 用於網頁伺服器代管

haproxy 與 nginx 用於網頁伺服器代管

我有一個 nginx 伺服器,它在連接埠 80 和 443 上為不同的網路伺服器提供服務,如果我直接點擊,它就可以正常工作。

abc.com、zyx.com

 client ---->abc.com,zyx.com (on nginx)

現在我試圖透過 haproxy 做同樣的事情但無法這樣做。

 client -----> abc.com, zyx.com (on haproxy) ----->nginx (abc.com, zyx.com)

haproxy和nginx位於同一個vpc上,所以我想進一步修改它以呼叫內部ip。能達到同樣的效果嗎?

以下是haproxy的配置

   global
    daemon
    maxconn 100000
    gid 99
    uid 99
    log /dev/log    local0
    log /dev/log    local1 notice
    nbproc 4
    pidfile /etc/haproxy/pid


   defaults
    log global
    mode http
    maxconn 100000
    option dontlognull
    option forwardfor except 127.0.0.1
    option http-server-close
    option httpclose
    option httplog
    option http_proxy
    option redispatch
    option logasap
    option log-separate-errors
    retries 3

    timeout http-request 86400
    timeout queue 86400
    timeout connect 86400
    timeout client 86400
    timeout server 86400
    timeout http-keep-alive 3000
    timeout check 2000

    errorfile 400 /etc/haproxy/errors/400.http
    errorfile 403 /etc/haproxy/errors/403.http
    errorfile 408 /etc/haproxy/errors/408.http
    errorfile 500 /etc/haproxy/errors/500.http
    errorfile 502 /etc/haproxy/errors/502.http
    errorfile 503 /etc/haproxy/errors/503.http
    errorfile 504 /etc/haproxy/errors/504.http


   frontend https-in
    bind *:443 ssl crt /etc/haproxy/ssl/*.pem
    reqadd X-Forwarded-Proto:\ https
    acl abc hdr(host) -i abc.com
    use_backend wow if abc

   frontend http
    bind *:80
    compression algo gzip
    compression type application/json text/html text/plain text/css
    reqadd X-Forwarded-Proto:\ http
    stats enable
    stats hide-version
    stats uri /stats
    stats realm Haproxy\ Statistics
    stats auth haproxy:haproxy
    stats refresh 10s 

    acl zyx hdr(host) -i zyx.com
    use_backend amazing if zyx

   wow
    server nginx someip:443 check
   amazing
    server nginx someip:80 check

以下是nginx伺服器的配置

abc.com

         server {
      listen 443;
      server_name abc.com;
      ssl on;
      ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
      ssl_certificate /etc/nginx/ssl/*.crt;
      ssl_certificate_key /etc/nginx/ssl/*.key;

      location  / {
       root /location_of_webserver;
       proxy_set_header Host $host;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_connect_timeout      360;
       proxy_send_timeout         360;
       proxy_read_timeout         360;
      }
    }

zyx.com

    server {
            listen 80;
            server_name zyx.com;

            location  / {
              alias /location_of_webserver/;
              autoindex on;
              expires 24h;
             }
    }

答案1

嘗試更改以下內容:

wow
server nginx someip:443 check
amazing
server nginx someip:80 check

到:

backend wow
server nginx ipaddress:443 check
backend amazing
server nginx ipaddress:80 check

如果您可以使用日誌檔案錯誤更新您的問題,則可以更輕鬆地確定原因。

相關內容