我有一个域sub.example.com
,该域在Ubuntu服务器上已配置并可以正常运行。我使用Certbot通过HTTPS配置域,但是我还配置了可以在该域的特定端口(例如2500)上访问的API。当我访问example.com
时,看到的锁表明该站点是安全的,但是,每当我转到example.com:2500/api/someAPI
时,该API都会返回适当的结果,但是该站点并不安全。因为主站点是安全的,而API访问位置却不安全,所以我无法相应地进行API调用,从而导致net::ERR_SSL_PROTOCOL_ERROR
。
堆栈:
前段时间,我能够在另一个VPS(DigitalOcean)和域上使用完全相同的技术来使其运行,但是我不相信我曾经遇到过这个问题。
nginx.conf:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile off;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# nginx-naxsi config
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
#include /etc/nginx/naxsi_core.rules;
##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##
#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
答案 0 :(得分:0)
默认情况下,除端口443外,没有其他端口使用安全连接。但是,如果您指定这样做(例如使用https://example.com:2500),则它应该安全连接。如果希望它自动执行此操作,则需要在Web服务器应用程序设置中对其进行配置。如果您知道使用哪个应用程序托管服务器(例如apache),并且有权编辑配置文件,则应该可以对其进行配置。
编辑:对不起,我最初没有看到您将问题标记为使用nginx。在使用nginx之前,我还没有专门进行此操作,但是我发现this tutorial可以解释该过程。在/etc/nginx/sites-available/example.com内部,您应该可以更改“ listen 443 ssl;”。收听443、2500 ssl;为了侦听多个端口。另外,您还可以添加return 301 https://$host$request_uri;
以便将连接重定向到https安全连接(请参阅https://serversforhackers.com/c/redirect-http-to-https-nginx)。
我希望这会有所帮助!
答案 1 :(得分:0)
我能够解决我的问题。事实证明,它与nginx配置无关。相反,在定义和公开API的node.js
文件中,我需要包括HTTPS
访问。为此,我在代码中添加了以下几行:
const https = require('https');
const httpsOptions = {
cert: fs.readFileSync("/etc/letsencrypt/live/<domain>/fullchain.pem"),
key: fs.readFileSync("/etc/letsencrypt/live/<domain>/privkey.pem")
}
https.createServer(httpsOptions, app).listen(port);
将<domain>
替换为托管API的域名。