Welcome! Log In Create A New Profile

Advanced

Proxy_pass to Apache and serving static contents directly when they exist

Posted by compiler2k 
Proxy_pass to Apache and serving static contents directly when they exist
March 29, 2010 01:20PM
Hi.

I'm trying to serve EXISTING static files with nginx and proxy_pass-ing all the other file extensions (including .php) of EZPUBLISH to Apache on port 81. I wrote the following config:


[code]
# If file exists in /var/www and is static, serve it directly:
location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|doc|xls|pdf|ppt|txt|tar||js|htm|html)$ {
if (-f $request_filename) {
expires 1d;
root /var/www;
break;
}

# We couldn't find the file, proxy-pass it so that is served by image-cluster.php in EZ Publish.
proxy_pass http://127.0.0.1:81;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
include /etc/nginx/proxy.conf;

}

# PHP scripts and other files not static
location / {
proxy_pass http://127.0.0.1:81;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
include /etc/nginx/proxy.conf;
}
[/code]



The idea is:

[list]
[*] If file requested is jpg|gif|zip|... :
[list]
[*] If file exists in /var/www: serve it directly with nginx
[*] If file does not exists in /var/www/: proxy pass it to apache on port 81
[/list]
[*] If file requested is not jpg|gif|zip ... : Proxy_pass to apache on port 81.
[/list]



The proxy_pass inside the first location is needed because there are images that cannot be found physically in disk, and those images are served by apache+ez publish with this rule in port 81:

(Apache)
[code]
Rewriterule ^/var/([^/]+/)?storage/images-versioned/.* /index_cluster.php [L]
Rewriterule ^/var/([^/]+/)?storage/images/.* /index_cluster.php [L]
RewriteRule content/treemenu/?$ /index_treemenu.php [L]
[/code]



The above configuration works, but I found something strange: when I ask NGINX for a file existing in /var/www/, I find that the file appears both in NGINX logs and APACHE logs, while I think it should only appear in NGINX logs, because it's not beeing (or shouldn't be) proxy-passed.

Maybe it's a matter of priority in the "location" statements...

Any idea of what I'm doing wrong?

Thanks.
Re: Proxy_pass to Apache and serving static contents directly when they exist
March 29, 2010 02:59PM
You could try something like:

[code]
location / {
root /var/www;
try_files request_uri request_uri/ @backend;
...
}

location @backend {
proxy_pass http://127.0.0.1:81;
include /etc/nginx/proxy.conf;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
...
}
[/code]


The try_files directive is more efficient than an "if" directive, which can have unexpected results. See http://wiki.nginx.org/IfIsEvil. In this case try_files will simply look in the web root as you've defined it, serve the file if it's there, optionally serve a directory index if a directory is requested (that can be omitted if not necessary) and then pass everything else to your back end Apache listening on 127.0.0.1:81. The location "@backend" represents an internal redirect. It *must* exist or you will get an error. The try_files directive is available in nginx 0.8.x and 0.7.27+. See http://wiki.nginx.org/NginxHttpCoreModule#try_files.

--
Jim Ohlstein
Re: Proxy_pass to Apache and serving static contents directly when they exist
March 29, 2010 05:37PM
Thanks a lot for the answer, I'll check it tomorrow, but it seems that is exactly what I was looking for.

Just an extra question: Is it possible that nginx logs all the requests served? I want to use awstats with the log files and with proxypass I have the hits in 2 separate log files (apache's log and nginx's log). I need all the access to be registered in a single log file so that I can pass it to awstats..

Thanks.



Edited 1 time(s). Last edit at 03/29/2010 05:42PM by compiler2k.
Re: Proxy_pass to Apache and serving static contents directly when they exist
March 30, 2010 04:21AM
Hi. I tested your suggestions with the following code:

[code]
location ~* ^.+.(jpg|jpeg|gif|png|ico|(and so on)|htm|html)$ {
root /var/www;
try_files $request_uri $request_uri/ @backend;
}

