美文网首页Docker nginx
docker nginx配置https、ssl

docker nginx配置https、ssl

作者: 简书首席大臣 | 来源:发表于2020-03-10 11:54 被阅读0次
    1. 准备工作

      (1)确保已安装docker和nginx

      (2)签名文件

      (3)域名

    2. 创建nginx映射文件、目录

    mkdir /root/docker-nginx
    
    //存放nginx配置文件
    mkdir /root/docker-nginx/conf.d
    
    //存放nginx日志(方便查错)
    mkdir /root/docker-nginx/logs
    
    //存放签名文件
    mkdir /root/docker-nginx/ssl
    
    //项目
    mkdir /root/docker-nginx/www
    
    
    1. 在/root/docker-nginx/www 目录下创建个index.html文件,方便查看结果
    cd /root/docker-nginx/www
    vim index.html
    

    index.html 页面内容如下

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>title</title>
    </head>
    <body>
    <h1>恭喜你,配置成功!</h1>
    </body>
    </html>
    
    1. 在/root/docker-nginx/ssl目录下存放你的签名证书

    2. 在/root/docker-nginx下创建nginx默认配置文件

    cd  /root/docker-nginx
    vim nginx.conf
    

    下面是nginx提供的默认配置

    user www-data;
    worker_processes auto;
    pid /run/nginx.pid;
    include /etc/nginx/modules-enabled/*.conf;
    
    events {
        worker_connections 768;
        # multi_accept on;
    }
    
    http {
    
        ##
        # Basic Settings
        ##
    
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;
    
        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;
    
        include /etc/nginx/mime.types;
        default_type application/octet-stream;
    
        ##
        # SSL Settings
        ##
    
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;
    
        ##
        # Logging Settings
        ##
    
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
    
        ##
        # Gzip Settings
        ##
    
        gzip on;
        gzip_disable "msie6";
    
        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    
        ##
        # Virtual Host Configs
        ##
    
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
    }
    
    
    #mail {
    #   # See sample authentication script at:
    #   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
    # 
    #   # auth_http localhost/auth.php;
    #   # pop3_capabilities "TOP" "USER";
    #   # imap_capabilities "IMAP4rev1" "UIDPLUS";
    # 
    #   server {
    #       listen     localhost:110;
    #       protocol   pop3;
    #       proxy      on;
    #   }
    # 
    #   server {
    #       listen     localhost:143;
    #       protocol   imap;
    #       proxy      on;
    #   }
    #}
    

    上面配置include /etc/nginx/conf.d/*.conf;这行可知,文件会包含conf.d目录下的所有.conf后缀的配置文件,所以我们只需要在conf.d目录下创建自己的配置文件即可。

    1. 在 /root/docker-nginx/conf.d 目录下创建default.conf配置文件
    cd /root/docker-nginx/conf.d
    vim default.conf
    

    内容如下

    server {
        listen       80;
        server_name  你的域名;
        #转发到http
        rewrite ^(.*)$ https://$host$1 permanent;
    }
    server {
        listen 443 ssl;
        server_name  你的域名;
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        ssl_certificate   /root/ssl/你的证书.pem;
        ssl_certificate_key  /root/ssl/你的证书.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
    }
    
    1. 启动nginx容器
    docker run -d \
    -p 80:80 -p 443:443\
    -v /root/docker-nginx/conf.d:/etc/nginx/conf.d:ro \
    -v /root/docker-nginx/nginx.conf:/etc/nginx/nginx.conf \
    -v /root/docker-nginx/logs:/var/log/nginx \
    -v /root/docker-nginx/www:/usr/share/nginx/html \
    -v /root/docker-nginx/ssl:/root/ssl \
    --name nginx \
    nginx:latest 
    

    ==特别注意:这里需要把443端口也映射出去==

    写在最后:
    以上只说明了nginx的一些相关配置。还有些其他配置需要注意,比如linux开发端口。如果是阿里云服务器,还需要在安全组开放端口

    相关文章

      网友评论

        本文标题:docker nginx配置https、ssl

        本文链接:https://www.haomeiwen.com/subject/vvqwdhtx.html