5

the following is our current setup on the apps

but we would like to add nginx as a reverse proxy cache much like squid or varnish is it possible to edit this config to enable that caching behavior or do i need to add another nginx in front of this set up like i would do for squid or varnish

if this can be done without resorting to usage of squid and varnish it would be nice to have the complete setup in nginx

thanks a lot

upstream backend_appname{
     #start1.someserver.com
     server start1.someserver.com:7810 fail_timeout=3s;
     server start1.someserver.com:7811 fail_timeout=3s;
     server start1.someserver.com:7812 fail_timeout=3s;
     server start1.someserver.com:7813 fail_timeout=3s;


}
server {
    server_name  appname.someserver.com;
    listen 80;
    access_log   logs/access_appname.log;
    #error_log   logs/error_appname.log;

    location /nginx_status {
        stub_status on;
        access_log   off;
    }

    location /static {
        root   /home/someuser/work/appname;
        expires max;
        add_header Cache-Control public,max-age=604800,post-check=604800,pre-check=1209600;
    }
    location / {
        root   /home/someuser/work/appname;
        fastcgi_pass backend_appname;
      set  $addr  $remote_addr;

     if ($http_x_forwarded_for ~ "(?:^|,)\s*(\d+\.\d+\.\d+\.\d+)\s*$") {
           set  $addr  $1;
     }

        fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
        fastcgi_param PATH_INFO       $fastcgi_script_name;
        fastcgi_param QUERY_STRING    $query_string;
        fastcgi_param CONTENT_TYPE    $content_type;
        fastcgi_param CONTENT_LENGTH  $content_length;
        fastcgi_param REQUEST_METHOD  $request_method;
        fastcgi_param REMOTE_ADDR     $addr;
        fastcgi_param REMOTE_PORT     $remote_port;
        fastcgi_param SERVER_PROTOCOL $server_protocol;
        fastcgi_param SERVER_ADDR     $server_addr;
        fastcgi_param SERVER_PORT     $server_port;
        fastcgi_param SERVER_NAME     $server_name;

    }

}

2 Answers 2

2

You can use nginx as a caching reverse proxy. The following links will be helpful.

http://staff.adams.edu/~cdmiller/posts/nginx-reverse-proxy-cache/

http://staff.adams.edu/~cdmiller/posts/nginx-reverse-proxy-cache/

http://forum.nginx.org/read.php?2,4979,46962

0

I would suggest using Varnish instead as it is made to be a reverse proxy and can perform much faster than nginx in that capacity.

Varnish is tuned for performance. It does crazy stuff like compile its config files into C objects, which are then loaded into its memory. This might not seem like much, but it really makes a difference when you have some logic defined in the configs (via Varnish's own VCL language, which is cool in itself).

I see what you're getting at, bu nginx isn't meant to be a monolithic program that does everything (unlike apache). It's meant to do a few things, and do them well... if there's a better reverse proxy (Varnish), use that instead!

Just my thoughts :)

4
  • Any (publicly documented) test available to back your claim that Varnish is faster than Nginx? I am asking because I have never seen that happening...
    – Gil
    Apr 10, 2012 at 10:46
  • I think it was accurate back in Dec 2010, but may not be accurate any more. No time to research this now. Jul 17, 2012 at 22:20
  • I have done some basic tests by putting nginx and varnish in front of my web servers. I always found that nginx had bit higher performance. Not a lot but enough to select nginx for few projects.
    – harry
    Jan 13, 2015 at 14:07
  • Isn't the core difference nowadays, that nginx can terminate SSL while varnish can't. I read a lot about both these past days and come to the conclusion that nginx is a bit trickier to set up correctly for complex sites and is deemed a tiny tiny bit slower but on the other hand has a much lighter hardware resource footprint. So, as always, caching performance depends very much on the usecase and the implementation....
    – sebisnow
    Nov 19, 2021 at 9:19

You must log in to answer this question.