参考https://blog.csdn.net/loongshawn/article/details/78199977
1.下载相关组件y源代码
cd ~
mkdir nginx
cd nginx
wget https://github.com/nginx/nginx/archive/release-1.19.3.tar.gz
tar -xzvf nginx-1.19.3.tar.gz
wget https://github.com/nginx/njs/archive/master.zip
unzip master.zip
rm master.zip
wget https://github.com/AirisX/nginx_cookie_flag_module/archive/master.zip
unzip master.zip
rm master.zip
wget https://github.com/openresty/headers-more-nginx-module/archive/master.zip
unzip master.zip
rm master.zip
https://github.com/openssl/openssl/archive/OpenSSL_1_1_1g.tar.gz
tar zxvf OpenSSL_1_1_1g.tar.gz
2.编译
./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --with-openssl=/root/nginx/openssl-OpenSSL_1_1_1g --with-stream=dynamic --with-stream_ssl_module --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' --add-dynamic-module=/root/nginx/njs-master/nginx --add-dynamic-module=/root/nginx/nginx_cookie_flag_module-master --add-dynamic-module=/root/nginx/headers-more-nginx-module-master
make
make install
3.测试nginx
nginx -V
4.使用nginx其他相关操作(大部分报错直接百度可查解决方案,就不重复记录了)
4.1 禁用centos防火墙
5.编写测试页面
index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
<script>
function getHeaders(){
var req = new XMLHttpRequest();
req.open('GET', document.location.href, false);
req.send(null);
var headerArr = req.getAllResponseHeaders().split('\n');
var headers = {};
headerArr.forEach(item=>{
if(item!==''){
var index = item.indexOf(':');
var key = item.slice(0,index);
var value = item.slice(index+1).trim();
headers[key] = value
}
})
return headers
}
function obj2string(o){
var r=[];
if(typeof o=="string"){
return "\""+o.replace(/([\'\"\\])/g,"\\$1").replace(/(\n)/g,"\\n").replace(/(\r)/g,"\\r").replace(/(\t)/g,"\\t")+"\"";
}
if(typeof o=="object"){
if(!o.sort){
for(var i in o){
r.push(i+":"+obj2string(o[i]));
}
if(!!document.all&&!/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)){
r.push("toString:"+o.toString.toString());
}
r="{"+r.join()+"}";
}else{
for(var i=0;i<o.length;i++){
r.push(obj2string(o[i]))
}
r="["+r.join()+"]";
}
return r;
}
return o.toString();
}
alert(obj2string(getHeaders()));
</script>
</head>
<body>
<h1>Welcome to nginx! Test header</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
6.修改nginx配置
user root;#修改这行
worker_processes 1;
#追加编译出来的三个模块
load_module /usr/lib64/nginx/modules/ngx_http_js_module.so;
load_module /usr/lib64/nginx/modules/ngx_http_cookie_flag_filter_module.so;
load_module /usr/lib64/nginx/modules/ngx_http_headers_more_filter_module.so;
...
server {
listen 801;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
#修改server信息
more_set_headers 'Server: -';
location / {
root html;
index index.html index.htm;
}
location /test {
add_header Cache-Control 'no-store';
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header Host 'dev.cppcc.com.cn2';
proxy_set_header Access-Control-Allow-Headers '*';
proxy_set_header Access-Control-Expose-Headers 'x_tif_uid,Host';
proxy_set_header X-Nginx-Proxy true;
proxy_set_header X-tif-Uid 'testUid';
proxy_pass http://127.0.0.1:802/;
}
...
# another virtual host using mix of IP-, name-, and port-based configuration
server {
listen 802;
server_name somen802;
root /root/web;
index index.html;
proxy_set_header Access-Control-Allow-Headers '*';
proxy_set_header Access-Control-Expose-Headers 'X-Tif-Uid,Host';
add_header X-Tif-Uid '$http_x_tif_uid';
add_header Host $Host;
}
...
7.测试
http://ip:801/test可以看到具体效果
注意点:
1.自定义的header里面不能用下划线,不然不生效
2.自定义header参数好像都要使用’X-'开头,没细测是否能去掉