Docker nginx不能同时使用http和https

时间:2018-04-06 19:11:16

标签: docker nginx

我正在使用最新的docker nginx。我想使用http和https来访问我的网站。到目前为止,这是我的nginx配置:

default.cnf

upstream backends {
    server app:8080;
}

server {
    listen       80;
    listen       443 ssl;
    server_name  example.com;

    # SSL
    # ssl on;
    ssl_certificate /etc/ssl/certs/214577452530751.pem;
    ssl_certificate_key /etc/ssl/certs/214577452530751.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    #proxy to webs
    location ~.*/ {
        proxy_redirect off;  
        proxy_set_header Host $host;  
        proxy_set_header X-Real-IP $remote_addr;  
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
        proxy_pass http://backends;
    }
}

但是当我访问我的服务器时,只有http有效。对于https,nginx将提供二进制日志:

nginx         | 120.236.174.140 - - [06/Apr/2018:19:04:40 +0000] "\x16\x03\x01\x00\xC3\x01\x00\x00\xBF\x03\x03T\xF7v\x15gk\x04\xE2\xC5\xB3\x8B\x10\xAA\xE0\x1C\xA4[\xCE\x01z\xCC\x81\x19\x93\xC6\x11T5\x02\xD7$7\x00\x00\x1CZZ\xC0+\xC0/\xC0,\xC00\xCC\xA9\xCC\xA8\xC0\x13\xC0\x14\x00\x9C\x00\x9D\x00/\x005\x00" 400 174 "-" "-" "-"

curl如下:

kangbb@udo:~$ curl -L  https://example.com
curl: (35) gnutls_handshake() failed: An unexpected TLS packet was received.

我写错的地方?谁能给我一些adivce?非常感谢你!

2 个答案:

答案 0 :(得分:2)

你必须将它们分开。有一堆SSL的东西正在进行,所谓的HTTP连接。在端口443中取消注释ssl on;。创建另一个侦听端口80的服务器。

upstream backends {
    server app:8080;
}

server {
    listen       443 ssl;
    server_name  example.com;

    # SSL
    ssl on;
    ssl_certificate /etc/ssl/certs/214577452530751.pem;
    ssl_certificate_key /etc/ssl/certs/214577452530751.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    #proxy to webs
    location / {
        proxy_redirect off;  
        proxy_set_header Host $host;  
        proxy_set_header X-Real-IP $remote_addr;  
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
        proxy_pass http://backends;
    }
}

server {
    listen 80;
    server_name example.com;
    location / {
        proxy_redirect off;  
        proxy_set_header Host $host;  
        proxy_set_header X-Real-IP $remote_addr;  
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
        proxy_pass http://backends;
    }
}

答案 1 :(得分:0)

对于docker niginx,它将为您提供两个端口,80和443.如果要将其用于http,请绑定80;如果要将其用于https,请绑定443.