Nginx 自定义添加Response Headers 修改server

之前说过如何隐藏Response Headers中 X-Powered-By

修改php.ini

expose_php = Off
service php-fpm reload

隐藏Nginx版本号 Server

修改nginx.conf, 在http{}添加

server_tokens off;

 

Nginx自定义Response Headers

 

add_header key 'value';
  server {
            listen 80 ;
            server_name ***.com;
            index index.html index.htm index.php;
            root  /home/wwwroot/***/backend/web;     
        autoindex  on;
        include enable-php.conf;  
        add_header aaa 'bbb';
        
        access_log  /home/wwwlogs/access.log;
        if (!-e $request_filename){
                rewrite ^/(.*) /index.php?r=$1 last;
            }
        }

 

nginx -t
service nginx reload

 

 

自定义修改Nginx的server

1: 修改server要在编译Nginx修改/src/core/nginx.h文件

#define NGINX_VERSION    "1.14"  
#define NGINX_VER       "nginx" 

 

 

2: 修改src/http/ngx_http_header_filter_module.c

static char ngx_http_server_string[]="Server: nginx" CRLF;

3: 修改src/http/ngx_http_special_response.c

static u_char ngx_http_error_tail[]="<hr><center>nginx</center>" CRLF

执行下列代码查看nginx的配置

nginx -V

然后configure 加上参数

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module
make && make install

 

 

 

转载于:https://www.cnblogs.com/dcb3688/p/4608065.html

### 如何在HTTP Headers中增加自定义字段 #### 使用Java Spring Boot框架添加自定义响应头 当使用Spring Boot开发Web应用程序时,可以通过`HttpHeaders`类来设置响应头。下面是一个具体的例子,在控制器方法中创建并返回带有自定义头部的`ResponseEntity`对象: ```java import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import javax.servlet.http.HttpServletResponse; public ResponseEntity<String> login(@RequestBody Map<String, String> map, HttpServletResponse response){ HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.add("Custom-Token", "your_custom_token_value"); // 添加自定义header return new ResponseEntity<>("登录成功", httpHeaders, HttpStatus.OK); } ``` 如果上述方式未能生效,则可能是因为浏览器或其他客户端对于某些特定名称的头部进行了过滤[^2]。 #### 利用Nginx服务器配置文件添加全局或针对某路径的自定义Header 除了应用层面上的操作外,还可以借助反向代理服务器如Nginx来进行更灵活地控制。编辑nginx.conf或者对应站点配置文件内的location部分,加入如下指令即可实现在转发请求之前附加额外的信息至下游服务调用者的请求头上;同样适用于修改现有标准头部的内容或将环境变量暴露给上游API作为参数传递的一部分: ```bash server { listen 80; server_name localhost; location /api/ { proxy_pass http://backend_service_address/; add_header X-Custom-Field "custom value"; # 自定义header proxy_set_header Host $host; ... } } ``` 此段落描述了如何利用Nginx配置来自定义HTTP头部信息[^3]。 #### .NET Core Web API项目内实现相同功能的方法 对于基于.NET平台构建的服务端程序而言,可以采用扩展中间件的方式拦截所有的入站流量并对它们施加统一策略——比如往每一个出去的消息里塞进去几个预设好的键值对组合而成的新属性。另外一种更为简便的做法是在Action函数内部直接操作`HttpContext.Response.Headers`集合完成即时性的变更而无需改动太多地方: ```csharp using Microsoft.AspNetCore.Mvc; [ApiController] [Route("[controller]")] public class ExampleController : ControllerBase { [HttpGet(Name = "GetExample")] public IActionResult Get() { Response.Headers.Add("X-Powered-By", ".Net"); return Ok(new { message = "Hello World!" }); } } ``` 这段代码展示了怎样快速地为ASP.NET Core MVC动作的结果集追加上个性化的元数据标签[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值