跳转到主要内容

nginx

  • docker-compose.yaml
version: "3"
services:
  mobile-office-web:
    image: docker.io/nginx:latest
    restart: always # 自动重启
    privileged: true
    ports:
      - 9095:80
    # volumes:
    #   - ./html:/usr/share/nginx/html
    #   - ./default.conf:/etc/nginx/conf.d/default.conf
    environment:
      - TZ=Asia/Shanghai
  • default.conf
# touch default.conf

server {
    listen 80;
    #listen 443 ssl;
    server_name localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    #ssl_certificate     /home/ssl/server.crt;
    #ssl_certificate_key /home/ssl/server.key;
    #### 性能优化 ####
    # 开启gzip压缩
    gzip on;
    # 压缩哪些文件类型
    gzip_types text/plain text/html text/css application/json text/javascript application/javascript;
    # 最小压缩大小,小于这个大小不压缩,单位是字节
    gzip_min_length 1000;
    # 压缩率,1-9,数字越大压缩的越好,但是也越消耗CPU
    gzip_comp_level 6;
    # 是否在http header中添加Vary: Accept-Encoding,建议开启
    gzip_vary on;
    # 禁止IE6使用gzip,因为这些浏览器不支持gzip压缩
    gzip_disable "MSIE [1-6]\.";
    # 在特定条件下对代理服务器的响应进行压缩
    gzip_proxied expired no-cache no-store private auth;
    # 缓冲区数量和大小,设置了16个8KB的缓冲区
    gzip_buffers 16 8k;
    # 最小http版本,低于这个版本不压缩
    gzip_http_version 1.1;
    #### 性能优化 ####

    #### 安全设置 ####
    # 禁止跨站脚本攻击
    add_header X-XSS-Protection "1; mode=block";
    # 禁止网页被嵌入到iframe或者frame中
    add_header X-Frame-Options "SAMEORIGIN";
    # 启用了 HSTS (HTTP 严格传输安全)
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";
    # 防止浏览器对响应的内容类型进行 MIME 类型嗅探
    add_header X-Content-Type-Options "nosniff";
    #### 安全设置 ####

    root /usr/share/nginx/html;
    index index.html;

    location / {
        # 不缓存首页,解决VUE单页面发版后不生效
        add_header Cache-Control "no-cache no-store must-revalidate proxy-revalidate,max-age=0";
        add_header Last-Modified $date_gmt;
        # 这个有顺序,需要加在后面
        etag off;
    }

    location /api/ {
        proxy_pass http://project-manager:8080;
        proxy_set_header Host $http_host;
    }
}

nginx经常被扫描的几个安全设置

# 增加到 server 区块
####server 安全设置 ####{

    # 禁止跨站脚本攻击
    add_header X-XSS-Protection "1; mode=block";
    # 禁止网页被嵌入到iframe或者frame中
    add_header X-Frame-Options "SAMEORIGIN";
    # 启用了 HSTS (HTTP 严格传输安全)
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";
    # 防止浏览器对响应的内容类型进行 MIME 类型嗅探
    add_header X-Content-Type-Options "nosniff";
#### 安全设置 ####}