如何使用上游和反向代理 nginx

如何使用上游和反向代理 nginx

我在 cdn1.xxvideo18.com 和 cdn2.xxvideo18.com 有 2 个节点服务器,主服务器在 cdnxx.xxvideo18.com 我如何使用上游和反向代理?我尝试了以下配置,但它返回默认的 404 页面

upstream main {
    server cdn1.xxvideo18.com;
    server cdn2.xxvideo18.com;
}

server {
    server_name cdnxx.xxvideo18.com;
    location / {
        proxy_pass http://main;
    }


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/cdnxx.xxvideo18.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/cdnxx.xxvideo18.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    if ($host = cdnxx.xxvideo18.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    server_name cdnxx.xxvideo18.com;
    listen 80;
    return 404; # managed by Certbot


}

找到了,我必须添加上游端口

upstream main {
    server cdn1.xxvideo18.com:443;
    server cdn2.xxvideo18.com:443;
}

server {
    server_name cdnxx.xxvideo18.com;
    location / {
        proxy_pass https://main;
    }


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/cdnxx.xxvideo18.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/cdnxx.xxvideo18.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

答案1

找到了,我必须添加上游端口

upstream main {
    server cdn1.xxvideo18.com:443;
    server cdn2.xxvideo18.com:443;
}

server {
    server_name cdnxx.xxvideo18.com;
    location / {
        proxy_pass https://main;
    }


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/cdnxx.xxvideo18.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/cdnxx.xxvideo18.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

相关内容