Need some help with rewrite rule translation

Hello List,

I managed to translate nearly all our old apache rules, but have some
problems with the following ones:

skip existing files

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]

RewriteRule ^favicon.ico$ - [R=404,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+).(flv|gif)$ getFile.php?itemid=$1&type=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/upload/
RewriteRule ^(.*)$ upload/$1 [L]

Thanks for the help
Simon

On Thu, Jun 13, 2013 at 2:44 AM, Simon Hnscheid <
[email protected]> wrote:

skip existing files

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]

if (-e $request_filename){
break;
}

try this at the first of your location/server block

I have an other rule driving me crazy:

Apache:

RewriteRule
^image/resized/(\d+)/([0-9a-fA-F]{6}/|)(.+)(\d+)(|m)x(\d+)(|m)(.+|).(jpg|jpeg|png|gif)
resized/getByDimension.php?domainid=$1&objectid=$3&width=$4&height=$6&format=$9&random=$8&color=$2&maxwidth=$5&maxheight=$7
[L]

nginx:

location / {
rewrite
“^/image/resized/(\d+)/([0-9a-fA-F]{6}/|)(.+)(\d+)(|m)x(\d+)(|m)(.+|).(jpg|jpeg|png|gif)”
/resized/getByDimension.php?domainid=$1&objectid=$3&width=$4&height=$6&format=$9&random=$8&color=$2&maxwidth=$5&maxheight=$7
last;

}
Is there a fault?
Thanks for the Help
Simon

Am 13.06.2013 10:52, schrieb Host DL:

What is the observed behavior?
What do show your logs?

B. R.

That is what I have got:
Am 13.06.2013 05:46, schrieb wishmaster:

skip existing files

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]

I have no solution here

RewriteRule ^favicon.ico$ - [R=404,L]

location /(^favicon)/(.*.(ico)) {
return 404;
}

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+).(flv|gif)$ getFile.php?itemid=$1&type=$2 [L]

location / {
if (!-e $request_filename){
rewrite ^/([0-9]+).(flv|gif)$ /getFile.php?itemid=$1&type=$2 last;
}
}

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/upload/
RewriteRule ^(.*)$ upload/$1 [L]

location ~ /upload/ {
}

location / {
if (!-e $request_filename){
rewrite ^(.*)$ /upload/$1 last;
}
}

On 13 June 2013 09:38, [email protected] wrote:
[snip]

RewriteRule ^favicon.ico$ - [R=404,L]

