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


15

我意识到这看起来至少是其他一些问题的重复,但是我已经阅读了几次,但仍在做错事。

以下是位于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;

}

它有效,当我转到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


1
您提到“我应​​该注意,我可以使用非标准端口,然后在URL中明确键入该端口,例如myexample.com:9443。” 能否请您解释一下您如何做到这一点,因为我想使用像“ myexample.com:9443 ” 这样的smth访问我的网站,其中包含SSL
Ghassan Zein

Answers:


22

为了支持在浏览器中键入“ 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;
}

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

希望这可以帮助!


这是最完整的答案,但是我想您要告诉我的是,为了自动重定向到安全连接,nginx仍然必须绑定到端口443。这就是问题所在:-(我在该端口上绑定了另一个进程
。– GojiraDeMonstah

Nginx是反向代理。您可以让它在443上侦听并将请求传递给适当的应用程序。
蒂姆(Tim)

蒂姆-你是对的。我可能问了一个愚蠢的问题,但我希望有一种方法可以“神奇地”更改SSL端口。
GojiraDeMonstah

@GojiraDeMonstah如果您不想使用端口443,则可以将选择的端口号放在URL中。就这些。
迈克尔·汉普顿

“ ssl”非常重要。我以前使用过default_server并在相当长的一段时间内苦苦挣扎。
swateek '18年

0

当您键入https://example.com,对于HTTPS标准://方案是连接到端口443在你的情况,你已经动了你的服务器,以便它现在侦听端口9443你得到的连接被拒绝因此,出现一条消息-端口443上没有监听。

您将需要安排一些监听端口443的操作,以将连接重定向到端口9443或将端口用作URL的一部分。


-1

如果将端口更改为非标准端口(例如9443),则需要添加从443到9443的重定向。将nginx设置为将代理反向到该端口。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.