Nginx proxying to Apache: what about mod_rewrite?

Hi!

I have a website running on a Nginx + Apache configuration.
Nginx serves the static content (in a different domain) and proxies
directly to Apache, where all the PHP is executed.

The problem is that my app uses mod_rewrite, and I can’t find where I
have to put the rules.

This is a snippet of nginx.conf…

server {
listen 0.0.0.0:80;
server_name domain.com;
location / {
proxy_pass http://127.0.0.1:8030;
}
}

And the Apache configuration…

ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /home/www

Options -Indexes IncludesNOEXEC FollowSymLinks -MultiViews
AllowOverride All
Order allow,deny
Allow from all

The rewrite rule in Apache was this one, and it worked before I created
the Nginx + Apache configuration.

RewriteRule ^(.*)$ index.php?r=$1?%{QUERY_STRING}

The question is, where should I put the rewrite condition? in Nginx
(adapted, of course) or Apache?

Thanks for having a look!

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,10271,10271#msg-10271

I would think it would work from either. In apache, it would be in an
.htaccess file in the directory that you want the rewrite rule to
apply to.

Hi,

It’s ok, since you’re using proxy_pass in ngnix, rewrite rules in Apache
must work. I have a similar configuration and it works fine.
What happens when you try to access the domain? 404? index.php?

Regards,

Héctor Paz

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,10271,10289#msg-10289

RewriteRule ^(.*)$ index.php?r=$1?%{QUERY_STRING}

The question is, where should I put the rewrite condition? in Nginx
(adapted, of course) or Apache?

You could do either, but you may as well do it in nginx and save Apache
the
effort. As a bonus, you can get nginx to serve static files before it
passes
the rewritten request to Apache:

try_files $uri $uri/ /index.php?r=$request_uri;

Two birds. One stone. :slight_smile:

  • Jeff

To Gabriel:
Actually, I get an Apache 404 Error:
“The requested URL /es was not found on this server.”

To Héctor:
Have tried also your suggestion, but then I can’t get the ngix.conf file
to work :slight_smile:
Where do I have to write this code exactly?

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,10271,10300#msg-10300

First check if nginx proxy to apache is working properly. Try with a
simple html file with no rewrite/redirections. Does it work?
It seems that your configuration is ok, also check the apache logs to
see is something wrong.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,10271,10315#msg-10315

By the way, this is a bigger snippet of my nginx.conf file, showing how
I’m dealing with statics.
I just need to connect properly nginx and Apache, as the statics are
server via another domain…

server {
listen 0.0.0.0:80;
server_name static.domain.com;
location / {
root /home/web/static/;
expires 40d;
add_header Cache-Control public;
}
}
server {
listen 0.0.0.0:80;
server_name domain.com www.domain.com;
location / {
#proxy to Apache
proxy_pass http://127.0.0.1:8030;
}
}

Thanks!

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,10271,10303#msg-10303

Hi Héctor,

The app works ok, so Apache and Nginx are running.
It is just that I need to keep the index.php file to make it work :slight_smile:
For example, i have

http://www.domain.com/index.php/blog/id/title.html

But it should work without index.php

http://www.domain.com/blog/id/title.html

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,10271,10329#msg-10329

This is how I solved it finally.
I use nginx for the domain redirect:

server {
listen 0.0.0.0:80;
server_name static.domain.com;
location / {
root /home/web/static/;
expires 40d;
add_header Cache-Control public;
}
}
server {
listen 0.0.0.0:80;
server_name domain.com www.domain.com;
if ( $host != ‘domain.com’ ) {
rewrite ^/(.*)$ http://domain.com/$1 permanent;
}
location / {
# mod_rewrite in apache
proxy_pass http://127.0.0.1:8030;
}
}

And the real rewrite is in Apache.
As this domain only deals with dynamic content, I can do it this way:

ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /home/www

Options -Indexes IncludesNOEXEC FollowSymLinks -MultiViews
AllowOverride All
Order allow,deny
Allow from all

RewriteEngine On
RewriteRule ^(.*)$ /index.php$1?%{QUERY_STRING}

Thanks for your help!

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,10271,10433#msg-10433

Hi All.

I have the same problem. If there are any way to solve the problem with
mod rewrite working with nginx like as reverse proxy?

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,10271,143541#msg-143541

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs