openresty / headers-more-nginx-module

Set, add, and clear arbitrary output headers in NGINX http servers
1.64k stars 221 forks source link

include -> if -> more_set_headers #98

Open dartyun opened 5 years ago

dartyun commented 5 years ago

I found a problem: if I use the following construction, then the header is missing in the answer:

location {
  include /etc/nginx/default.d/cors.conf;
  ..
}

/etc/nginx/default.d/cors.conf:

more_set_headers 'Access-Control-Allow-Origin: *';
more_set_headers 'Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE';
more_set_headers 'Access-Control-Allow-Headers: DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
more_set_headers 'Access-Control-Expose-Headers: Content-Length,Content-Range';

if ( $request_method = 'OPTIONS' ) {
  more_set_headers 'Access-Control-Max-Age: 1728000';
  more_set_headers 'Content-Type: text/plain; charset=utf-8';
  more_set_headers 'Content-Length: 0';
  return 204;
}

The answer is present only the first part to the block 'if'

When using schemes 'include -> more_set_headers' and 'if -> more_set_headers' all heders will be added correctly.

nginx version 1.17.0 headers-more-nginx-module 0.33

# nginx -V
nginx version: nginx/1.17.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with LibreSSL 2.9.2
TLS SNI support enabled
configure arguments: --prefix=/var/www/html --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules/ --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=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --user=nginx --group=nginx --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --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_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-stream --with-stream_ssl_module --with-pcre --with-openssl=./libressl-2.9.2 --add-module=ngx_devel_kit-0.3.1rc1 --add-module=lua-nginx-module-0.10.15 --add-module=njs-0.3.2/nginx --add-module=nginx-module-vts-0.1.18 --add-module=ngx_brotli --add-module=ngx_cache_purge-2.3 --add-module=ngx_http_geoip2_module-3.2 --add-module=echo-nginx-module-0.61 --add-module=nginx_upstream_check_module --add-module=nginx-auth-ldap-git20170725 --add-module=headers-more-nginx-module-0.33 --add-module=nginx-vod-module-1.24 --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 -DNGX_HAVE_MD5=0 -DNGX_LUA_USE_ASSERT -DNGX_LUA_ABORT_AT_PANIC -I/usr/include/luajit-2.0 -I/usr/include/httpd' --with-ld-opt='-Wl,-E -lrt'
omasseau commented 3 months ago

Got the same problem. Cannot use more_set_headers in a if statement defined in an included conf file. I get this error : nginx: [emerg] "more_set_headers" directive is not allowed here

omasseau commented 3 months ago

Ok actually I found this in the doc :

image