Docker下安装Nginx并且配置SSL证书

  • 拉取镜像

    docker pull nginx
    
  • 运行容器

    #先运行一个测试容器,然后将里面的配置文件复制到宿主机
    docker run --name nginx-test -p 80:80 -d nginx
    
  • 创建容器数据卷目录

    mkdir -p 你的目录/nginx/www 你的目录/nginx/logs 你的目录/nginx/conf
    
  • 复制配置文件

    docker cp 容器ID:/etc/nginx/nginx.conf 你的目录/nginx/conf
    
  • 根据自己的需求修改配置文件

    # 配置支持https协议的ssl证书
    user  nginx;
    worker_processes  4;
    
    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 60;
        tcp_nodelay on;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 128k;
        gzip on; 
        gzip_min_length 1k;
        gzip_buffers 4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types text/plain application/x-javascript text/css application/xml;
        gzip_vary on;
    
    	server {
    		listen 443; #监听的端口
      		server_name 你的域名(不带www);
      		ssl on;
      		#crt的全路径
      		ssl_certificate /etc/cert/1.crt;
      		#key的全路径
      		ssl_certificate_key /etc/cert/1.key
      		ssl_session_timeout 5m;
      		ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
      		ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
      		ssl_prefer_server_ciphers on;
      		#这是主页访问地址,因为使用的是静态的html网页,所以直接使用location就可以完成了。
      		location / {
      			root /usr/share/nginx/html;#站点目录
      			index index.html;
      		}
      		
      		location /xxx/ {
      		   proxy_pass http://127.0.0.1:8081/xxx/;
      		   proxy_set_header X-Real-IP $remote_addr;
      		   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      		}
    	}
    	server {
            listen 80; #有可能客户端访问的是http,所以需要将http的请求转发到443
            server_name 你的域名;
            rewrite ^/(.*)$ https:/你的域名:443/$1 permanent;
        }
        
        include /etc/nginx/conf.d/*.conf;
    }
    
  • 运行容器

    docker run -p 80:80 -p 443:443 \
    --name nginx \
    --restart=always \
    -v 你的目录/nginx/www:/usr/share/nginx/html \
    -v 你的目录/nginx/conf/cert:/etc/nginx/cert \
    -v 你的目录/nginx/logs:/var/log/nginx \
    -v 你的目录/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
    nginx
    
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值