NGINX学习笔记(五):一篇搞懂NGINX配置静态资源服务器

静态资源

NGINX作为静态资源WEB服务器,传输非常高效,常常用于静态资源处理,请求以及动静分离。通常非服务器动态运行生成的文件属于静态资源。
在这里插入图片描述

配置文件

worker_processes auto;

events {
    worker_connections 1024;
}

http {
    # 日志格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

	# 静态资源服务器配置优化, 开始: >>>>>
	# 开启高效的文件传输模式
    sendfile            on;
    # 该指令必须在sendfile打开的状态下才会生效,主要是用来提升网络包的传输'效率'
    tcp_nopush          on;
    # 该指令必须在keep-alive连接开启的情况下才生 效,来提高网络包传输的'实时性'
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

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

    # 开启gzip压缩功能
    gzip on;
	# 进行压缩响应页面的最小长度,当返回内容大于此值时才会使用gzip进行压缩,单位为字节。
	# 当值设为0时,所有页面都进行压缩。
    gzip_min_length 1k;
	# gzip压缩级别
    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/gif image/png;
    # 往头信息中添加压缩标识
	gzip_vary on;
	# 对IE6以下的版本都不进行压缩
    gzip_disable "MSIE [1-6]\.";
    
    # 静态资源服务器配置优化, 结束: <<<<<
    
    upstream hadoopx.cn{
	    server localhost:17080;
    }

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /opt/my-mall/web;

        location /my.api {
            proxy_pass http://hadoopx.cn/my.api;
            proxy_set_header Host              $http_host;
            proxy_set_header X-Real-IP         $remote_addr;
			proxy_set_header X-Forwarded-For   $remote_addr;
			proxy_set_header X-Forwarded-Proto $scheme;
        }
		
        location ~ .*\.(gif|jpg|jpeg|png|flv|mp4)$ {
			expires 90d;
			# 指定图片存放路径
            root /opt/my-mall/web/static/;
	   
            # 关闭重定向
            proxy_redirect          off;
			
            proxy_set_header Host              $http_host;
            proxy_set_header X-Real-IP         $remote_addr;
			proxy_set_header X-Forwarded-For   $remote_addr;
			proxy_set_header X-Forwarded-Proto $scheme;
			
			# 当请求的文件在本地的prox_temp_path指定的目录下不存在时, 再向源站请求拉取
			if ( !-e $request_filename) {
                proxy_pass  http://127.0.0.1:80;
            }
        }
        location / {
            root    /opt/my-mall/web/;
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
            if (!-e $request_filename) {
                rewrite ^/h5/* /h5/index.html last;
                break;
            }
        }
    }
}
压缩提示
  • 过高的压缩比比较耗费服务端性能;
  • 随着压缩级别的升高,压缩比有所提高,但到了级别6后,很难再提高;
  • 随着压缩级别的升高,处理时间明显变慢;
  • gzip很消耗cpu的性能,高并发情况下cpu达到100%。
PROXY_REDIRECT指令
①. 作用

proxy_redirect指令用来修改被代理服务器返回的响应头中的Location头域和“refresh”头域。

②. 语法
proxy_redirect 旧地址 新地址;
proxy_redirect default;  # 默认配置
proxy_redirect off;      # 关闭重定向
PROXY_SET_HEADER指令
①. 官网

官网:PROXY_SET_HEADER指令

②. 作用

proxy_set_header指令用来设定被代理服务器接收到的HEADER的相关信息。如果不设置proxy_set_header,则默认HOST的值为proxy_pass 设置的域名或者IP(一般写IP),即 $proxy_host 的值

③. 语法
# HOST为:IP,不含端口
proxy_set_header Host $host;
# HOST为:IP:PORT,包含端口
proxy_set_header Host $http_host;
④. 常见设置
proxy_set_header Host              $http_host;
proxy_set_header X-Real-IP         $remote_addr;
proxy_set_header X-Forwarded-For   $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
# 如果某个请求头的值为空, 那么这个请求头将不会传送给后端服务器
proxy_set_header Accept-Encoding "";
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cloneme01

谢谢您的支持与鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值