# Nginx proxying to Apache: what about mod\_rewrite?

**URL:** <https://www.ruby-forum.com/t/nginx-proxying-to-apache-what-about-mod-rewrite/176453>\
**Category:** NGINX\
**Created:** [September 30, 2009, 7:12pm UTC](https://www.ruby-forum.com/t/nginx-proxying-to-apache-what-about-mod-rewrite/176453 "2009-09-30T19:12:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![illarra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/aeb1de/32.png) [@illarra](https://www.ruby-forum.com/u/illarra)\
**Post date:** [September 30, 2009, 7:12pm UTC](https://www.ruby-forum.com/t/nginx-proxying-to-apache-what-about-mod-rewrite/176453/1 "2009-09-30T19:12:34Z")

</div>

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](http://domain.com);  
location / {  
proxy\_pass [http://127.0.0.1:8030](http://127.0.0.1:8030);  
}  
}

And the Apache configuration…

ServerName [domain.com](http://domain.com)  
ServerAlias [www.domain.com](http://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:

> **[Nginx proxying to Apache: what about mod\_rewrite?](https://forum.nginx.org/read.php?2,10271,10271#msg-10271)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![illarra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/aeb1de/32.png) [@illarra](https://www.ruby-forum.com/u/illarra)\
**Post date:** [September 30, 2009, 7:31pm UTC](https://www.ruby-forum.com/t/nginx-proxying-to-apache-what-about-mod-rewrite/176453/2 "2009-09-30T19:31:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![illarra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/aeb1de/32.png) [@illarra](https://www.ruby-forum.com/u/illarra)\
**Post date:** [September 30, 2009, 7:34pm UTC](https://www.ruby-forum.com/t/nginx-proxying-to-apache-what-about-mod-rewrite/176453/3 "2009-09-30T19:34:15Z")

</div>

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:

> **[Re: Nginx proxying to Apache: what about mod\_rewrite?](https://forum.nginx.org/read.php?2,10271,10289#msg-10289)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![illarra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/aeb1de/32.png) [@illarra](https://www.ruby-forum.com/u/illarra)\
**Post date:** [September 30, 2009, 7:39pm UTC](https://www.ruby-forum.com/t/nginx-proxying-to-apache-what-about-mod-rewrite/176453/4 "2009-09-30T19:39:31Z")

</div>

> 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. 🙂

- Jeff

---

<div class="post-metadata">

**Author:** ![illarra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/aeb1de/32.png) [@illarra](https://www.ruby-forum.com/u/illarra)\
**Post date:** [September 30, 2009, 8:49pm UTC](https://www.ruby-forum.com/t/nginx-proxying-to-apache-what-about-mod-rewrite/176453/5 "2009-09-30T20:49:13Z")

</div>

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 🙂  
Where do I have to write this code exactly?

Posted at Nginx Forum:

> **[Re: Nginx proxying to Apache: what about mod\_rewrite?](https://forum.nginx.org/read.php?2,10271,10300#msg-10300)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![illarra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/aeb1de/32.png) [@illarra](https://www.ruby-forum.com/u/illarra)\
**Post date:** [September 30, 2009, 9:22pm UTC](https://www.ruby-forum.com/t/nginx-proxying-to-apache-what-about-mod-rewrite/176453/6 "2009-09-30T21:22:35Z")

</div>

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:

> **[Re: Nginx proxying to Apache: what about mod\_rewrite?](https://forum.nginx.org/read.php?2,10271,10315#msg-10315)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![illarra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/aeb1de/32.png) [@illarra](https://www.ruby-forum.com/u/illarra)\
**Post date:** [September 30, 2009, 8:49pm UTC](https://www.ruby-forum.com/t/nginx-proxying-to-apache-what-about-mod-rewrite/176453/7 "2009-09-30T20:49:48Z")

</div>

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](http://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](http://domain.com) [www.domain.com](http://www.domain.com);  
location / {  
#proxy to Apache  
proxy\_pass [http://127.0.0.1:8030](http://127.0.0.1:8030);  
}  
}

Thanks!

Posted at Nginx Forum:

> **[Re: Nginx proxying to Apache: what about mod\_rewrite?](https://forum.nginx.org/read.php?2,10271,10303#msg-10303)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![illarra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/aeb1de/32.png) [@illarra](https://www.ruby-forum.com/u/illarra)\
**Post date:** [September 30, 2009, 10:34pm UTC](https://www.ruby-forum.com/t/nginx-proxying-to-apache-what-about-mod-rewrite/176453/8 "2009-09-30T22:34:36Z")

</div>

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 🙂  
For example, i have

[http://www.domain.com/index.php/blog/id/title.html](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](http://www.domain.com/blog/id/title.html)

Posted at Nginx Forum:

> **[Re: Nginx proxying to Apache: what about mod\_rewrite?](https://forum.nginx.org/read.php?2,10271,10329#msg-10329)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![illarra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/aeb1de/32.png) [@illarra](https://www.ruby-forum.com/u/illarra)\
**Post date:** [October 1, 2009, 10:31am UTC](https://www.ruby-forum.com/t/nginx-proxying-to-apache-what-about-mod-rewrite/176453/9 "2009-10-01T10:31:08Z")

</div>

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](http://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](http://domain.com) [www.domain.com](http://www.domain.com);  
if ( $host != ‘[domain.com](http://domain.com)’ ) {  
rewrite ^/(.\*)$ [http://domain.com/$1](http://domain.com/$1) permanent;  
}  
location / {  
# mod\_rewrite in apache  
proxy\_pass [http://127.0.0.1:8030](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](http://domain.com)  
ServerAlias [www.domain.com](http://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:

> **[Re: Nginx proxying to Apache: what about mod\_rewrite?](https://forum.nginx.org/read.php?2,10271,10433#msg-10433)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![illarra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/aeb1de/32.png) [@illarra](https://www.ruby-forum.com/u/illarra)\
**Post date:** [October 22, 2010, 5:42pm UTC](https://www.ruby-forum.com/t/nginx-proxying-to-apache-what-about-mod-rewrite/176453/10 "2010-10-22T17:42:35Z")

</div>

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:

> **[Re: Nginx proxying to Apache: what about mod\_rewrite?](https://forum.nginx.org/read.php?2,10271,143541#msg-143541)**
>
> 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...
