【问题标题】:implement https on nginx config file在 nginx 配置文件上实现 https
【发布时间】:2019-02-21 21:11:37
【问题描述】:

我有一个用 vuejs 编写的单页应用程序 我想使用 digitalocean、nginx 和 docker 部署应用程序

我的问题是, 我需要在配置文件/docker 文件中添加什么 使应用程序将使用 https 而不是 http? (我将使用自签名证书)

是的,我已经搜索过这个问题,但似乎没有任何效果 使用那个模板。

另外,这个应用程序的后端是带有 express 的 nodejs 这将在同一个数字海洋服务器上的不同容器上

我正在使用来自 vue docs 的 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

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

谢谢。

【问题讨论】:

    标签: docker nginx vue.js single-page-application digital-ocean


    【解决方案1】:

    只需在构建期间复制 SSL 和 conf 并将它们保存在 docker 文件根目录中的 configs/nginx/

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

    /etc/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
    

    【讨论】:

    • 嗨!感谢您的回答,我没有像您所说的那样,但仍然无法正常工作,转发到 443 工作正常,但应用程序不向用户提供服务,不在端口 80 或端口 443 中。页面无法访问,在日志上我可以看到对端口 80 的请求,但当它变为端口 443 请求时。页面加载失败
    • 请分享确切的模板,以便我尝试使用您的模板。这应该工作
    • docker file bitbucket.org/boomeryo/boomer3/src/master/client/Dockerfile nginx stuff(包括证书):bitbucket.org/boomeryo/boomer3/src/master/client/nginx_config auto build on dockerhub: (tag name: latest) hub.docker.com/r/omer2500/boomer3/builds 运行容器的命令:docker run -p 80 :80 --rm --name 应用程序 omer2500/boomer3:latest
    • 让我试试你的模板
    • 谢谢,我这里给的链接可以吗?
    猜你喜欢
    • 2016-08-23
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-08
    • 2017-08-03
    • 1970-01-01
    相关资源
    最近更新 更多