How do you change the server header returned by ng

2019-01-12 15:07发布

There's an option to hide the version so it will display only nginx, but is there a way to hide that too so it will not show anything or change the header?

11条回答
Luminary・发光体
2楼-- · 2019-01-12 15:45

After I read Parthian Shot's answer, I dig into /usr/sbin/nginx binary file. Then I found out that the file contains these three lines.

Server: nginx/1.12.2
Server: nginx/1.12.2
Server: nginx

Basically first two of them are meant for server_tokens on; directive (Server version included). Then I change the search criteria to match those lines within the binary file.

sed -i 's/Server: nginx/Server: thing/' `which nginx`

After I dig farther I found out that the error message produced by nginx is also included in this file.

<hr><center>nginx</center>

There are three of them, one without the version, two of them included the version. So I run the following command to replace nginx string within the error message.

sed -i 's/center>nginx/center>thing/' `which nginx`
查看更多
迷人小祖宗
3楼-- · 2019-01-12 15:47

Like Apache, this is a quick edit to the source and recompile. From Calomel.org:

The Server: string is the header which is sent back to the client to tell them what type of http server you are running and possibly what version. This string is used by places like Alexia and Netcraft to collect statistics about how many and of what type of web server are live on the Internet. To support the author and statistics for Nginx we recommend keeping this string as is. But, for security you may not want people to know what you are running and you can change this in the source code. Edit the source file src/http/ngx_http_header_filter_module.c at look at lines 48 and 49. You can change the String to anything you want.

## vi src/http/ngx_http_header_filter_module.c (lines 48 and 49)
static char ngx_http_server_string[] = "Server: MyDomain.com" CRLF;
static char ngx_http_server_full_string[] = "Server: MyDomain.com" CRLF;

March 2011 edit: Props to Flavius below for pointing out a new option, replacing Nginx's standard HttpHeadersModule with the forked HttpHeadersMoreModule. Recompiling the standard module is still the quick fix, and makes sense if you want to use the standard module and won't be changing the server string often. But if you want more than that, the HttpHeadersMoreModule is a strong project and lets you do all sorts of runtime black magic with your HTTP headers.

查看更多
一纸荒年 Trace。
4楼-- · 2019-01-12 15:48

The last update was a while ago, so here is what worked for me on Ubuntu:

sudo apt-get update
sudo apt-get install nginx-extras

Then add the following two lines to the http section of nginx.conf, which is usually located at /etc/nginx/nginx.conf:

sudo nano /etc/nginx/nginx.conf
server_tokens off; # removed pound sign
more_set_headers 'Server: Eff_You_Script_Kiddies!';

Also, don't forget to restart nginx with sudo service nginx restart.

查看更多
叛逆
5楼-- · 2019-01-12 15:48

It’s very simple: Add these lines to server section:

server_tokens off;
more_set_headers 'Server: My Very Own Server';
查看更多
够拽才男人
6楼-- · 2019-01-12 15:49

The only way is to modify the file src/http/ngx_http_header_filter_module.c . I changed nginx on line 48 to a different string.

What you can do in the nginx config file is to set server_tokens to off. This will prevent nginx from printing the version number.

To check things out, try curl -I http://vurbu.com/ | grep Server

It should return

Server: Hai
查看更多
Bombasti
7楼-- · 2019-01-12 15:53

If you are using nginx to proxy a back-end application and want the back-end to advertise its own Server: header without nginx overwriting it, then you can go inside of your server {…} stanza and set:

proxy_pass_header Server;

That will convince nginx to leave that header alone and not rewrite the value set by the back-end.

查看更多
登录 后发表回答