Forum: NGINX rewrite for javascript files.. again, fire on live site

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.
Chris Cortese (Guest)
on 2009-03-10 20:40
(Received via mailing list)
Hello,

Somehow my rewrite for Javascript got broken again.  I've tried every
possible combination of things I can think of.  What I need is:

orig URL:  http://mysite.com/javascript/fileA.js,fileB.js,fileC.js

...becomes...

dest URL:
http://mysite.com/combine.php?type=javascript&file...

So this is the latest variation I've tried, with the new nginx 0.7.40:

 location ^/javascript/(.*)$ {
    fastcgi_pass  127.0.0.1:10005;
    fastcgi_param SCRIPT_FILENAME
/home/fugbert/www/live/escort-space/trunk/html/public/combine.php?type=javascript&files=$1;
    fastcgi_param QUERY_STRING      q=$request_uri;
    include /etc/nginx/fastcgi_params;
  }

** in fastcgi_params, I have the defaults except I commented out
QUERY_STRING line.

The error I'm getting is undefined index "type" which tells me that the
$_GET array is not available to combine.php.

This is hurting a live site.

Thanks in advance for your help,
Chris
Igor S. (Guest)
on 2009-03-10 20:53
(Received via mailing list)
On Tue, Mar 10, 2009 at 11:29:42AM -0700, Chris Cortese wrote:

> http://mysite.com/combine.php?type=javascript&file...
>
> ** in fastcgi_params, I have the defaults except I commented out
> QUERY_STRING line.

It's really strange: in my debug log I see

2009/03/10 21:35:31 [debug] 10047#0: *1 http request line: "GET
/javascript/fileA.js,fileB.js,fileC.js HTTP/1.0"
2009/03/10 21:35:31 [debug] 10047#0: *1 http uri:
"/javascript/fileA.js,fileB.js,fileC.js"

[...]

2009/03/10 21:35:31 [debug] 10047#0: *1 http script copy:
"SCRIPT_FILENAME"
2009/03/10 21:35:31 [debug] 10047#0: *1 http script copy:
"/home/fugbert/www/live/escort-space/trunk/html/public/combine.php?type=javascript&files="
2009/03/10 21:35:31 [debug] 10047#0: *1 http script capture:
"fileA.js,fileB.js,fileC.js"
2009/03/10 21:35:31 [debug] 10047#0: *1 fastcgi param: "SCRIPT_FILENAME:
/home/fugbert/www/live/escort-space/trunk/html/public/combine.php?type=javascript&files=fileA.js,fileB.js,fileC.js"

I.e., SCRIPT_FILENAME is right.
Chris Cortese (Guest)
on 2009-03-10 22:52
(Received via mailing list)
I tried going back to what I had previously posted here that worked, at
least for the location /javascript/ part.  Now it gives me 503 error.

Here's my config:

my_domain.conf:

server {
  listen   80;
  server_name  my_domain.com;

  access_log  /var/log/nginx/my_domain.access.log;
  error_log /var/log/nginx/my_domain.error.log debug;

  root /home/my_linux_user/www/live/my_domain/trunk/html/public;
  index index.php;

  location / {
    fastcgi_pass  127.0.0.1:10005;
    fastcgi_param SCRIPT_FILENAME
/home/my_linux_user/www/live/my_domain/trunk/html/public/index.php;
    fastcgi_param QUERY_STRING      q=$request_uri;
    include /etc/nginx/fastcgi_params;
  }

  location /javascript/ {
#    fastcgi_pass  127.0.0.1:10005;
#    fastcgi_param SCRIPT_FILENAME
/home/my_linux_user/www/live/my_domain/trunk/html/public/combine.php?type=javascript&files=$request_filename;
#   fastcgi_param QUERY_STRING     $query_string;
#    include /etc/nginx/fastcgi_params;
    rewrite ^/javascript/(.*)$ /combine.php?type=javascript&files=$1
last;
  }

  location ^~ /style/ {
    fastcgi_pass  127.0.0.1:10005;
    fastcgi_param SCRIPT_FILENAME
/home/my_linux_user/www/live/my_domain/trunk/html/public/combine.php?type=css&files=$request_filename;
    fastcgi_param QUERY_STRING      q=$request_uri;
    include /etc/nginx/fastcgi_params;
    expires    30d;
    access_log off;
  }

  location /tmp/ {
  }
  location /filestore/ {
  }

  location /images/ {
    expires    30d;
    access_log off;
  }

  location /cgi-bin/ {
    root /home/my_linux_user/www/live/my_domain/trunk;
    fastcgi_pass unix:/tmp/cgi.sock;
    include /etc/nginx/perl_fcgiwrap_params;
  }

  location ~* (jpg|jpeg|gif|png)$ {
      expires    30d;
      access_log off;
  }

  location ~ \.php$ {
    fastcgi_pass  127.0.0.1:10005;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME
/home/my_linux_user/www/live/my_domain/trunk/html/public$fastcgi_script_name;
#   fastcgi_param SCRIPT_FILENAME
/home/my_linux_user/www/live/my_domain/trunk/html/public/index.php;
    fastcgi_param QUERY_STRING      q=$request_uri;
    include /etc/nginx/fastcgi_params;
  }

  location = /style/main_style.php {
    fastcgi_pass  127.0.0.1:10005;
    fastcgi_param SCRIPT_FILENAME
/home/my_linux_user/www/live/my_domain/trunk/html/public/style/main_style.php;
    fastcgi_param QUERY_STRING      q=$request_uri;
    include /etc/nginx/fastcgi_params;
    fastcgi_param QUERY_STRING      q=$request_uri;
    include /etc/nginx/fastcgi_params;
  }
}

