我希望查看下面的一些代码——它是“nginx/sites-enabled/default”文件。我相信它可能有一些明显的问题,导致我的网站无法在 https 下重定向。我花了几天时间查看 Nginx 文档,但未能解决我的问题。感谢您的帮助!
背景: 我正在尝试设置一个反向代理,将我的域 url 指向我的 Digital Ocean 服务器上的 localhost:3000。除了我的 https 无法解析之外,一切似乎都运行良好。我通常遵循这两个教程:https://code.lengstorf.com/deploy-nodejs-ssl-digitalocean/和https://www.digitalocean.com/community/tutorials/how-to-secure- nginx-with-let-s-encrypt-on-ubuntu-18-04 .
当我访问我的网址时,我得到:“ERR_CONNECTION_RESET”。当我在服务器上运行 http://localhost:3000 时,我的 html 按预期返回。当我在服务器上运行 https://mysite.io 时,我收到错误:“curl (35) gnutls_handshake() 失败:拉取函数中出现错误”。我所有的 http curl 请求都正确重定向到 https,dig +short mysite.io 指向我的服务器,nginx -t 没有错误地返回。
我的预感是问题出在我的“nginx/sites-enabled/default”文件上,更具体地说是处理 https 的服务器块。前两个服务器块来自第一个教程,后两个是由 Certbot 在上述第二个教程中自动生成的。 再次感谢您的帮助!
# HTTP — redirect all traffic to HTTPS
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
return 301 https://$host$request_uri;
}
# HTTPS — proxy all requests to the Node app
server {
# Enable HTTP/2
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name mysite.io;
# Use the Let’s Encrypt certificates
ssl_certificate /etc/letsencrypt/live/mysite.io/fullchain.pem; # managed $
ssl_certificate_key /etc/letsencrypt/live/mysite.io/privkey.pem; # manage$
# Include the SSL configuration from cipherli.st
include snippets/ssl-params.conf;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3000/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}
server {
return 301 https://$host$request_uri;
server_name www.mysite.io; # managed by Certbot
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/mysite.io/fullchain.pem; # managed $
ssl_certificate_key /etc/letsencrypt/live/mysite.io/privkey.pem; # manage$
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.mysite.io) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80 ;
server_name www.mysite.io;
return 404; # managed by Certbot
}
前两个块是不必要的,可以删除。然后,我们可以从第二个服务器块中取出位置块并将其添加到第三个服务器块中。然后,最后我们可以从第三个块中删除 301,最终得到这样的结果:
server {
server_name www.mysite.io; # managed by Certbot
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3000/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
# change the above line to "listen 443 ssl http2" if you want http2
ssl_certificate /etc/letsencrypt/live/mysite.io/fullchain.pem; # managed $
ssl_certificate_key /etc/letsencrypt/live/mysite.io/privkey.pem; # manage$
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.mysite.io) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80 ;
server_name www.mysite.io;
return 404; # managed by Certbot
}
我建议也安装 nginx-extras
sudo apt-get install nginx-extras
那么,
cd /etc/nginx/sites-available
现在, 是时候用随机文本隐藏你的 nginx 服务器了:
more_set_headers 'Server: random'; # To Set a custom string as "Server"
这是可选的,但建议保护您的网络服务器,因为市场上引入了许多漏洞利用,它可以以某种方式防止攻击者。这也将隐藏您的 nginx 服务器以及 nginx 版本;
另外您还可以添加:
#add this on http section
server_tokens off;
more_clear_headers Server;