Nginx指令(alias、set、proxy_pass、rewrite、upstream)、跨域问题、防盗链、缓存、gzip压缩

常用的配置信息

events {										# 工作模式
    worker_connections  1024;					# 最大连接数
}
http {											# 配置http服务器
    include       mime.types;					# 定义mime的文件类型
    default_type  application/octet-stream;		# 默认文件类型
	sendfile        on;							# 开启 sendfile 函数(zero copy 方式)输出文件
	keepalive_timeout  65;						# 连接超时时间,单位秒
	
	upstream pictureserver {					# 定义负载均衡设备的ip和状态
		server 192.168.225.133:8081 ; 			# 默认权重值为一
		server 192.168.225.133:8082 weight=2; 	# 值越高,负载的权重越高
		server 192.168.225.133:8083 down;		# 当前server 暂时不参与负载
		server 192.168.225.133:8084 backup;		# 当其他非backup状态的server 不能正常工作时,才请求该server,简称热备
	}
	server {									# 设定虚拟主机配置
		listen  80;								# 监听的端口
		server_name  picture.itdragon.com;		# 监听的地址,多个域名用空格隔开
		location / {							# 默认请求 ,后面 "/" 表示开启反向代理,也可以是正则表达式
		   root		html;						# 监听地址的默认网站根目录位置
		   proxy_pass   http://pictureserver;	# 代理转发
		   index  index.html index.htm;			# 欢迎页面
		   deny 127.0.0.1;  					# 拒绝的ip
		   allow 192.168.225.133; 				# 允许的ip
		}
		error_page   500 502 503 504  /50x.html;# 定义错误提示页面     
        location = /50x.html {					# 配置错误提示页面
            root   html;
        }
	}

root:

Sets the root directory for requests. For example, with the following configuration
location /i/ {
    root /data/w3;
}
The /data/w3/i/top.gif file will be sent in response to the “/i/top.gif” request

root响应的路径:配置的路径+完整访问路径(完整的location配置路径+静态文件)
如上访问地址是: /data/w3/i/top.gif
在这里插入图片描述

alias:

Defines a replacement for the specified location. For example, with the following configuration
location /i/ {
    alias /data/w3/;
}
on request of “/i/top.gif”, the file /data/w3/top.gif will be sent.

alias响应的路径:配置路径+静态文件(去除location中配置的路径)
如图访问地址是:/data/w3/top.gif
在这里插入图片描述

set:

在这里插入图片描述

rewrite:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

upstream(负载均衡):

在这里插入图片描述
在这里插入图片描述

proxy_pass:(正向和反向、负载均衡)

在这里插入图片描述

#配置代理
   #=====================================
   #反向代理1
   server{
        listen 89;
        server_name localhost;
        
        #重写后端服务器的location和refresh头
        #proxy_redirect on;  
        location / {
           proxy_pass http://192.168.2.112:8080/;
           index index.html index.htm;
        }
   }
   
   #反向代理2
   server{
        listen 86;
        server_name localhost;
        proxy_redirect off; 
        location / {
           proxy_pass http://127.0.0.1:8011;
           index index.html index.htm;
        }
   }
   #=====================================
   
   
   
   
   #=====================================
   #配置正向代理
   server{
      listen 88;
      resolver 114.114.114.114;
      location / {
         proxy_pass http://$http_host$request_uri;
      }
   }
   

   #配置https正向代理
   #curl -l --proxy 192.168.2.108:443 www.baidu.com
   server{
      resolver 114.114.114.114;
      listen 443;
      location / {
        proxy_pass https://$host$request_uri;
        proxy_buffers 256 4k;
      }
      
   }
   #=====================================
   
   
   
   
   
   #=====================================
   #配置负载均衡
   upstream myFuzaiTest{
   #四种均衡方式。 轮询; weight:权重 ;ip_hash:每个请求会固定一个服务器 ; fair :第三方,按服务器响应时间优先分配(url_hash 按url的hash结果来分配)。
      server 127.0.0.1:8011 weight=3;
      server 127.0.0.1:90   weight=1;
   }
    
    server{
      listen 8077;
      server_name localhost;
      location / {
         proxy_pass http://myFuzaiTest;
         #proxy_set_header Host $host;#将请求头转发给后端服务器
         #proxy_set_header X-Forward_For $remote_addr;#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
      }
    }
   #=====================================

跨域问题:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述

防盗链:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

并且进行上述配置后,图片也是无法下载到本地的

缓存:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

文章大部分转载自此处:链接 ,感谢原创作者分享学习。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值