分享免费的编程资源和教程

网站首页 > 技术教程 正文

nginx 代理设置一 之常见的设置 nginx设置代理地址

goqiw 2024-10-19 06:03:29 技术教程 31 ℃ 0 评论

1、nginx代理配置 proxy_pass

server {
	listener 8099;

 location = /test {
		proxy_pass http://192.168.18.132:9010;
	} 
}

2、nginx 代理中设置请求头 proxy_set_header

server {
	listener 8099;
	
 	location = /test {
			proxy_pass http://192.168.18.132:9010;
  		# 设置请求头
  		proxy_set_header Host $proxy_host;
	} 
}

3、nginx 反向代理中设置请求超时时间 proxy_send_timeout

server {
	listener 8099;
  
 	location = /test {
			proxy_pass http://192.168.18.132:9010;
  		# 设置请求头
  		proxy_set_header Host $proxy_host;
  		# 设置请求超时时间  默认是60s
      proxy_send_timeout 30s;
	} 
}

4、nginx反向代理中设置 请求响应超时时间 proxy_read_timeout

server {
listener 8099;
  			
 location = /test {
			proxy_pass http://192.168.18.132:9010;
  		# 设置请求头
  		proxy_set_header Host $proxy_host;
  		# 设置请求超时时间  默认是60s
      proxy_send_timeout 30s;
  		# 设置请求响应超时时间 默认是60s
  		proxy_read_timeout 10s;
	} 
}

5、nginx反向代理中设置 允许的响应头 proxy_pass_header 和 需要隐藏的响应头 proxy_hide_header

server {
listener 8099;
  			
 location = /test {
		proxy_pass http://192.168.18.132:9010;
  
  	# 设置请求头
  	proxy_set_header Host $proxy_host;
  
  	# 设置请求超时时间  默认是60s
    proxy_send_timeout 30s;
  
  	# 设置请求响应超时时间 默认是60s
  	proxy_read_timeout 10s;
  	
  	proxy_pass_header xxxx;  # 放行
    proxy_hide_header abc;   # 隐藏
	} 
}

6、重新设置cookie的path proxy_cookie_path

server {
listener 8099;
  
 location = /test {
	 proxy_pass http://192.168.18.132:9010;
  
   # 设置请求头
   proxy_set_header Host $proxy_host;
  
   # 设置请求超时时间  默认是60s
   proxy_send_timeout 30s;
  
   # 设置请求响应超时时间 默认是60s
   proxy_read_timeout 10s;
   
   proxy_pass_header xxxx;  # 放行
   proxy_hide_header abc;   # 隐藏
              
   proxy_cookie_path /user/ /;  #意味着 path=/user/some/uri/ 改成 path=/some/uri/
	} 
}

7、设置cookie 的属性 proxy_cookie_flags(1.19.3以上版本才有)

server {
	listener 8099;
  			
 	location = /test {
			proxy_pass http://192.168.18.132:9010;
  
  		# 设置请求头
  		proxy_set_header Host $proxy_host;
  
  		# 设置请求超时时间  默认是60s
      proxy_send_timeout 30s;
  
  		# 设置请求响应超时时间 默认是60s
  		proxy_read_timeout 10s;
  		
  		proxy_pass_header xxxx;  # 放行
      proxy_hide_header abc;   # 隐藏
      
      proxy_cookie_path /user/ /; #意味着 path=/user/some/uri/ 改成 path=/some/uri/
      
        # 设置cookie属性相关举例 如ecure, httponly, samesite=strict, samesite=lax, samesite=none
  			# 相反 nosecure, nohttponly, nosamesite 表示去掉相关属性
       proxy_cookie_flags username secure; # 意味着 给username这个cookie增加secure 属性
       proxy_cookie_flags ~ nosecure samesite=strict; # 意味着 删除secure属性,增加samesite=strict属性
		} 
}

8、设置DNS服务 resolver resolver_timeout

server {
	listener 8099;
  			
 	location = /test {
  		# 设置代理DNS解析
  		resolver 127.0.0.1 [::1]:5353;
  		resolver_timeout 10s; #默认30s
      # xxx.com.cn ===> 192.168.18.132
			proxy_pass http://xxx.com.cn:9010;

  		# 设置请求头
  		proxy_set_header Host $proxy_host;

  		# 设置请求超时时间  默认是60s
      proxy_send_timeout 30s;
  		
     # 设置请求响应超时时间 默认是60s
  		proxy_read_timeout 10s;
  		
  		proxy_pass_header xxxx;  # 放行
      proxy_hide_header abc;   # 隐藏
      
      proxy_cookie_path /user/ /; #意味着 path=/user/some/uri/ 改成 path=/some/uri/
       
        # 设置cookie属性相关举例 如ecure, httponly, samesite=strict, samesite=lax, samesite=none
  			# 相反 nosecure, nohttponly, nosamesite 表示去掉相关属性
       proxy_cookie_flags username secure; # 意味着 给username这个cookie增加secure 属性
       proxy_cookie_flags ~ nosecure samesite=strict; # 意味着 删除secure属性,增加samesite=strict属性
		} 
}

9、设置读取客户端数据超时时间(指前后两次读取的时间间隔) client_body_timeout

server {
	listener 8099;
  			
 	location = /test {
  	# 设置代理DNS解析
  	resolver 127.0.0.1 [::1]:5353;
  	resolver_timeout 10s; #默认30s
    # xxx.com.cn ===> 192.168.18.132
		proxy_pass http://xxx.com.cn:9010;

  	# 默认60s, 意味着前后两次读取的时间超过10s(不是指全部读取时间)就返回 408 (Request Time-out)错误 
  	client_body_timeout 10s; 
  } 
}

10、设置客户端的最大请求体 client_max_body_size

server {
	listener 8099;
  			
	location = /test {
  		# 设置代理DNS解析
  		resolver 127.0.0.1 [::1]:5353;
  		resolver_timeout 10s; #默认30s
      # xxx.com.cn ===> 192.168.18.132
			proxy_pass http://xxx.com.cn:9010;
  		# 默认60s, 意味着前后两次读取的时间超过10s(不是指全部读取时间)就返回 408 (Request Time-out)错误 
  		client_body_timeout 10s; 
  		client_header_timeout 10s; #读取【全部】请求头的超时时间,超过则报 408 (Request Time-out)错误 
  	
      # 设置客户端最大请求体
			client_max_body_size 512k; #默认1M,超过则报 413 (Request Entity Too Large) error
      .......
     } 
}

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表