Home > Mobile >  Running Nginx Docker with SSL self signed certificate
Running Nginx Docker with SSL self signed certificate

Time:10-28

I am trying to run a UI application with Docker using nginx image I am able to access the service on port 80 without any problem but whenever I am trying access it via https on 443 port I am not able to access the applications the site keeps loading and eventually results in not accessible I have updated the nginx.conf file in default.conf to allow access over port 443

Following is my nginx.conf

charset utf-8;

server {
    listen 80;
    server_name localhost;
    root /usr/nginx/html;
}

server {
    listen 443;
    server_name localhost;
    root /usr/nginx/html; 
}

I have added the SSL self-signed certificate in the /usr/nginx folder and exposed port 443 via Dockerfile

The following is my Dockerfile

FROM nginx

COPY dist /usr/nginx/html
RUN chmod -R 777 /usr/nginx/html/*

COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY domain.crt /usr/nginx

EXPOSE 80:443
ENTRYPOINT nginx -g 'daemon off;'

Can anyone please explain me is port 443 not allowing any access

CodePudding user response:

For nginx server to allow SSL encryption you need to provide ssl flag while listening in nginx.conf and only ssl certificate will not be sufficient you will need the ssl certificate key and password as well and they must be configured

charset utf-8;

server {
    listen 80;
    server_name localhost;
    root /usr/share/nginx/html;
}

server {
    listen 443 ssl;
    ssl_certificate /usr/nginx/ssl.crt;
    ssl_certificate_key /usr/nginx/ssl.key;
    ssl_password_file /usr/nginx/ssl.pass;
    server_name localhost;
    root /usr/nginx/html;
}

and you need to put the ssl certificate, key and password via volumes or via embedding in docker container if you are running container over kubernetes cluster adding them via kubernetes secrets will be better option For Dockerfile you can add like

FROM nginx

COPY dist /usr/nginx/html
RUN chmod -R 777 /usr/nginx/html/*

COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY ssl.crt /usr/nginx/
COPY ssl.pass /usr/nginx/
COPY ssl.key /usr/nginx/

EXPOSE 80:443
ENTRYPOINT nginx -g 'daemon off;'

For further info you can refer the Nginx Docker article https://medium.com/@agusnavce/nginx-server-with-ssl-certificates-with-lets-encrypt-in-docker-670caefc2e31

  • Related