location /(^favicon)/(.*.(ico)) {
return 404;

Don’t do that. You’re only hurting yourself. Just return an empty gif
(http://wiki.nginx.org/HttpEmptyGifModule) or ico/etc equivalent.

Jonathan

Jonathan M. // Oxford, London, UK
http://www.jpluscplusm.com/contact.html

On Thu, Jun 13, 2013 at 4:51 PM, Jonathan M.
[email protected]wrote:

On 13 June 2013 09:38, [email protected] wrote:
[snip]

RewriteRule ^favicon.ico$ - [R=404,L]

location /(^favicon)/(.*.(ico)) {
return 404;

Don’t do that. You’re only hurting yourself. Just return an empty gif
(http://wiki.nginx.org/HttpEmptyGifModule) or ico/etc equivalent.


You could also do that:
location /favicon.ico {
return 204;
access_log off;
log_not_found off;
}​

Jonathan

Jonathan M. // Oxford, London, UK
http://www.jpluscplusm.com/contact.html


nginx mailing list
[email protected]
nginx Info Page


B. R.

Am 13.06.2013 19:35, schrieb B.R.:

What is the observed behavior?

The parameters are not given corectly to the php script, delivering the
picture.

What do show your logs?

As expected there ist a 404 in the belonging access log. I activated the
rewrite_log but there is no output in the error log.

Simon

Both solutions look interesting, I will have a look on it.
Am 13.06.2013 23:01, schrieb B.R.:

On Fri, 14 Jun 2013 09:58:12 +0200, [email protected]
wrote:

Both solutions look interesting, I will have a look on it.

We use been successfully the “return no content=204” opiton:

location = /favicon.ico { access_log off; log_not_found off; expires
30d; try_files /sites/$server_name/files/favicon.ico $uri =204; }

Meaning that try_files first looks in a directory where we usually keep
favicon.ico and logos (.png, .jpg, etc.), then it tries the user
provied $uri and, if it does not find any then thows out a “no content”
code (204).

There is no need to load one more module (Empty Gif Module) just to do
that.

M.

Thanks for the Detailed Information :slight_smile:

I have still problems with this role:

Client sends the first part of the urls, server rewrites it, and php
delivers the picture.
I tried to escape \ and dots, but this does not help.
Server gives back a 404.

Then I activated the error_log with level notice and added rewrite_log
on, but there is no log output.
Any hints?

Simon

Apache:

RewriteRule
^image/resized/(\d+)/([0-9a-fA-F]{6}/|)(.+)(\d+)(|m)x(\d+)(|m)(.+|).(jpg|jpeg|png|gif)
resized/getByDimension.php?domainid=$1&objectid=$3&width=$4&height=$6&format=$9&random=$8&color=$2&maxwidth=$5&maxheight=$7
[L]

nginx:

location / {
rewrite
“^/image/resized/(\d+)/([0-9a-fA-F]{6}/|)(.+)(\d+)(|m)x(\d+)(|m)(.+|).(jpg|jpeg|png|gif)”
/resized/getByDimension.php?domainid=$1&objectid=$3&width=$4&height=$6&format=$9&random=$8&color=$2&maxwidth=$5&maxheight=$7
last;

}
Is there a fault?

Am 14.06.2013 11:15, schrieb Mark A.:

I have found the point where my rules break,

I’ve had the following location on top, to enable browser caching for
images, for one month:

#images give caching response for 1 month, browser will request after
this period of time again
location ~* .(png|jpg|jpeg|gif)$ {
expires 1m;
log_not_found off;
}
This prevented the Rules to run.
Is there a chance to get this functionality without breaking other
rules?

Simon

Am 14.06.2013 13:40, schrieb B.R.:

Hello,

On Fri, Jun 14, 2013 at 4:35 AM, [email protected]
wrote:

Am 13.06.2013 19:35, schrieb B.R.:

What is the observed behavior?

The parameters are not given corectly to the php script, delivering the
picture.

​What​

​does the script receive ? Details!

What do show your logs?

As expected there ist a 404 in the belonging access log. I activated the
rewrite_log but there is no output in the error log.


Are you sure you log everything correctly?
The error output from rewrite_log is produced at the ‘notice’ level. Is
that error level written to the log file?​
Do you have access_logs telling you which files are being accessed and
what
answers are provided?

_____________**
nginx mailing list
[email protected]

http://mailman.nginx.org/**mailman/listinfo/nginxhttp://mailman.nginx.org/mailman/listinfo/nginx


B. R.

Am 14.06.2013 14:11, schrieb [email protected]:

}
This prevented the Rules to run.
Is there a chance to get this functionality without breaking other
rules?

Simon

This is still driving me mad. We need the expire header for images, but
if we add the location above, our rules arent processed anymore. If we
add the expire-header to the locations with the rewrite rules, the
headers are ignored.
I would realy appreciate some help.

config:

server {
listen 1.2.3.4:80;
index index.php;
server_name static.examplecompany.com
access_log /var/log/nginx/static.examplecompany.com-access.log combined;
error_log /var/log/nginx/static.examplecompany.com-error.log notice;
root /usr/share/examplecompany/static;
#deny access to hidden files
location ~ /. {
deny all;
access_log off;
log_not_found off;
}
#this breaks the rewrite rules, need to fix this soon, due to traffic

location ~* .(png|jpg|jpeg|gif)$ {

expires 30d;

log_not_found off;

}

#images get caching response for 1 month, browser will request after
this period of time again
#return 204 if favicon is requested, there is none, so we turn off
logging too
location = /favicon.ico {
log_not_found off;
access_log off;
return 204;
}

resized trailer images

location /image {
rewrite
“^/image/resized/(\d+)/([0-9a-fA-F]{6}/|)(.+)(\d+)(|m)x(\d+)(|m)(.+|).(jpg|jpeg|png|gif)”
/resized/getBySize.php?domainid=$1&objectid=$3&width=$4&height=$6&format=$9&random=$8&color=$2&maxwidth=$5&maxheight=$7
last;
rewrite
“^/image/logo_examplecompany_(|black_|lightgray_)([0-9a-fA-F]{6})(|alpha)(|big).gif"
/images/pics/logo_examplecompany.php?color=$2&transparency=$3&picversion=$1&size=$4
last;
rewrite "^/image/logo_examplecompany
(small|big)
([0-9a-fA-F]{6}).png”
/images/pics/logo_examplecompany_png.php?color=$2&size=$1 last;
rewrite
“^/image/(trailer|pcd)hints(|2)/([0-9a-fA-F]{6})_([a-z]{2}).(png|gif)”
/images/trailerhints/trailerhint.php?what=$1&suffix=$2&color=$3&lang=$4&format=$5
last;

images for intext border

}
location /images {
rewrite
“^/images/popup/colored/([0-9a-fA-F]{6})/bg_([a-z0-9_]+).(png|gif)”
/images/popup/colored.php?color=$1&image=$2&format=$3 last;
rewrite “^/images/popup/(ppp[0-9]?)_([0-9a-fA-F]{6}).css”
/images/popup/ppp_css.php?file=$1.css&color=$2 last;
}
location /media {
rewrite “^/media/(\d+)/(\d+)/(.+).(.{2,})$”
/media/get.php?domainid=$1&objectid=$3&randomcachekey=$2 last;
}

if file does not exist, assume its in the pattern somewhere

#this is almost ugly, there might be a better solution with try_file
location / {
try_files $uri @missing;
}
location @missing {
rewrite ^/(.).(.)$ /fullexamplecompany.php?file=$1.$2 last;
}

for partners who configure their widgets on their own.

location /retargeting_v1 {
rewrite ^/retargeting_v1.js /jscollections.php?name=retargeting last;
}
location /recommendations_v1 {
rewrite ^/recommendations_v1.js /jscollections.php?name=recommendations
last;
}

to get single javascript modules

location /jsmodule {
rewrite ^/jsmodule/([_0-9A-Za-z.,]+)/?([_A-Za-z0-9[]]+)?$
/jscollections.php?name=module&modules=$1&callback=$2 last;
}

tinyexamplecompany integrations.

publickey and optional file name extension .js

location /tiny {
rewrite ^/tiny/([a-zA-Z0-9]+)(.js)?$
/tinyexamplecompany.php?publickey=$1&widgetname=belowArticle last;
rewrite ^/tiny/([a-zA-Z0-9]+)/([a-z][a-zA-Z_0-9]+).js$
/tinyexamplecompany.php?publickey=$1&widgetname=$2 last;
}
location /picturetrailers {
rewrite ^/picturetrailers/([a-zA-Z0-9]+)(.js)?$
/tinyexamplecompany_picturetrailers.php?publickey=$1 last;
}
location /flytrailer {
rewrite ^/flytrailer/([a-zA-Z0-9]+)(.js)?$
/fullexamplecompany.php?publickey=$1&widgetname=flytrailer&autoinit=1&file=x.js
last;
}
location /fullexamplecompany {
rewrite ^/fullexamplecompany/([a-zA-Z0-9]+).js$
/fullexamplecompany.php?publickey=$1&file=x.js last;
rewrite ^/fullexamplecompany/([a-zA-Z0-9]+)/([a-z][a-zA-Z_0-9]+).js$
/fullexamplecompany.php?publickey=$1&widgetname=$2&file=x.js last;
}
location /async {
rewrite ^/async.js /async.php last;
}
location /demo {
rewrite ^/demo/([a-zA-Z0-9]+).js$ /demo.php?type=$1 last;
rewrite ^/demo/([a-zA-Z0-9]+)/([a-z]+).js$ /demo.php?type=$1&lang=$2
last;
}
location ~* .php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $request_filename;
}
}

Kind Regards
Simon

On Wed, Jun 19, 2013 at 06:44:55PM +0200,
[email protected] wrote:

Hi there,

This is still driving me mad. We need the expire header for images, but
if we add the location above, our rules arent processed anymore. If we
add the expire-header to the locations with the rewrite rules, the
headers are ignored.
I would realy appreciate some help.

This is nginx.

One request is handled in one location.

Look at all of the (top-level) “location” directives. Look at the
request
that you are making. See which one location will handle it. Be aware of
any rewrites or internal redirects that might happen.

In the one location that handles the final request, put all of the
configuration that you want for that request.

http://nginx.org/r/location for details.

So, you have:

location /jsmodule {
location /tiny {
location /picturetrailers {
location /flytrailer {
location /fullexamplecompany {
location /async {
location /demo {
location ~* .php$ {

What one request do you make that does not return what you want? What
does it return instead?

Testing using “curl -i” is probably best, since you will see exactly
what
is returned. Enable the debug log and you might see why that is
returned.

f

Francis D. [email protected]