记录一个 nginx 配置

docker-compose

  • docker-compose.yml
version: '3.1'
services: 
  nginx:
    restart: always
    image: nginx:1.19.2-alpine
    container_name: xujiayi-blog-nginx
    ports: 
      - 80:80
      - 443:443
    volumes: # 外部路径(服务器路径):内部路径(容器路径)      - 
      - "/home/ubuntu/app/files:/usr/app/files"
      - "./conf/nginx.conf:/etc/nginx/nginx.conf"
      - "./wwwroot:/usr/share/nginx/wwwroot"
      - "./log:/var/log/nginx"
yml

上面 volumes 是挂载(映射),将本地路径挂载到(映射)内部路径。

nginx

  • nginx.conf
user  nginx;
worker_processes  1;

events {
  worker_connections  1024;
}

http {

  # 开启gzip 压缩,提升网站访问速度相关配置
  gzip on;
  # 开始压缩的最小长度(再小就不要压缩了,意义不在)
  gzip_min_length 1k;
  # 推荐6 压缩级别(级别越高,压的越小,越浪费CPU计算资源)
  gzip_comp_level 6;
  gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/jpg image/png;
  # 是否传输gzip压缩标志
  gzip_vary on;
  # 配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持)
  gzip_disable "MSIE [1-6]\.";

  #服务器,多个可以实现负载均衡
  upstream web-server {
    server ***.***.***.***:**** weight=1;
  }

  include mime.types;
  default_type  application/octet-stream;


  sendfile        on;
  keepalive_timeout  65;

  server {
    #http请求
    listen 80;
    server_name  你的域名;
      #重定向为https
    rewrite ^(.*) https://$server_name$1 permanent;
  }
  server {

    #https请求
    listen       443 ssl;
    server_name  www.hyz.cool;

    # https的证书文件路径
    ssl_certificate      web.hyz.cool.pem;
    # https的证书文件路径
    ssl_certificate_key  web.hyz.cool.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_ciphers请上阿里云查看
    ssl_prefer_server_ciphers on;


    location / {
      root    /usr/share/nginx/wwwroot/html;
      index  index.html index.htm;
      try_files $uri $uri/ /index.html;
    }

    #配置子路径:
    #让https://你的域名/subroot 指向内部路径wwwroot/html/subroot,映射之后实际指向外部路径./wwwroot/html/subroot
    location /subroot/ {
      root    /usr/share/nginx/wwwroot/html;
      index  index.html index.htm;
      try_files $uri $uri/ /index.html;
    }

    #配置服务:
    #让https://你的域名/server 指向 http://web-server/ (此处的web-server 为前面配置的web-server服务 )
    location ^~ /server/ {
      proxy_pass  http://web-server/;
    }
    #屏蔽子路径:
    location ^~ /doc.html {
      rewrite ^(.*) https://你的域名;
    }
    
    #这里将https://你的域名//files/请求指向了容器内部路径/usr/app/,结合前面的路径映射,实际指向外部路径/home/ubuntu/app/
    location ^~ /files/ {
      root  /usr/app/;
    }

  }
}


nginx

欢迎关注我的微信公众号

续加仪

打赏
  • 微信
  • 支付宝
评论
来发评论吧~
···

歌手: