njs icon indicating copy to clipboard operation
njs copied to clipboard

How to remove or override value of 'Server' header for all locations.

Open bhavin9695 opened this issue 1 year ago • 9 comments

I am migrating from LUA to NJS. With LUA we were using 'more_set_headers' of 'headers-more-nginx-module' to replace(from default value 'Server : nginx' to 'Server : custom value') the value of the 'Server' header. We were replacing it in http block as we wanted it to make it effective for all the locations.

With NJS, I tried the below approaches but could not replace it.

Approach 1 :

http {
     js_set $un_used_val headerJs.setServerHeader;
}

Approach 2 :

server {
    js_set $un_used_val headerJs.setServerHeader;
}

Approach 3 :

location {
    js_set $un_used_val headerJs.setServerHeader;
}

Approach 4 :

location {
    js_content headerJs.setServerHeader;
}

Approach 5 :

location {
    js_header_filter headerJs.setServerHeader;
}

headerJs.js

function setServerHeader(r){
     r.headersOut['Server'] = 'custom value';
}

If I try to replace it in the location block (Approach : 3,4,5), the browser shows two headers 'Server : nginx' and 'Server : custom value' . image

So, How do I replace the default value 'nginx' of this 'Server' header? I want to override the value for all the locations.

bhavin9695 avatar Feb 29 '24 04:02 bhavin9695

Hi @bhavin9695,

js_set just declares a variable, but the JS function is only called when the variable is referenced. One way to reference a variable early is to use set directive.

Regarding the Server header, the current behaviour is incorrect and will be fixed.

xeioex avatar Feb 29 '24 05:02 xeioex

Thanks, @xeioex for your prompt response. Understood your point on js_set. Forgot to return the dummy value from setServerHeader function.

For the Server header, is there any plan on the timeline to fix this?

bhavin9695 avatar Feb 29 '24 05:02 bhavin9695

Also, How do I set it for all the locations? Do I have to call setServerHeader method from each location block? or is there any other way?

bhavin9695 avatar Feb 29 '24 05:02 bhavin9695

@bhavin9695

For the Server header, is there any plan on the timeline to fix this?

Yes, I plan to fix it this or next week.

Also, How do I set it for all the locations? Do I have to call setServerHeader method from each location block? or is there any other way?

Yes, using NJS, as of now, you have to configure it per location.

With LUA we were using 'more_set_headers' of 'headers-more-nginx-module'

What are the reasons to move away from them? AFAIK, more_set_headers and headers-more-nginx-module can be used as is (without LUA) and they are good at what they do.

xeioex avatar Feb 29 '24 05:02 xeioex

Thanks @xeioex

we are using headers-more-nginx-module for setting one header only and also NJS is Nginx's in-house. So since we are migrating to NJS we choose to use NJS only for setting this one header also.

Once again thanks for the very prompt response ( Your quick response is another reason to use NJS as much as we can :) )

bhavin9695 avatar Feb 29 '24 09:02 bhavin9695

Thanks @xeioex and we have the same issue. By the way, it there any function used for header modification to upstream like proxy_set_header in nginx directive. You know r.headersOut used for response to client. We looking for a easy way for request to upstream.

liaojianxiong avatar Mar 05 '24 01:03 liaojianxiong

@liaojianxiong

By the way, it there any function used for header modification to upstream like proxy_set_header in nginx directive.

No, in nginx the incoming headers are not designed to be modified in place. Instead use proxy_set_header to modify outgoing headers at the moment of request construction.

xeioex avatar Mar 05 '24 20:03 xeioex

@bhavin9695

Server modification is committed.

xeioex avatar Mar 05 '24 20:03 xeioex

Thanks @xeioex for update.

bhavin9695 avatar Mar 05 '24 20:03 bhavin9695

Released in 0.8.4 release.

xeioex avatar Apr 17 '24 01:04 xeioex