Welcome! Log In Create A New Profile

Advanced

Nginx proxying Apache: rewrite question

Posted by illarra 
Nginx proxying Apache: rewrite question
May 15, 2009 04:49PM
Hi!
I have made a installation with Nginx as Frontend Server, proxying some dynamic requests to Apache (to serve a PHP app). I have it almost working, but I need some help with the [b]Rewrite module[/b]. The installation has this characteristics:

- Nginx is listening to ports IP:80 and IP:443
- Nginx serves directly calls to static.mydomain.com (all images, js, css, flash... are there)
- Nginx proxyes to Apache to serve www.mydomain.com, as it is dynamic content generated with PHP

I'm doing the proxying to Apache with this code in "nginx.conf" (snippet for mydomain.com):
[code]
...
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

server {
listen IP:80;
server_name mydomain.com alias www.mydomain.com;
location / {
proxy_pass http://127.0.0.1:80;
}
...
[/code]
The only problem I have now is that the PHP app uses Apache mod_rewrite, with this rule:
[code]
RewriteRule ^(.*)$ index.php?r=$1?%{QUERY_STRING} [QSA,L]
[/code]
I thought that proxying to Apache [b]mod_rewrite[/b] will be still working, but it seems that I have to add the rewrite rule directly in the "nginx.conf", somewhere in [i]location /{}[/i].
I have tried this but is not working:
[code]
rewrite ^(.*)$ index.php?r=$1?$query_string last;
[/code]
Any idea? Do I have to put the rewrite rule in Nginx or am I doing something wrong in the proxying to make mod_rewrite not work (it worked when Apache was the main server)?
Thanks!
Re: Nginx proxying Apache: rewrite question
May 16, 2009 01:01AM
Try

[code]
rewrite ^/(.*)$ /index.php?r=$1?$args last;
[/code]

--
Jim Ohlstein
Re: Nginx proxying Apache: rewrite question
May 16, 2009 03:54AM
Thanks Jim, but doesn't solve the problem. This is what I have:
[code]
server {
listen IP:80;
server_name mydomain.com alias www.mydomain.com;
location / {
rewrite ^/(.*)$ /index.php?r=$1?$args last;
proxy_pass http://127.0.0.1:80;
}
}
[/code]
In fact, I get a [b]500 Internal Server Error[/b].
And if I see the nginx log, I can read:
[code]
2009/05/16 07:47:34 [error] 3188#0: *1 rewrite or internal redirection cycle while processing "/index.php", client: *.*.*.*, server: mydomain.com, request: "GET / HTTP/1.1", host: "mydomain.com"
2009/05/16 07:47:37 [error] 3188#0: *2 rewrite or internal redirection cycle while processing "/index.php", client: *.*.*.*, server: mydomain.com, request: "GET / HTTP/1.1", host: "mydomain.com"
[/code]



Edited 2 time(s). Last edit at 05/16/2009 03:54AM by illarra.
Re: Nginx proxying Apache: rewrite question
May 16, 2009 04:54AM
Seems to be solved. For some reason, it works fine with a [b]break[/b]:
[code]
rewrite ^/(.*)$ /index.php?r=$1?$args break;
[/code]
Re: Nginx proxying Apache: rewrite question
May 16, 2009 07:28AM
Hmmm....


I wonder if the rewrite came at the end of the block (or after it entirely) if that would have worked.

No matter as long as your app is functioning as intended.

--
Jim Ohlstein
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 76
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready