NGINX 报错:nginx: [emerg] "proxy_set_header" directive is not allowed here in 解决方法
NGINX 报错:nginx: [emerg] 'proxy_set_header' directive is not allowed here in 解决方法
在使用 NGINX 做反向代理时,可能会遇到以下报错:
nginx: [emerg] "proxy_set_header" directive is not allowed here in /etc/nginx/nginx.conf:14
这个错误提示表明 'proxy_set_header' 指令在当前位置不允许使用。
原因:
'proxy_set_header' 指令需要在 'proxy_pass' 指令之后使用,因为 'proxy_set_header' 指令用于设置发送给后端服务器的请求头信息,而 'proxy_pass' 指令则用于将请求转发到后端服务器。
解决方法:
将 'proxy_set_header' 指令移动到 'proxy_pass' 指令之后即可。
示例代码:
错误代码:
location = / {
if ($cookie_safe != "") {
proxy_pass http://127.0.0.1;
proxy_set_header Host $host;
add_header X-Cache $upstream_cache_status;
#Set Nginx Cache
if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2|ico)$" ) {
expires 365d;
}
proxy_ignore_headers Set-Cookie Cache-Control expires;
proxy_cache my_cache;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 304 301 302 5s;
}
try_files /123.html =200;
}
更改后的代码:
location = / {
if ($cookie_safe != "") {
proxy_pass http://127.0.0.1;
add_header X-Cache $upstream_cache_status;
#Set Nginx Cache
if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2|ico)$" ) {
expires 365d;
}
proxy_ignore_headers Set-Cookie Cache-Control expires;
proxy_cache my_cache;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 304 301 302 5s;
proxy_set_header Host $host;
}
try_files /123.html =200;
}
注意:
- 'proxy_set_header' 指令用于设置发送给后端服务器的请求头信息。
- 'proxy_pass' 指令用于将请求转发到后端服务器。
- 'add_header' 指令用于添加响应头信息。
- 其他指令的含义可以参考 NGINX 官方文档:https://nginx.org/en/docs/
通过以上步骤,就可以解决 NGINX 报错 'proxy_set_header' directive is not allowed here in 的问题。
原文地址: https://www.cveoy.top/t/topic/n4mw 著作权归作者所有。请勿转载和采集!