大网数据广告活动
服务器运维问答

Nginx as Reverse Proxy and LetsEncrypt

· 52CE 我爱测

Although there's aplethora of articles on the web about this, I'm still having issues getting this to work. I've set up nginx on ubunto 18.04 - everythings is patched to date. I installed Certbot (sudo apt-get install python-cert…

问题原文

原帖链接 · Scepticalist · CC BY-SA 4.0

Although there's aplethora of articles on the web about this, I'm still having issues getting this to work.

I've set up nginx on ubunto 18.04 - everythings is patched to date.

I installed Certbot (sudo apt-get install python-certbot-nginx)

I'm using "default" config as I'm not going to be running anything on this server except the reverse proxy:

Heres the config - working fine on http:

##
# Default server configuration
#
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    server_name _;

    location / {

            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
    }
}
#
## Redirect to internal servers
#
# HomeAssistant
server {
    listen 80;
    server_name hass.mysite.com;

    proxy_set_header Host   $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    location / {
            proxy_pass http://192.168.1.245:8123;
            proxy_buffering off;
    }
}
#
# SSH Tunnel
server {
    listen 80;
    server_name remote.mysite.com;

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

I'm happy to redirect all external connections to https and leave internal as http.

If I run sudo certbot --nginx I get this and can approve both sites

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: hass.mysite.com
2: remote.mysite.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input

There's no prompt from Certbot to redirect all traffic to https and I;m struggling to set it up to do so - do I have to configure "listen 443" on each redirect?

已采纳解答

原帖链接 · Scepticalist · CC BY-SA 4.0

Ok, after a lot of trial and error, it seems that it didn't like certificating subdomains without the parent domain.

Luckily I'm running in a VM so I rolled back to before the Certbot install.

I commented out this line in the config:

server_name _;

..and added the root domain sites:

server {
    server_name mysite.com www.mysite.com;

    root /var/www/html;

    index index.html index.htm mysite.html;
}

I then got certificates for the root domains:

sudo certbot --nginx -d mysite.com -d www.mysite.com

When I did this, Certbot added the SSL config successfully to the root domains, so I went ahead with certifying the subdomains and this also worked fine.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-02-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。