Docker容器中的Nginx SSL

时间:2015-01-13 11:14:41

标签: nginx docker

我已经配置了一个Docker容器来运行Nginx并设置/ etc / nginx / sites-available / default文件,如下所示

server 
{
 listen 80 default_server;
 listen [::]:80 default_server ipv6only=on;

 root /usr/share/nginx/html;
 index index.php index.html index.htm;

 server_name example.com;

 location / {
            try_files $uri $uri/ /index.html;
    }

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
          root /usr/share/nginx/html;
    }


    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;   
        include fastcgi_params;
 }

}

server 
{
 listen 443;

 root /usr/share/nginx/html;
 index index.php index.html index.htm;

 server_name example.com;

 ssl    on;
 ssl_certificate    /etc/ssl/certs/ssl-cert-snakeoil.pem;
 ssl_certificate_key    /etc/ssl/private/ssl-cert-snakeoil.key;


 location / {
            try_files $uri $uri/ /index.html;
    }

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
          root /usr/share/nginx/html;
    }


location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;   
        include fastcgi_params;
 }


}

我映射/ etc / ssl / certs&我运行docker容器时主机上的/ etc / ssl / private文件夹

docker run -dt -p 8080:443 -p 8081:80 -v /t-base/log:/var/log/nginx -v
/etc/ssl/certs:/etc/ssl/certs -v /etc/ssl/private:/etc/ssl/private nginx

Docker ps shows

Up n minutes 0.0.0.0:8081->80/tcp 0.0.0.0:8080->443/tcp <container-name>

并且映射的/ t-base / log文件夹中的nginx错误日志文件保持为空。

docker exec -it <container-name> /bin/bash

接着是

service nginx status

回来后说nginx正在运行。

以上所有都表明一切正常。但是,我发现虽然我能够浏览

http://example.com:8080

打开默认页面

https://example.com:8081

Chrome已向我显示其默认的“悲伤笑脸”错误页面。我看不出我在这里做错了什么。我非常感谢任何帮助。

1 个答案:

答案 0 :(得分:14)

您已互换端口。根据此命令行-p 8080:443 -p 8081:80,您应该执行以下操作:

https://example.com:8080请注意,这是http s

http://example.com:8081

这应该有效