nginx.conf:

user              nginx;
worker_processes  2;

#error_log        /var/log/nginx/error.log  notice;
#error_log        /var/log/nginx/error.log  info;

pid               /var/run/nginx.pid;



#----------------------------------------------------------------------
# Events Module
#
#   http://wiki.codemongers.com/NginxEventsModule
#
#----------------------------------------------------------------------

events {
    worker_connections  1024;
}http {
    include       /etc/nginx/mime.types;
    error_log     /var/log/nginx/error.log;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local]
$request '
                      '"$status" $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  2;

    gzip  on;
    gzip_comp_level 6;

    # Load config files from the /etc/nginx/conf.d directory
    include /etc/nginx/conf.d/*.conf;
}

fastcgi_params:
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  PATH_INFO          $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
Chris Cortese (Guest)
on 2009-03-11 03:46
(Received via mailing list)
I finally got this working but I had to go back to a rewrite method.

The relevant part of the config is:

 location ~ /javascript/(.*\.js) {
    rewrite /javascript/(.*\.js) /combine.php?type=javascript&files=$1
last;
}

(and similarly for css)

I'm not sure why I couldn't get the non-rewrite method working.  I am
not really clear on how QUERY_STRING works.  For 99% of the php (the
code igniter part), setting the QUERY_STRING q=$request_uri does it (in
the CodeIgniter config you have to set the protocol to REQUEST_URI).
like so:

  location / {
    fastcgi_pass  127.0.0.1:10005;
    fastcgi_param SCRIPT_FILENAME
/home/fugbert/www/live/escort-space/trunk/html/public/index.php;
    fastcgi_param QUERY_STRING      q=$request_uri;
    include /usr/local/nginx/conf/fastcgi_params;
  }

combine.php doesn't work the way CodeIgniter does.  It's just a PHP
script that expects (for example)
?type=javascript&files=fileA.js,fileB.js,fileC.js


Chris
Anıl Çetin (Guest)
on 2009-03-11 06:18
(Received via mailing list)
Hi All,
I did this for my customers (highly using apache before) to let them
convert htaccess rewrite rules to nginx. Check it from
http://www.anilcetin.com/convert-apache-htaccess-to-nginx/ If you have
any feedbacks please email me (anil?saog.net)
Glen L. (Guest)
on 2009-03-11 07:14
(Received via mailing list)
Thank you so much for the great tools. Hopefully someone will clarify if
it's 100% correct
Max (Guest)
on 2009-03-12 10:00
(Received via mailing list)
Oh that's really helpful, it's a real time saving tool. Thanks.

Max
Chris Z. (Guest)
on 2009-03-12 11:48
(Received via mailing list)
Is the code available to look at and or use? It would be greatly
beneficial
to use on the commandline for me :s
Chris Z. (Guest)
on 2009-03-12 11:53
(Received via mailing list)
Hrm, I need to learn to read first. Sorry hah.
Joe (Guest)
on 2009-03-13 22:42
(Received via mailing list)
Thanks, very helpful.



Regards,
Joe
Anıl Çetin (Guest)
on 2009-03-14 02:12
(Received via mailing list)
No problem, I just updated the code, I think this time it is more close
to be a "working" thing :) If anyone knows a way of getting environment
variables please inform me, I couldnt find if there is. (yes someone
said mod_parsed_vars thing but I am not sure of it, is this patch
applied already to nginx sources? if not so,  the patch is very old to
apply I think)

Joe yazmış:
This topic is locked and can not be replied to.