1,proxy_set_header指令(ngx_http_proxy_module模块
)
1)语法。
Syntax: proxy_set_header field value;
Default:
proxy_set_header Host $proxy_host;
proxy_set_header Connection close;
Context: http, server, location // 可以在http模块,server模块和location模块设置。
2)These directives are inherited from the previous level if and only if there are no proxy_set_header directives defined on the current level.可以从上级level继承,但是如果当前设置了proxy_set_header,上级设置的全局参数失效
3)proxy_set_header Host $host;its value equals the server name in the “Host” request header field or the primary server name if this field is not present:
请求头中的Host字段或者server_name域。
4)eg
http {
proxy_set_header Host $host;
}
server {
listen 80;
server_name www.hzq.com;
access_log off;
# 当设置了proxy_set_header, http中的proxy_set_header全局设置失效。
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
proxy_pass http://127.0.0.1:8080/; #全局未设置,server和localtion未设置,则host取www.hzq.com;全局设置,server中有其他header设置,覆盖了host,host取127.0.0.1:8080(或者backend_server)。
}
}
2,add_header指令。(ngx_http_headers_module模块
)
1)Syntax: add_header name value [always];
Default: —
Context: http, server, location, if in location
2)当响应码为2xx,3xx的时候,才会返回设置的response header,Adds the specified field to a response header with some 2xx/3xx response code。
3)设置了always参数,则不管响应码是什么,都会返回。
4)add_header会覆盖上一级设置;当this level没有设置的时候,会继承previous level的设置。
5)eg: location /slides_pdf {
add_header 'Access-Control-Allow-Origin' '*';
}
3,如果希望允许传递某些响应头给客户端,可以使用 proxy_pass_header Content-Disposition指令。
1)proxy_pass_header Content-Disposition;
语法: proxy_hide_header field;
默认值: —
上下文: http, server, location
2)eg
location ~* /slides_pdf/([^/]*)/([^/]*\.pdf)$ {
set $id $1;
set $filename $2;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Max-Age' 31536000;
add_header 'Content-Transfer-Encoding' 'binary';
proxy_pass_header Content-Disposition;
add_header 'Content-Disposition' 'inline; filename="$filename"';
proxy_connect_timeout 1s;
proxy_next_upstream http_500 http_502 http_504 http_404 http_403 error timeout invalid_header;
proxy_pass http://slides_backend/slides_pdf/$id/$filename
}
4,nginx认证与https配置
1)可以单独给location配置nginx认证
auth_basic "Restricted";
auth_basic_user_file /usr/local/nginx/conf/devpasswd;
2)https配置。
listen 443 ssl http2;
ssl_certificate /usr/local/nginx/ssl/zhanqi.com/ssl_zhanqi.cer;
ssl_certificate_key /usr/local/nginx/ssl/zhanqi.com/ssl_zhanqi.key;