location @backend {
proxy_pass http://127.0.0.1:81;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
}

# Scripts en PHP:
location / {
proxy_pass http://127.0.0.1:81;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
}
[/code]


The doubts:


1.- Do you think the above Location / can give problems with the @fallback ?

2.- Why apache does not show the real IP of the request, instead of nginx's 127.0.0.1 origin IP?

Thanks a lot.
Re: Proxy_pass to Apache and serving static contents directly when they exist
March 30, 2010 12:43PM
compiler2k Wrote:
-------------------------------------------------------
> Hi. I tested your suggestions with the following
> code:
>
>
> location ~* ^.+.(jpg|jpeg|gif|png|ico|(and so
> on)|htm|html)$ {
> root /var/www;
> try_files $request_uri $request_uri/
> @backend;
> }
>
> location @backend {
> proxy_pass http://127.0.0.1:81;
> proxy_redirect off;
> proxy_set_header X-Real-IP
> $remote_addr;
> }
>
> # Scripts en PHP:
> location / {
> proxy_pass http://127.0.0.1:81;
> proxy_redirect off;
> proxy_set_header X-Real-IP
> $remote_addr;
> }
>
>
>
> The doubts:
>
>
> 1.- Do you think the above Location / can give
> problems with the @fallback ?

I don't see why it would.

>
> 2.- Why apache does not show the real IP of the
> request, instead of nginx's 127.0.0.1 origin IP?

Probably you are not running Apache with mod_rpaf - see http://stderr.net/apache/rpaf/. If you google nginx + mod_rpaf you will probably find several tutorials with the details.

>
> Thanks a lot.

As for logging, all requests should be logged by nginx.

--
Jim Ohlstein
Re: Proxy_pass to Apache and serving static contents directly when they exist
March 30, 2010 12:48PM
[code]
> > 2.- Why apache does not show the real IP of the
> > request, instead of nginx's 127.0.0.1 origin IP?

> Probably you are not running Apache with mod_rpaf - see http://stderr.net/apache/rpaf/.
> If you google nginx + mod_rpaf you will probably find several tutorials with the details.
[/code]

No, I'm not using mod_rpaf. But I managed to log the real IP address just with "standard" (basic) apache modules, just by checking for the existence of the X-Forwarded-For header:

[code]
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" proxy
SetEnvIf X-Forwarded-For "^.*\..*\..*\..*" proxied

CustomLog /var/log/apache2/www.domain.com-access.log combined env=!proxied
CustomLog /var/log/apache2/www.domain.com-access.log proxy env=proxied
ErrorLog /var/log/apache2/www.domain.com-error.log
[/code]

[code]
> As for logging, all requests should be logged by nginx.
[/code]

Perfect, this allows me to extract the stats directly from nginx files :-)

Thanks a lot!
Re: Proxy_pass to Apache and serving static contents directly when they exist
April 09, 2010 03:06AM
if you are not mod_rpaf then how could you do all this with apache modules

Since it is in .[url=http://www.pass4sure.com/1Y0-A23.html]1Y0-A23[/url] an unused area of the file system and
mdadm can't [url=http://www.pass4sure.com/1Y0-456.html]1Y0-456[/url] tell when the aborted [url=http://www.pass4sure.com/HP0-D08.html]HP0-D08[/url] write happened it is just left
alone[url=http://www.pass4sure.com/642-583.html]642-583[/url]
-------------------------------
Re: Proxy_pass to Apache and serving static contents directly when they exist
April 09, 2010 04:22AM
Just by checking for the existence of the X-Forwarded-For header:

[code]
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" proxy
SetEnvIf X-Forwarded-For "^.*\..*\..*\..*" proxied

CustomLog /var/log/apache2/www.domain.com-access.log combined env=!proxied
CustomLog /var/log/apache2/www.domain.com-access.log proxy env=proxied
ErrorLog /var/log/apache2/www.domain.com-error.log
[/code]
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 77
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