Setting custom response headers

Hi all,
I’m trying something that I though should be quite simple but turns out
it isn’t…
I have a rails app and want all static assets with a timestamp query
string to be sent with a custom response header called “User-Expires”,
instead of the regular “expires” directive.
I’m coming from apache, and I’m not sure if proxy_set_header is what I’m
looking for or not.
In any case, I’m using the following code:

if ($request_uri ~* “.(ico|css|js|gif|jpe?g|png|swf)?[0-9]+$”) {
expires 30d;
proxy_set_header User-Expires 300d;
break;
}

And I’m getting the following error:
28514#0: “proxy_set_header” directive is not allowed here

I tried moving it to the top (right under all my other proxy_set_headers
and it didn’t return an error but also did not seem to set the header.

I’m totally confused here - why is it called proxy_set_header when I’m
not even using a proxy - i’m just returning the static asset without
proxying anything to mongrel. Am I totally missing something here?
Thanks :slight_smile:

Posted at Nginx Forum:

On Wed, Jun 03, 2009 at 05:36:50AM -0400, ehudros2 wrote:

                   break;
            }

And I’m getting the following error:
28514#0: “proxy_set_header” directive is not allowed here

I tried moving it to the top (right under all my other proxy_set_headers and it didn’t return an error but also did not seem to set the header.

I’m totally confused here - why is it called proxy_set_header when I’m not even using a proxy - i’m just returning the static asset without proxying anything to mongrel. Am I totally missing something here?

I do not know why do use proxy_set_header if you are not even using a
proxy.

Probably, you need

location ~* \.(ico|css|js|gif|jpe?g|png|swf)$ {
    if ($args ~ ^\d+$) {
        add_header  User-Expires 300d;
    }
}

OMG, add_header…
That’s what’s i’ve been looking for the entire morning. I’ve tried so
many different searches on how to add custom headers - they’ve all lead
me to the proxy_pass_header.

Thanks :wink:

Posted at Nginx Forum:

On Wed, Jun 03, 2009 at 08:24:14AM -0400, ehudros2 wrote:

How can I set that header to 30 days from now, as I do with expires 30d?
Thanks a lot! :slight_smile:

No way. “expires” handles parameter specially.

How can I set that header to 30 days from now, as I do with expires 30d?
Thanks a lot! :slight_smile:

Posted at Nginx Forum:

So there is no way I can calculate a time string in runtime unless I’m
using expire?

Posted at Nginx Forum:

On Wed, Jun 03, 2009 at 09:28:06AM -0400, ehudros2 wrote:

So there is no way I can calculate a time string in runtime unless I’m using expire?

You may try to use builtin perl:

http {
perl_var $user_exp
‘sub { return scalar (localtime(time() + 300 * 86400)) }’;

location …

    add_header  User-Expires  $user_exp;

Looks awesome, but seems like I dont have the perl module installed?
I get a "unknown directive “perl_var” error…
I’ve tried searching for the perl module but it seems like the docs are
in russian. Is there a way I can compile it into ngnix?

Posted at Nginx Forum:

ehudros2 wrote:

anyone? I’m getting lost with this perl module stuff here :wink:
is it already in the nginx apt packed I installed? if it is, why is perl_var unidentified?

Posted at Nginx Forum: Re: Setting custom response headers

You need to build your own nginx - see

http://wiki.nginx.org/NginxEmbeddedPerlModule

I think 0.8.x should introduce pluggable modules.

Then the distros could support a stripped down basic version with
optional modules, and then just have a “module foo;” line or something
in the nginx.conf. I don’t think it would take too much overhead as
it’s only run during startup…

On Sun, Jun 07, 2009 at 03:26:58AM -0700, Michael S. wrote:

I think 0.8.x should introduce pluggable modules.

Then the distros could support a stripped down basic version with
optional modules, and then just have a “module foo;” line or something
in the nginx.conf. I don’t think it would take too much overhead as
it’s only run during startup…

I thought about dynamic loadable modules (and it’s really very
intresting
to me from technical point of view :slight_smile: ), however, if I will ever
implement
this, the loading will be allowed only on start, but not on
reconfiguration,
since unloading with removing dependences is complex thing.
Anyway it’s not 0.8.x thing.

anyone? I’m getting lost with this perl module stuff here :wink:
is it already in the nginx apt packed I installed? if it is, why is
perl_var unidentified?

Posted at Nginx Forum:

Definately. It would be a lot better than having to compile nginx
custom for each module you want. Just a simple .so file or something
that takes effect on a start or restart only still works better.
Somehow doing it via graceful reload would be even better :slight_smile:

This way modules can be written and deployed any time.

On Sun, Jun 07, 2009 at 07:58:49AM -0400, ehudros2 wrote:

Thanks for the help guys…
Here’s my configure line:
./configure --sbin-path=/usr/sbin --with-http_ssl_module --conf-path=/etc/nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_perl_module --prefix=/etc/nginx

I get a ton of “undefined reference” errors when it tries to compile the perl module (like “undefined reference to perl_free' objs/src/http/modules/perl/ngx_http_perl_module.o: In functionngx_http_perl_xs_init’”)
although I do have perl 5.8.8 installed. Do I need perl build headers? How do i install them?
This is a much more difficult task than I originally thought it’d be :wink:

What does ./configure show near this line:

checking for perl

and what does

grep lperl objs/Makefile

show ?

Thanks for the help guys…
Here’s my configure line:
./configure --sbin-path=/usr/sbin --with-http_ssl_module
–conf-path=/etc/nginx --error-log-path=/var/log/nginx/error.log
–http-log-path=/var/log/nginx/access.log --with-http_perl_module
–prefix=/etc/nginx

I get a ton of “undefined reference” errors when it tries to compile the
perl module (like “undefined reference to perl_free' objs/src/http/modules/perl/ngx_http_perl_module.o: In function ngx_http_perl_xs_init’”)
although I do have perl 5.8.8 installed. Do I need perl build headers?
How do i install them?
This is a much more difficult task than I originally thought it’d be :wink:

Posted at Nginx Forum:

Hi guys,
seems like ./configure does not check for perl (I dont see that line and
./configure | grep perl also comes back empty).
So does “grep lperl objs/Makefile”.

Any idea why that is?
I’m really thankful for all your help :wink:

Posted at Nginx Forum:

(bump)

Posted at Nginx Forum:

Guys, I really feel bad about bumping a second time in 2 days, but I’m
being hammered here for not getting this done :wink:
Can someone please throw in his thoughts on why I can’t get the perl
module installed?

Thanks!

Posted at Nginx Forum:

Hallelujah! I finally got it to work with you great help.
Thanks :slight_smile:

BTW - it was not perl_var but perl_set that did the trick, if anyone
needs something like this in the future.

Posted at Nginx Forum:

On Mon, Jun 08, 2009 at 03:04:47AM -0400, ehudros2 wrote:

Hi guys,
seems like ./configure does not check for perl (I dont see that line and ./configure | grep perl also comes back empty).
So does “grep lperl objs/Makefile”.

Any idea why that is?
I’m really thankful for all your help :wink:

Could you show full configure output, including command line ?