реализовать https в конфигурационном файле nginx - PullRequest
0 голосов
/ 18 сентября 2018

У меня есть одностраничное приложение, написанное с помощью vuejs, и я хочу развернуть приложение, используя digitalocean, nginx и docker

Мой вопрос: что мне нужно добавить в файл конфигурации / файл докера, чтобы сделатьчто приложение будет использовать https, а не http?(я собираюсь использовать самозаверяющие сертификаты)

да, я уже искал вопрос, но, похоже, ничего не работает с этим шаблоном.

также, серверная часть для этого приложения является nodejs сexpress, который будет находиться в другом контейнере на одном и том же цифровом океаническом сервере

Я использую шаблон файла docker из vue docs:

dockerfile:

# build stage
FROM node:9.11.1-alpine as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

# production stage
FROM nginx:1.13.12-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

nginx.conf:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

Спасибо.

1 Ответ

0 голосов
/ 18 сентября 2018

Просто скопируйте SSL и conf во время сборки, сохраните оба в configs/nginx/ в корне вашего файла Docker

# build stage
FROM node:9.11.1-alpine as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

COPY configs/nginx/conf.d/ /etc/nginx/conf.d/
COPY configs/nginx/nginx.conf /etc/nginx/nginx.conf
COPY configs/nginx/ssl/ /etc/nginx/ssl/
RUN rm -rf /etc/nginx/conf.d/default.conf
# production stage
FROM nginx:1.13.12-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

ваш nginx.conf

#test comment4
user  nginx;
daemon off;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  120;

    gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

/ и т.д. / Nginx / conf.d / mydoamin.com

server {
        listen          80;
        server_name     mydomain.com default_server;
        add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
        expires off;
        return  301     https://$server_name$request_uri;
}
server {
        listen 443 ssl;
        server_name mydomain.com;
        client_max_body_size 32M;
        add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";

        ssl_certificate      /etc/nginx/ssl/mydomain.bundle.crt;
        ssl_certificate_key  /etc/nginx/ssl/mydomain.com.key;

  root "/usr/share/nginx/html";
  index index.html index.htm index.php; #####fruther config####
  }

Таким образом, команда обновления будет

 docker run -it --add-host boomerb2b.com:192.168.1.23 -p 443:443 -p 80:80 --rm --name app nginx-ssl:latest

Кроме того, добавьте значение в / etc / hosts, если не разрешаете домен

vim /etc/hosts/ 
192.168.1.23 boomerb2b.com
...