Nginx 相关知识点和配置

2019-09-29  本文已影响0人  Joah_l

全局变量

Nginx 设置反向代理

  location ^~ /api/ {
      proxy_pass        http://127.0.0.1:7071;
      proxy_redirect off;
      # 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
      proxy_set_header  Host  $host;
      proxy_set_header  X-Real-IP  $remote_addr;
      proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
      proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
  }

前端使用 nginx 部署 React Spa (Browser History) 的特殊配置

  location / {
    root: path;
    index: index.html;
    try_files try_files $uri /index.html;
  }

nginx 配置 https 和 开启 h2

  server {
    listen     443 ssl http2;
    server_name  106.53.94.167;
    root   /home/ubuntu/workspace/mpa-dance-admin/dist;

    charset utf-8;

    ssl_certificate             hxedu.top.crt;
    ssl_certificate_key         hxedu.top.key;
    ssl_session_cache           shared:SSL:1m;
    ssl_session_timeout         5m;
    ssl_protocols               SSLv2 SSLv3 TLSv1.2;
    ssl_ciphers                 ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_prefer_server_ciphers   on;
    location / {
      <!-- 具体的配置 -->
    }
  }

nginx 开启 gzip

  ##
  # Gzip Settings
  ##
  gzip on;
  gzip_vary on;
  gzip_min_length 1k;
  # gzip_proxied any;
  gzip_comp_level 2;
  # gzip_buffers 16 8k;
  # gzip_http_version 1.1;
  gzip_types text/csv text/xml text/css text/plain text/javascript application/javascript application/x-javascript application/json application/xml;

nginx 日志配置

可以给每一个 HTTP, Server, Location 定义一个日志

Nginx 的浏览器本地缓存设置

http {......} 中间配置, 启动压缩

  location ~ .*\.(gif|jpg|png)$ {
    expires 30d
  }

Nginx 设置限速 limit_rate

  location /download {
    limit_rate 256k;
    proxy_pass http://1.2.3.4
  }

  location /movie {
    limit_rate_after 10m;
    limit_rate 100k;
    if ($http_user_agent ~ Google|baidu) {
      limit_rate 20k;
    }
  }
上一篇下一篇

猜你喜欢

热点阅读