如何在非标准端口上运行 nginx SSL

Goj*_*ira 18 ssl nginx

我意识到这看起来至少是其他一些问题的重复,但我已经将它们读了好几遍,但仍然做错了什么。

以下是我的 myexample.com nginx 配置文件的内容,位于/etc/nginx/sites-available.

server {

  listen       443 ssl;
  listen       [::]:443 ssl;

  server_name myexample.com www.myexample.com;
  add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
  ssl_certificate /etc/letsencrypt/live/myexample.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/myexample.com/privkey.pem;

  #Configures the publicly served root directory
  #Configures the index file to be served
  root /var/www/myexample.com;
      index index.html index.htm;

}
Run Code Online (Sandbox Code Playgroud)

它有效,当我转到https://myexample.com 时,内容已提供并且连接是安全的。所以这个配置看起来不错。

现在,如果我将 ssl 端口更改为 9443 并重新加载 nginx 配置,则配置会重新加载而不会出错,但访问https://myexample.com 会在浏览器中显示错误(无法访问此站点 / myexample.com 拒绝访问连接。ERR_CONNECTION_REFUSED)

我在这里这里这里(以及其他)尝试了建议和文档,但我总是收到 ERR_CONNECTION_REFUSED 错误。

我应该注意,我可以使用非标准端口,然后将该端口显式键入 URL,例如https://myexample.com:9443。但我不想那样做。我想要的是让用户能够在任何浏览器中输入 myexample.com 并让 nginx 自动重定向到安全连接。

同样,当我使用标准 443 SSL 端口时,我没有任何问题。

编辑:我在 debian/jessie 上使用 nginx/1.6.2

Cas*_*lia 32

为了支持在浏览器中输入“ https://myexample.com ”,让它由nginx监听端口 9443的配置处理,您将需要一个nginx仍然监听端口 443的附加配置,因为这是 IP 端口浏览器连接的

因此:

server {
  listen 443 ssl;
  listen [::]:443 ssl;

  server_name myexample.com www.myexample.com;
  ssl_certificate /etc/letsencrypt/live/myexample.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/myexample.com/privkey.pem;

  # Redirect the browser to our port 9443 config
  return 301 $scheme://myexample.com:9443$request_uri;
}

server {
  listen 9443 ssl;
  listen [::]:9443 ssl;

  server_name myexample.com www.myexample.com;
  ssl_certificate /etc/letsencrypt/live/myexample.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/myexample.com/privkey.pem;
  add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";

  #Configures the publicly served root directory
  #Configures the index file to be served
  root /var/www/myexample.com;
  index index.html index.htm;
}
Run Code Online (Sandbox Code Playgroud)

请注意,两个部分都需要相同的证书/密钥,因为证书通常与 DNS 主机名相关联,但不一定与端口相关联。

希望这可以帮助!