首页 > 系统相关 >自用nginx配置(常见安全配置,http转https,http和https混合请求,解决http host头攻击漏洞)

自用nginx配置(常见安全配置,http转https,http和https混合请求,解决http host头攻击漏洞)

时间:2023-05-12 11:24:26浏览次数:43  
标签:http log header server nginx proxy https

自用nginx配置(常见安全配置,http转https,http和https混合请求,解决http host头攻击漏洞)

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;
	
	access_log off; 

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    #keepalive_timeout  65;
    keepalive_timeout  60 60;
	
	# 设置请求正文即请求体(request body)的读超时时间
	client_body_timeout 60s;  
    # 指定等待client发送一个请求头的超时时间	
    client_header_timeout 60s; 	
	# 设置将响应传输到客户端的超时
    send_timeout 60s;  
	# 有大文件上传时,需要指定body的最大值
	client_max_body_size 50m;
    # 限制了同一ip1秒钟内最多可以请求20次
    #limit_req_zone $binary_remote_addr zone=reqperip:20m rate=50r/s;
	# 同一ip的请求使用缓存,大小为10,并且不延迟
    #limit_req zone=reqperip burst=50 nodelay;
	 
	# 限制并发数,$binary_remote_addr是限制同一客户端ip地址;
    limit_conn_zone $binary_remote_addr zone=one:10m; 
    # 限制并发数,$server_name是限制同一server最大并发数	
    limit_conn_zone $server_name zone=perserver:10m;   
	# 最大并发连接数100
    limit_conn one  500; 
    # 表示该服务提供的总连接数不得超过2000,超过请求的会被拒绝	
    limit_conn perserver 2000;  
	# 限制下载带宽,limit_rate为限制下载速度;
    limit_rate 5000k;           
	
	
	#隐藏Nginx版本号
	server_tokens off;
		
	#隐藏Nginx后端服务指定Header的状态:
	proxy_hide_header X-Powered-By;
    proxy_hide_header Server;	

	#防止XSS攻击
	add_header X-Frame-Options SAMEORIGIN;
	add_header X-XSS-Protection '1;mode=block';
	add_header X-Content-Type-Options nosniff;
	
    
	###############
    gzip  on;  # 默认压缩配置
    gzip_min_length 2k; # 小于2k的不压缩
    gzip_proxied any; # 无条件压缩
    gzip_comp_level 2; # 压缩级别1~9,越高压缩越小CPU资源消耗越多
    gzip_buffers 16 8k; # 存储压缩结果数据流,以8k为单位,向内存申请16倍
	

    #gzip  on;
	
	#定义负载均衡域名,供后面的http和https使用 test.abc.com 
	upstream server8180 { 
      server 127.0.0.1:8180;   
    }





    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
	
    server {
	    listen       80;
        listen       443 ssl;  
        # 配置Nginx解决http host头攻击漏洞https://blog.csdn.net/weixin_48207312/article/details/128288660
		server_name 127.0.0.1 test.abc.com localhost;
        if ($http_Host !~* ^127.0.0.1|test.abc.com|localhost$)
        {
            return 403;
        }

        ssl_certificate      my_ssl.pem;
        ssl_certificate_key  my_ssl.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
		
		
		
		
		# 自定义错误页(关键参数:这个变量开启后,我们才能自定义错误页面,当后端返回404,nginx拦截错误定义错误页面)
        proxy_intercept_errors on;
        error_page  404  /404.html;
        # 承接上面的location。

        location = /404.html {
            # 放错误页面的目录路径。
            root   html;
        }		
		
	    location /html {
            # 放错误页面的目录路径。
            root   html;
        }		
		
		#强制将http重定向到https
		if ($scheme = http) {
           return 307 https://$host$request_uri;
        }
		
		#解决Nginx HTTP服务器的报错“400 Bad Request: The plain HTTP request was sent to HTTPS port”
		
		location ~/ {
			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_set_header X-Forwarded-Proto https;
			proxy_pass http://server8180;
			proxy_redirect http:// https://;
			
			#解决http和https混合请求报错
		    add_header Content-Security-Policy upgrade-insecure-requests;
		}

        
	
        #location / {
        #    root   html;
        #    index  index.html index.htm;
        #}
    }

}

