大网数据广告活动
服务器运维问答

Can't turn SSL on a docker's nginx container

· 52CE 我爱测

I'm new to docker and try to build an image based on Alpine to take care of my websites with HTTPS connexions. Currently i'm at the step to make it work from the container so i'm not finished yet with my Dockerfile my Dockerfile …

问题原文

原帖链接 · idiocrate · CC BY-SA 4.0

I'm new to docker and try to build an image based on Alpine to take care of my websites with HTTPS connexions. Currently i'm at the step to make it work from the container so i'm not finished yet with my Dockerfile my Dockerfile :

FROM alpine


RUN apk update \
        && apk upgrade \
        && apk add nginx php7 php7-fpm php7-opcache php7-gd php7-mysqli php7-zlib php7-curl \
        && mkdir /run/nginx
EXPOSE 80

From there i start my container :

docker run -ti -p 80:80 -p 443:442 -v /home/docker/web/conf:/etc/nginx/conf.d/ -v /home/docker/web/www:/var/www/localhost/htdocs test

I then install certbot-nginx, and start it to generate my certificate, which i copy in my /var/www/localhost/htdocs/example.fr/ to access it from outside the container. Finally i start php-fpm7 & nginx.

My nginx config :

server {
        server_name example.fr;
        root /var/www/localhost/htdocs/example.fr;
        index index.html index.htm index.php;
        listen [::]:443 ssl;
        server_tokens off;
        # configure php
        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
                index index.html index.htm index.php;
        }

        location ~* \.pem {
                deny all;
        }

        ssl_certificate /var/www/localhost/htdocs/example.fr/fullchain.pem; 
        ssl_certificate_key /var/www/localhost/htdocs/example.fr/privkey.pem; # managed by Certbot

}

server {
    if ($host = example.fr) {
        return 301 https://$host$request_uri;
    }
       # managed by Certbot
       server_name example.fr;
       listen 80;
       listen [::]:80;
       #return 404;
}

I'm probably missing something simple but i'm stuck since a week. thanks !

已采纳解答

原帖链接 · Carlos · CC BY-SA 4.0

Rather than re-invent the wheel, perhaps take a look at some boilerplate projects such as:

https://github.com/wmnnd/nginx-certbot

This covers your needs:

  • nginx
  • certbot

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-03-06)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。