Welcome! Log In Create A New Profile

Advanced

nginx as a cache proxy for apache on the same server

Posted by thorwald 
nginx as a cache proxy for apache on the same server
December 20, 2012 05:23AM
Hi,

I have got a problem with nginx configuration to work in such way:
I want to put apache to run on 127.0.0.1 port 80 and nginx to run on public IP on port 80 as well which will be basically a caching proxy to the apache so if someone will ask for the domain which apache is serving nginx will take it over and either read it from its cache or ask for it.

Everything works like a charm when apache is running on public IP on port 80 and nginx on the same public IP on another port (8081). Lets say I have got 2 domains - domain1.com and domain2.com. In the first configuration I have found a way to run nginx as catch-all so if I put domain1.com:8081 I can see the website for that domain fine. I have checked headers to avoid any confusion and it was saying the web server was nginx so it's what I really wanted. When I put domain2.com I could see the website for domain2.com. So ... in theory if I move apache to 127.0.0.1:80 and nginx to PUBLIC_IP:80 everything should work the same way but... it doesn't.

Let me paste a config which is working for me:
nginx.conf:
user apache;
worker_processes 8;
worker_rlimit_nofile 8192;

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
use epoll;
accept_mutex off;
}

http {
server_names_hash_bucket_size 64;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;

proxy_cache_path /var/www/cache levels=1:2 keys_zone=my-cache:10m max_size=1000m inactive=600m;
proxy_temp_path /var/www/cache/tmp;

gzip on;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_min_length 0;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types text/plain text/css text/xml text/javascript application/xml application/xml+rss application/javascript application/json;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;

server {
listen 8081;
server_name _;
index index.php;

location / {
resolver 127.0.0.1;
proxy_buffering on;
proxy_pass http://$host$uri;
proxy_cache my-cache;
proxy_cache_valid 200 302 60m;
proxy_cache_valid 404 1m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}


}
}

So the config above works fine as long as apache runs on PUBLIC_IP:80 and nginx on PUBLIC_IP:8081.

Now when I change apache to 127.0.0.1:80 and nginx to PUBLIC_IP:80 I'm getting "not found". I thoguht OK ... maybe I need to create vhosts first so I have used the same configuration cutting only a proxy part to another file "proxy.conf" to include that into the vhosts config part so I have 2 vhosts:

server {
listen PUBLIC_IP:80;
server_name domain1.com www.domain1.com;

location / {
proxy_pass http://127.0.0.1;
include /etc/nginx/proxy.conf;
}
}


server {
listen PUBLIC_IP:80;
server_name domain2.com www.domain2.com;

location / {
proxy_pass http://127.0.0.1;
include /etc/nginx/proxy.conf;
}
}

so the proxy.conf file is:
resolver 127.0.0.1;
proxy_buffering on;
proxy_cache my-cache;
proxy_cache_valid 200 302 60m;
proxy_cache_valid 404 1m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

and that part was also removed from nginx.conf to avoid duplicates.

So now in this configuration if I go to domain1.com I can see the website for that domain but when I go to domain2.com I can see exactly the same website. The one change I had to make is in proxy_pass
from
proxy_pass http://$host$uri;
to
proxy_pass http://127.0.0.1;

The problem is the URL or the header somewhere is not being passed to apache hence it returns always the same website.

What I would realy like to have is to keep the catch-all facility on nginx and don't configure any vhosts, let apache decide what's hosted there what's not but I don't seem to find any way to configure nginx to send all request to the apache on the background, cache it and serve the proper page - if that is possible at all.

So what I have done wrong ? What I don't understand ? What I have missed ?

Thank you for your help in advance.

Regards.
Re: nginx as a cache proxy for apache on the same server
December 20, 2012 06:57AM
Try this one in your proxy settings:

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;

And remove the line:

proxy_set_header X-Real-IP $remote_addr;
Re: nginx as a cache proxy for apache on the same server
December 20, 2012 08:05AM
Shall I keep the vhost configs too or are we talking about the catch-all configuration now ?

Thank you for your help.

Regards.
Re: nginx as a cache proxy for apache on the same server
December 20, 2012 08:28AM
Hi tested in both "catch-all" and with vhosts and it didn't work :(. Still the same website appears for all vhosts :(.

Regards.
Re: nginx as a cache proxy for apache on the same server
December 20, 2012 09:03AM
Ok, now know what your problem is. :D

This is my catch-all vhost:

server {

listen 80 default_server;
server_name _;
error_log /var/log/nginx/default-error.log;
proxy_set_header Host $host;
return 301 $scheme://www.template-domain.local$request_uri;

}

Forget about the "return 301", but try to insert the "proxy_set_header" here as well. And not only for the location.
Re: nginx as a cache proxy for apache on the same server
December 20, 2012 09:36AM
Hi,

I have found an issue also. Don't know where does it come from but it is related with my "caching" feature.

I run the server on another box with different IP and forwarded all to the "apache". When cache is enabled and I go to domain2.com I see that website for domain2.com. When I go to domain1.com I see website for domain2.com. When I clear the cache and go to domain1.com now I see domain1.com website but when I go to domain2.com it shows domain2.com website so the issue is somewhere in the cache. Don't know where but it looks like nginx thinks all requests are in cache so displays what it does have.

When I used yours I have got: Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects..

So now.. how to deal with the cache correctly ?

My "working" but no caching nginx.conf looks like that:
user nginx;
worker_processes 8;
worker_rlimit_nofile 8192;

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
use epoll;
accept_mutex off;
}

http {
server_names_hash_bucket_size 64;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;


proxy_cache_path /var/www/cache levels=1:2 keys_zone=my-cache:10m max_size=1000m inactive=600m;
proxy_temp_path /var/www/cache/tmp;

gzip on;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_min_length 0;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types text/plain text/css text/xml text/javascript application/xml application/xml+rss application/javascript application/json;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;


server {
listen PUBLIC_IP:80;
server_name _;
index index.php;

location / {
include /etc/nginx/proxy.conf;
}


}
}

and proxy.conf is only:
resolver 127.0.0.1;
proxy_pass http://OTHER_SERVER_PUBLIC_IP;
proxy_set_header Host $http_host;

Regards.
Re: nginx as a cache proxy for apache on the same server
December 20, 2012 09:47AM
I'm not sure but I think this is the key to the final answer:
proxy_cache_key "$scheme://$host$request_uri";

with this setting everything seems to be working as I expected.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 185
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready