Rewrite rules from Apache again

I’m still trying to move everything from apache to nginx.
I’ve successfully translated some rules from the .htaccess:

RewriteRule ^(.)_k(.).htm$ pagina.php?k=$2

works pretty weff if turned into:

rewrite ^/(.)_k(.).htm$ /pagina.php?k=$2 ;

while

RewriteRule ^privacy.php$ pagina.php?k=privacy

does not work if translated as:

rewrite ^/privacy.php$ /pagina.php?k=privacy ;

the request is passed unchanged and I get a 404 error for the
/privacy.php page does’nt exist.

What did I miss?

AdvThanksAnce,

Luciano.

/"\ /Via A. Salaino, 7 - 20144 Milano (Italy)
\ / ASCII RIBBON CAMPAIGN / PHONE : +39 2 485781 FAX: +39 2 48578250
X AGAINST HTML MAIL / E-MAIL: [email protected]
/ \ AND POSTINGS / WWW: http://www.lesassaie.IT/

On Tue, Jun 10, 2014 at 06:00:13PM +0200, Luciano Mannucci wrote:

Hi there,

works pretty weff if turned into:

rewrite ^/(.)_k(.).htm$ /pagina.php?k=$2 ;

does not work if translated as:

rewrite ^/privacy.php$ /pagina.php?k=privacy ;

the request is passed unchanged and I get a 404 error for the
/privacy.php page does’nt exist.

What did I miss?

The rest of the config?

==
server {
rewrite ^/(.)_k(.).htm$ /pagina.php?k=$2 ;
rewrite ^/privacy.php$ /pagina.php?k=privacy ;

location = /pagina.php {
return 200 “I got $uri$is_args$args from $request_uri\n”;
}
}

seems to work as expected for me, for requests like /privacy.php and
/a_k_b.htm?key=value.

What do you have that is different?

f

Francis D. [email protected]

On Tue, 10 Jun 2014 22:43:05 +0100
Francis D. [email protected] wrote:

The rest of the config?
:slight_smile:
Well, I’ve posted it in my previous request for help. Beeing
longish I tried to spare some bandwith… :slight_smile:

==
server {
rewrite ^/(.)_k(.).htm$ /pagina.php?k=$2 ;
rewrite ^/privacy.php$ /pagina.php?k=privacy ;
Wow!
I had it under “location /”
Moving it to “server” level and adding a “break” seems to make it work!

location = /pagina.php {
return 200 “I got $uri$is_args$args from $request_uri\n”;
}
Many thanks for this elegant way of debugging this kind of
configuration problems.

What do you have that is different?
Another problem :frowning:
If I try the same thing in a subdirectory, it doesn’t work.

In the error log I get:
2014/06/11 17:51:46 [error] 602#0: *264 FastCGI sent in stderr: “Primary
script
unknown” while reading response header from upstream, client:
212.121.88.183,
server: new.assirm.it, request: “GET /en/privacy.php HTTP/1.1”,
upstream: “fastcgi://127.0.0.1:9004”, host: “new.assirm.it”, referrer:
http://new.assirm.it/en/history_khistory.htm

It seems that the “location =” that I’ve put to intercept the rewrite
doesn’t match.

My configuration, now looks like this:
server {
listen 212.45.144.216:80 default_server;
server_name new.assirm.it test.assirm.it;
access_log /dati/log/http/assirm/access.log;
error_log /dati/log/http/assirm/error_new.log;

    rewrite_log on;
    rewrite ^/ultime-news.php$ /archivio-news.php?last=1 ;
    rewrite ^/(.*)_news(.*).htm$ /news.php?id=$2 ;
    rewrite ^/(.*)_ev(.*).htm$ /evento.php?id=$2 ;
    rewrite ^/(.*)_att(.*).htm$ /attivita.php?id=$2 ;
    rewrite ^/(.*)_k(.*).htm$ /pagina.php?k=$2 ;
    rewrite ^/(.*)_sk(.*).htm$ /stampa-contenuto.php?k=$2 ;
    rewrite ^/(.*)_sn(.*).htm$ /stampa-news.php?id=$2 ;
    rewrite ^/(.*)_a(.*).htm$ /associato.php?id=$2&$args ;
    rewrite ^/(.*)_p(.*).htm$ /mypost.php?id=$2&$args ;
    rewrite ^/ricerca-(.*).htm$ /risultati.php?s=$1&goo=1 ;
    rewrite ^/privacy.php$ /pagina.php?k=privacy break;

    location ~ \.php$ {
        root /dati/httpd/web_assirm/sito_nginx;
        fastcgi_pass 127.0.0.1:9004;
        fastcgi_index index.php;
        include /etc/nginx/fastcgi.conf;
    }

    location / {
        root   /dati/httpd/web_assirm/sito_nginx;
        index  index.html index.htm index.php home.html 

welcome.html;
}

    location ^~ /en/ {
        root   /dati/httpd/web_assirm/sito_nginx;
        index  index.html index.htm index.php home.html;
        rewrite ^/en/ultime-news.php$ /en/archivio-news.php?last=1 ;
        rewrite ^/en/(.*)_news(.*).htm$ /en/news.php?id=$2 ;
        rewrite ^/en/(.*)_ev(.*).htm$ /en/evento.php?id=$2 ;
        rewrite ^/en/(.*)_att(.*).htm$ /en/attivita.php?id=$2 ;
        rewrite ^/en/(.*)_k(.*).htm$ /en/pagina.php?k=$2 ;
        rewrite ^/en/(.*)_sk(.*).htm$ /en/stampa-contenuto.php?k=$2 

;
rewrite ^/en/(.)_sn(.).htm$ /en/stampa-news.php?id=$2 ;
rewrite ^/en/(.)_a(.).htm$ /en/associato.php?id=$2&$args ;
rewrite ^/en/(.)_p(.).htm$ /en/mypost.php?id=$2&$args ;
rewrite ^/en/(.)_wit(.).htm$ /en/wit.php?c=$2&$args ;
rewrite ^/en/ricerca-(.*).htm$ /en/risultati.php?s=$1&goo=1
;
rewrite ^/en/privacy.php /en/pagina.php?k=privacy ;
location = /en/pagina.php {
return 200 “I got $uri$is_args$args from
$request_uri\n”;
}
location ~ .php$ {
index index.html index.htm index.php home.html;
fastcgi_pass 127.0.0.1:9004;
fastcgi_index index.php;
include /etc/nginx/fastcgi.conf;
}

    }

}


/"\ /Via A. Salaino, 7 - 20144 Milano (Italy)
\ / ASCII RIBBON CAMPAIGN / PHONE : +39 2 485781 FAX: +39 2 48578250
X AGAINST HTML MAIL / E-MAIL: [email protected]
/ \ AND POSTINGS / WWW: http://www.lesassaie.IT/

On Wed, 11 Jun 2014 18:05:50 +0100
Francis D. [email protected] wrote:

On Wed, 11 Jun 2014 18:05:50 +0100
Francis D. [email protected] wrote:

Good luck with it,

Solved!

Moving all the rewrite rules outside the “location” to the “server”
section makes them work as expected. The regexp takes care of the
different rewritings to be done.

Maaany thanks again,

Luciano.

/"\ /Via A. Salaino, 7 - 20144 Milano (Italy)
\ / ASCII RIBBON CAMPAIGN / PHONE : +39 2 485781 FAX: +39 2 48578250
X AGAINST HTML MAIL / E-MAIL: [email protected]
/ \ AND POSTINGS / WWW: http://www.lesassaie.IT/

On Wed, Jun 11, 2014 at 06:20:50PM +0200, Luciano Mannucci wrote:

On Tue, 10 Jun 2014 22:43:05 +0100
Francis D. [email protected] wrote:

Hi there,

The rest of the config?
:slight_smile:
Well, I’ve posted it in my previous request for help. Beeing
longish I tried to spare some bandwith… :slight_smile:

No worries.

It can be useful to have a minimal test case that shows the problem.

I had it under “location /”
Moving it to “server” level and adding a “break” seems to make it work!

Very approximately, the order is:

choose the server{}
run the rewrite directives
choose the location{}
run the rewrite directives, looping back as necessary
handle the request

So your non-server-level rewrites will only apply if they are in the
location{} that is chosen.

location = /pagina.php {
return 200 “I got $uri$is_args$args from $request_uri\n”;
}
Many thanks for this elegant way of debugging this kind of
configuration problems.

You’re welcome.

You may also find it useful to enable debug logging for your test
client,
such as by putting something like

debug_connection 127.0.0.10;

within the events{} block, and then looking in error_log.

If I try the same thing in a subdirectory, it doesn’t work.

Put the rewrites at server{} level, or in the location{} that is chosen.

It seems that the “location =” that I’ve put to intercept the rewrite
doesn’t match.

No.

The rewrite that you want doesn’t happen, because the request
/en/privacy.php is handled in:

    location ~ \.php$ {
    location / {
    location ^~ /en/ {
        location = /en/pagina.php {
        location ~ \.php$ {

…that location, and not in the one two above it.

Good luck with it,

f

Francis D. [email protected]