【问题标题】:How to add certbot to another port如何将 certbot 添加到另一个端口
【发布时间】:2019-06-11 19:13:27
【问题描述】:

我正在设置我的服务器的后端,但我无法让 certbot 使用 https 在我的 API 上工作。只有我的网站被重定向到 https。

我只是不太熟悉 nginx,我不知道如何解决这个问题。我需要最后一台服务器来使用 certbot,在端口 4444 上。

root /var/www/domain.com/public;

server {

        index index.html index.htm index.nginx-debian.html;

        server_name domain.com www.domain.com blog.domain.com;

        location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $Host;
        proxy_cache_bypass $http_upgrade;
               # try_files $uri $uri/ =404;
        }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain.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 = www.domain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


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


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

        listen 80;
        listen [::]:80;

        server_name domain.com blog.domain.com www.domain.com;
    return 404; # managed by Certbot

}

server {
    location / {
        proxy_pass http://localhost:4444;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $Host;
        proxy_cache_bypass $http_upgrade;
               # try_files $uri $uri/ =404;
    }
}

【问题讨论】:

    标签: ubuntu nginx certbot


    【解决方案1】:

    这里需要澄清一下:

    我需要最后一台服务器来使用 certbot,在端口 4444

    您的意思是说使用 Certbot 颁发的证书访问端口 4444(通过反向代理)?

    在最后一个服务器块中,您可能错过了设置server_name。请注意,您之前的服务器块中有这一行。

    假设以下场景:

    API 在不同的子域中运行

    • API:api.domain.com(您的 API 运行的地方)
    • 证书由 Certbot 在/etc/letsencrypt/live/domain.com 中颁发通配符证书

    鉴于这些假设,建议的服务器块配置可能如下:

    server {
        server_name api.domain.com;
        listen 443 ssl;
        ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
        include /etc/letsencrypt/options-ssl-nginx.conf;
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
    
         location / {
            proxy_pass http://localhost:4444;
            ## additional proxy configurations here
        }
    }
    

    如果您没有请求通配符证书覆盖所有子域,我建议专门为您的 API 子域请求,并根据需要修改 ssl_certificate_keyssl_certificate 路径。

    API 是主域的一部分

    • API:domain.com/api(API 在 domain.com 内运行)

    如果是这种情况,请考虑在您的第一个服务器块内添加一个位置块:

    server {
        ....
        location / {
            ..... no changes here
        }
    
        ## add this block
        location /api/ {
            proxy_pass http://localhost:4444;
            ## additional proxy configurations here
        }
    
        listen [::]:443 ssl ipv6only=on; # managed by Certbot
        ....
    }
    
    

    在这种情况下,ssl_certificate_keyssl_certificate 路径不需要更改。

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 2015-12-14
      • 2021-05-28
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多