Home > Net >  SSL Certificate fails to load with Nginx in Docker
SSL Certificate fails to load with Nginx in Docker

Time:10-20

I have a Dockerized web app that I am trying to deploy. I am using Nginx and uWSGI to serve the static files and manage traffic.

I am stuck with a problem for a while. I want to serve the app over HTTPS as well (obviously).

I am adding relevant information here and if anything more is needed please let me know.

Directory structure:

root
|
'-> app (django app)
'-> proxy (nginx)
    '-> Dockerfile (calling it Nginx-Dockerfile in the remainder of the post)
    '-> data/certs/
        '-> example.com.crt
        '-> example.com.key
    '-> default.conf (configuration for nginx)
    '-> ... (some more files, irrelevant to the problem)
'-> docker-compose-deploy.yml
'-> Dockerfile (calling it root-Dockerfile in the remainder of the post)
'-> ... (some more files, irrelevant to the problem)

As shown above, I have already created the SSL certificates.

Nginx-Dockerfile:

FROM nginxinc/nginx-unprivileged:1-alpine
LABEL maintainer="hackacademy.in"

COPY ./default.conf.tpl /etc/nginx/default.conf.tpl
COPY ./uwsgi_params /etc/nginx/uwsgi_params
COPY ./data/certs/example.com.crt /etc/nginx/certs
COPY ./data/certs/example.com.key /etc/nginx/certs
COPY ./run.sh /run.sh

ENV LISTEN_PORT=8000
ENV APP_HOST=app
ENV APP_PORT=9000

USER root

RUN mkdir -p /vol/static && \
    chmod 755 /vol/static && \
    touch /etc/nginx/conf.d/default.conf && \
    chown nginx:nginx /etc/nginx/conf.d/default.conf && \
    chmod  x /run.sh && \
    chmod  rx /etc/nginx/certs

VOLUME /vol/static

USER nginx

CMD ["/run.sh"]

default.conf

server {
        listen 443 ssl;
        server_name example.com;
        ssl_certificate /etc/nginx/certs/example.com.crt;
        ssl_certificate_key /etc/nginx/certs/example.com.key;

        location /static {
                alias /vol/web/;
        }

        location / {
                uwsgi_pass ${APP_HOST}:${APP_PORT};
                include /etc/nginx/uwsgi_params;
                client_max_body_size 10M;
        }
}

server {
        listen 80;
        server_name example.com;

        location / {
                return 301 https://$host$request_uri;
        }
}

This successfully builds the image but nginx server is giving errors and doesn't actually serve the purpose. The error it shows in the logs is

nginx: [emerg] cannot load certificate "/etc/nginx/certs/example.com.crt": BIO_new_file() failed (SSL: error:02001014:system library:fopen:Not a directory:fopen('/etc/nginx/certs/example.com.crt','r') error:2006D002:BIO routines:BIO_new_file:system lib)

I have done my fair bit of experimentation and googling and I am getting stuck in some loop here. I am deploying a webapp for the first time, heck I am not even a web developer (I am learning as I go), so please help me out in simple terms. Will be really grateful.

Note: I have replaced my domain with example.com in this post at all places.

CodePudding user response:

I found the solution.

I changed Nginx-Dockerfile to

FROM nginx:1.21.3
LABEL maintainer="hackacademy.in"

COPY ./default.conf.tpl /etc/nginx/default.conf.tpl
COPY ./uwsgi_params /etc/nginx/uwsgi_params
COPY ./data/certs/example.com.crt /etc/nginx/certs/
COPY ./data/certs/example.com.key /etc/nginx/certs/
COPY ./run.sh /run.sh

ENV LISTEN_PORT=8000
ENV APP_HOST=app
ENV APP_PORT=9000

RUN mkdir -p /vol/static && \
    chmod 755 /vol/static && \
    touch /etc/nginx/conf.d/default.conf && \
    chown nginx:nginx /etc/nginx/conf.d/default.conf && \
    chmod  x /run.sh && \
    chmod  r /etc/nginx/certs/example.com.crt && \
    chmod  r /etc/nginx/certs/example.com.key

VOLUME /vol/static

CMD ["/run.sh"]
  • Related