Forum: NGINX Setting custom response headers

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
ehudros2 (Guest)
on 2009-06-03 13:42
(Received via mailing list)
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 :)

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,2598,2598#msg-2598
Igor S. (Guest)
on 2009-06-03 15:51
(Received via mailing list)
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;
        }
    }
ehudros2 (Guest)
on 2009-06-04 09:22
(Received via mailing list)
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 ;)

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,2598,2605#msg-2605
ehudros2 (Guest)
on 2009-06-04 09:34
(Received via mailing list)
How can I set that header to 30 days from now, as I do with expires 30d?
Thanks a lot! :)

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,2598,2606#msg-2606
Igor S. (Guest)
on 2009-06-04 10:09
(Received via mailing list)
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! :)

No way. "expires" handles parameter specially.
ehudros2 (Guest)
on 2009-06-04 10:39
(Received via mailing list)
So there is no way I can calculate a time string in runtime unless I'm
using expire?

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,2598,2611#msg-2611
Igor S. (Guest)
on 2009-06-04 11:03
(Received via mailing list)
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;
ehudros2 (Guest)
on 2009-06-05 06:18
(Received via mailing list)
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:
http://forum.nginx.org/read.php?2,2598,2624#msg-2624
ehudros2 (Guest)
on 2009-06-08 04:25
(Received via mailing list)
anyone? I'm getting lost with this perl module stuff here ;)
is it already in the nginx apt packed I installed? if it is, why is
perl_var unidentified?

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,2598,2679#msg-2679
Ian H. (Guest)
on 2009-06-08 07:34
(Received via mailing list)
ehudros2 wrote:
> anyone? I'm getting lost with this perl module stuff here ;)
> is it already in the nginx apt packed I installed? if it is, why is perl_var 
unidentified?
>
> Posted at Nginx Forum: http://forum.nginx.org/read.php?2,2598,2679#msg-2679
>
>
>
You need to build your own nginx - see

http://wiki.nginx.org/NginxEmbeddedPerlModule
Michael S. (Guest)
on 2009-06-08 07:36
(Received via mailing list)
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...
Igor S. (Guest)
on 2009-06-08 08:25
(Received via mailing list)
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 :) ), 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.
Michael S. (Guest)
on 2009-06-08 08:38
(Received via mailing list)
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 :)

This way modules can be written and deployed any time.
ehudros2 (Guest)
on 2009-06-08 09:14
(Received via mailing list)
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 ;)

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,2598,2682#msg-2682
Igor S. (Guest)
on 2009-06-08 16:34
(Received via mailing list)
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 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 ;)

What does ./configure show near this line:

checking for perl

and what does

grep lperl objs/Makefile

show ?
ehudros2 (Guest)
on 2009-06-09 04:20
(Received via mailing list)
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 ;)

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,2598,2699#msg-2699
ehudros2 (Guest)
on 2009-06-10 11:51
(Received via mailing list)
ehudros2 (Guest)
on 2009-06-11 13:27
(Received via mailing list)
Guys, I really feel bad about bumping a second time in 2 days, but I'm
being hammered here for not getting this done ;)
Can someone please throw in his thoughts on why I can't get the perl
module installed?

Thanks!

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,2598,2817#msg-2817
Igor S. (Guest)
on 2009-06-11 13:33
(Received via mailing list)
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 ;)

Could you show full configure output, including command line ?
ehudros2 (Guest)
on 2009-06-11 16:35
(Received via mailing list)
Hallelujah! I finally got it to work with you great help.
Thanks :)

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:
http://forum.nginx.org/read.php?2,2598,2829#msg-2829
ehudros2 (Guest)
on 2009-06-12 07:51
(Received via mailing list)
Of course. Here is my configure command:
./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've noticed the "checking for perl" line this time (really don't know
how I've missed it last time), and here's the printout:
checking for perl
 + perl version: v5.8.8 built for i486-linux-gnu-thread-multi
Note (probably harmless): No library found for -lperl
 + perl interpreter multiplicity found

the full printout is here: http://paste2.org/p/260873

Thanks Igor ;)

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,2598,2824#msg-2824
Igor S. (Guest)
on 2009-06-12 08:02
(Received via mailing list)
On Thu, Jun 11, 2009 at 06:40:22AM -0400, ehudros2 wrote:

> Of course. Here is my configure command:
> ./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've noticed the "checking for perl" line this time (really don't know how I've missed 
it last time), and here's the printout:
> checking for perl
>  + perl version: v5.8.8 built for i486-linux-gnu-thread-multi
> Note (probably harmless): No library found for -lperl
>  + perl interpreter multiplicity found
>
> the full printout is here: http://paste2.org/p/260873

Probably, you need to set something like libperl-dev package.
ehudros2 (Guest)
on 2009-06-16 12:25
(Received via mailing list)
Hi every, I'm back :)
It seems I need the header to be set in a different format, and look
like an expires date.
it currently gives:
User-Expires  Mon Apr 12 03:52:54 2010

When the desired outcome should be:
Mon, 12 Apr 2010 03:52:54 GMT

Is there a way I can format the date to the expires date format? I'm
guessing it should be done in the perl function you suggested, but I
have no knowledge in perl and searching for perl date formatting just
flooded me with information I did not know how to use :)

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,2598,2942#msg-2942
ehudros2 (Guest)
on 2009-06-21 13:05
(Received via mailing list)
slight bump ;)
Can a perl monk help here?

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,2598,3208#msg-3208
Igor S. (Guest)
on 2009-06-21 13:57
(Received via mailing list)
On Tue, Jun 16, 2009 at 04:07:18AM -0400, ehudros2 wrote:

> Hi every, I'm back :)
> It seems I need the header to be set in a different format, and look like an expires 
date.
> it currently gives:
> User-Expires  Mon Apr 12 03:52:54 2010
>
> When the desired outcome should be:
> Mon, 12 Apr 2010 03:52:54 GMT
>
> Is there a way I can format the date to the expires date format? I'm guessing it should 
be done in the perl function you suggested, but I have no knowledge in perl and searching 
for perl date formatting just flooded me with information I did not know how to use :)

You need to install Date::Manip module:

use Date::Manip qw(UnixDate);

$date = UnixDate(time(), "%a, %e %b %Y %H:%M:%S %z");
ehudros2 (Guest)
on 2009-06-22 10:05
(Received via mailing list)
Thanks Igor, your help on this issue so far is outstanding. I'll give it
a try and post my results.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,2598,3211#msg-3211
This topic is locked and can not be replied to.