Docker容器内的Nginx SSL [英] Nginx SSL inside a docker container

查看:222
本文介绍了Docker容器内的Nginx SSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经配置了一个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;

位置/ {
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;
}


位置〜\.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;
包含fastcgi_params;
}

}

服务器
{
听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;


位置/ {
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;
}


位置〜\.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;
包含fastcgi_params;
}


}

我映射/ etc / ssl / certs& / etc / ssl / host文件夹,当我运行docker容器

  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 nginx

Docker ps显示

上n分0.0.0.0:8081->80/tcp 0.0.0.0:8080->443/tcp<容器名称>

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

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

后跟

 服务nginx状态

刚刚回来,说nginx正在运行。



以上所有内容都表明一切正常。但是,我发现,虽然我可以浏览到

  http://example.com:8080 

打开默认页面

  https://example.com:8081 

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

解决方案

您已经互换了端口。根据这个命令行 -p 8080:443 -p 8081:80 ,你应该这样做:



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



http://example.com:8081



这应该工作


I have configured a Docker container to run Nginx and setup the /etc/nginx/sites-available/default file as shown below

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;
 }


}

I map the /etc/ssl/certs & /etc/ssl/private folders on the host when I run the docker container

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>

and the nginx error log file inside the mapped /t-base/log folder stays empty.

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

followed by

service nginx status

just comes back and says that nginx is running.

All of the above would indicate that everything is working correctly. However, I find that whilst I am able to browse to

http://example.com:8080

turns up the default page

https://example.com:8081

has the Chrome showing me its default "sad smiley" error page. I cannot see what I might be doing wrong here. I'd much appreciate any help.

解决方案

You have interchanged the ports. According to this command line -p 8080:443 -p 8081:80, you should do:

https://example.com:8080 note this is https

and

http://example.com:8081

This should work

这篇关于Docker容器内的Nginx SSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