【NGINX】配置访问、文件传输、压缩文件功、资源缓存、防盗链

配置访问:root、alias、error_page

server 
{
  listen 80;
  server_name localhost;

  # 访问
  location ^~/abc {
    default_type text/plain;
    return 200 "Welcome nginx auth";
  }


  # root
  # 当在浏览器中访问/images时,会在/images目录下查找文件  ==>  root路径 + locatiosn路径 
  # 浏览器访问 XX.XX.XX.XX/images/xxx.png   
  # 服务器路径 /nginx_test/images/xxx.png
  location /images {
    root /nginx_test;	 	
  }



  # alias
  # 当在浏览器中访问/images时,会在/nginx_test下查找文件,==>  locatiosn路径 
  # 浏览器访问 XX.XX.XX.XX/images/xxx.png   
  # 服务器路径 /nginx_test/xxx.png
  # 如果location路径是以/结尾,则alias也必须是以/结尾,root没有要求
  location /images {
    alias /nginx_test;	 
    index xx.jpg index.html     # 访问路径后找到默认文件
  }


  # error_page 错误页面配置
  # 第一种:
  error_page 404 http://www.baidu.com;
  # 第二种:
  error_page 500 502 503 /50x.html;
    location /50.html{
    root html;
  }
  # 第三种:
  error_page 404 @jumpto_error;
    location @jumpto_error{
    default_type text/plain;
    return 404 "Not Found";
  }
  # 第四种: 修改重定向后返回的状态码 200前面不能有空格
  error_page 404 =200 /50x.html;
    location /50.html{
    root html;
  }
}

文件传输:sendfile、tcp_nopush、tcp_nodelay

http 
{
    # 在 Linux 2.5.9 以后的版本中,开启 sendfile、tcp_nopush和tcp_nodelay 这三个指令可以提高文件传输的效率和性能。sendfile 可以开启高效的文件传输模式,tcp_nopush 可以确保数据包在发送到客户端之前已经充分填满,减少网络开销,并加快文件发送速度。当最后一个数据包到达时,即使没有充满,Nginx 会忽略 tcp_nopush 参数,并使tcp_nodelay 强制发送数据。因此,tcp_nopush 可以与 tcp_nodelay 一起设置,比单独配置 tcp_nodelay具有更强的性能。
    sendfile       on;  # 可以加速静态文件获取,原理是底层获取文件时少了两次拷贝的步骤
    tcp_nopush     on;  # 先缓存数据,存满后进行发送
    tcp_nodelay    on;	# 有数据就发送
  server { ... }
}

压缩文件功能 :gzip (与sendfile模块一起使用时需要添加 gzip_static on;否则就禁用sendfile)

# 位置:http / server / location
http
{

  gzip  on;
  # gzip_types *;      # *表示全部文件都压缩(正式上线不推荐)
  gzip_types application/javascript text/html text/css application/json; # 多类型压缩文件;
  gzip_comp_level 6;   # 提供1~9的压缩等级,数字越大压缩文件越小;
  gzip_vary on;        # 在响应头中添加 Vary: Accept-Encoding;
  gzip_disable "MSIE [1-6]\."; # Nginx 将禁用 IE6 和 IE7 的 gzip 压缩功能;
  gzip_min_length 1000;   # 当超过1000字节才会对文件进行压缩,太小压缩性价比较低;
  
  gzip_proxied 参数;   # 用于配置在代理请求时是否启用 Gzip 压缩功能,功能参数如下:
  # off:禁用 Gzip 压缩,即不对任何代理请求进行压缩。
  # expired:对过期的响应进行压缩。
  # no-cache:对不缓存的响应进行压缩。
  # no-store:对不存储的响应进行压缩。
  # private:对私有响应进行压缩。
  # no_last_modified:不检查 Last-Modified 头部的代理请求进行压缩。
  # no_etag:不检查 ETag 头部的代理请求进行压缩。
  # auth:对需要身份验证的响应进行压缩。
  # any:对所有类型的代理请求进行压缩。
  
  gzip_static  on; # 需要激活ngx_http_gzip_static_module模块,才可以使用。
    
}

配置资源缓存时间:expires

# 位置:http / server / location
server
{
    location ~ .*\.(html|js|css|png|jpg) {
    expires epoch;   # 设置为 UNIX 时间戳 0,即过期时间为 1970 年 1 月 1 日。每次请求不缓存
    expires max;     # 设置为最大过期时间,即 31 年 12 月 31 日。
    expires off;	 # 禁用过期时间。
    expires 1m;		 # 设置1分钟过期 
    # s:秒
    # m:分钟
    # h:小时
    # d:天
    # w:周
    # M:月
    # y:年
  }
}

配置防盗链

server
{
  
    # 也可以针对某个目录下进行增加反盗链 location /images { root html valid_referers... }
    location ~ \.(jpg|jpeg|png|gif)$ {
    
    # valid_referers none  不允许任何引用来源访问资源
    # blocked 后面跟允许的来源
    valid_referers none blocked example.com *.example.com;

    # 如果invalid_referer==1 
    if ($invalid_referer) {   	
      return 403;
    }
    
    
  }
}
posted @   PythonNew_Mr.Wang  Views(159)  Comments(0Edit  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示