如何将certbot添加到另一个端口

时间:2019-06-10 00:51:50

标签: ubuntu nginx certbot

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

我对nginx不太熟练,我不知道该如何解决。我需要最后一个服务器在端口4444上使用certbot。

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;
    }
}

1 个答案:

答案 0 :(得分:0)

此处需要对此行进行一些澄清:

  

我需要最后一个服务器在端口4444上使用certbot

您是说要使用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路径不需要更改。

希望有帮助!