标签:http,log,header,server,nginx,proxy,https
From: https://www.cnblogs.com/tc310/p/17393463.html

相关文章

  • 二级域名做http代理(跳到指定机器的指定端口)
    1添加域名  2添加域名后会自动生成一个加速域名,将这个加速域名做CNAME解析。  3开启自定义端口(首先打开静态回源配置和动态内容回源配置)  6然后配置自定义端口7配置后将以上两项配置的http关掉才能生效(回源配置,动态加速配置) ......
  • HttpClient连接池的连接淘汰策略分析,以及解决HttpNoResponse异常
    本文分析的apacheHttpClient版本为4.5在HttpClient连接池的使用中,发现有三处关于连接释放的时间配置PoolingHttpClientConnectionManager构造函数中的timeToLive,默认是-1publicPoolingHttpClientConnectionManager(finalRegistry<ConnectionSocketFactory>socke......
  • 怎样防止浏览器自动将http网址转变为https
    Chrome浏览器地址栏中输入 chrome://net-internals/#hsts在 Deletedomainsecuritypolicies 中输入项目的域名,并 Delete 删除 可以在 Querydomain 测试是否删除成功 【注】这里如果还是不行,请清除浏览器缓存!......
  • 怎样在局域网中给网站作ssl认证,使其能以https协议访问(转)
    如果要在局域网达到效果需要满足以下几点要求:1. 证书由可信任的CA机构颁发2. 证书在有效期3. 访问地址和证书的认证地址一致说明:1.需要在局域网内构建CA机构2.证书的有效期建议设置长一点,毕竟在内网使用,升级比较麻烦3.生成的自签证书其中包含的域名或ip要和浏览器中......
  • Https通信的SSL证书工作流程原理(转)
    前言浏览器与服务器之间的https加密通信会包括以下一些概念:非对称加密、对称加密、RSA、证书申请、根证书。https证书加密,就是在传输层tcp和应用层http之间加了一层ssl层来对传输内容进行加密。用时序图简单记录一下大致过程吧。证书工作原理服务器向证书机构申请证书的流程如......
  • aiohttp.client_exceptions.ServerDisconnectedError: Server disconnected
    #解决:#1.aiohttp.TCPConnector(limit=0)=>aiohttp.TCPConnector(limit=10)#2.添加timeout=aiohttp.ClientTimeout(total=600)asyncdefget_data_main():#初始化数据库#db_utils.init_db('auto_search_uat.db')set_logging.set_get_data_log(......
  • questions_03 【http://127.0.0.1:8000/%5Emanage/(%3FP1%5Cd+)/dashboard/】项目id参
    【原因背景】当我们在点击进入具体项目的时候,根据我们所写的url,中间应该包含我们的项目id,当不知道什么原因可以进入项目,但是id是乱码的【原因分析】在查看相关资料后发现是我们在写path的时候出现的问题:Django2.2.x之后的版本path:用于普通路径,不需要自己手动添加正则首位......
  • Visual Studio Connected Services 生成http api代码
    生成的代码将和接口对应的参数、返回值一一对应,本文底层使用的工具为NSwag.exe,其他可替代的方案还有AutoSet.exe1.配置连接2.配置生成的代码相关属性3.如果遇到报错考虑将version添加到生成的swagger.json文件中底层实际是调用NSwag.exe生成的代码生成的文件位置此......
  • https://pengchenggang.github.io/vuejsdev-com-github 备份发布
    https://pengchenggang.github.io/vuejsdev-com-github备份发布现在还没有解决的就是开clash,代码提交不上去,只能关了提,但是关了提交,也得赶运气。提交代码体验很差~......
  • Blackbox_exporter的HTTP模块配置Bearer令牌
    如果要监控需要携带token才能访问的接口,您可以使用Blackbox_exporter的HTTP模块配置Bearer令牌。以下是一个示例:安装和配置Blackbox_exporter。创建一个名为auth.yml的配置文件,并将其放置在Blackbox_exporter配置文件夹中。在auth.yml文件中,添加类似以下的配置:modules:  http_2x......