views:

631

answers:

5

I am planning to move all my static content to a CDN so on my server I only have dynamic content left. I now have Nginx set up as reverse proxy to Apache. The static request that came in where directly delivered by Nginx without having to go to Apache.

In this case Nginx handled a large portion of the request and I can clearly see the necessity of Nginx.

Now that I moved all the static content to another domain, is there still a need to have nginx in front of Apache. Because now all the request are by default dynamic requests and all go to Apache.

Are there any other benefits of having Nginx and Apache running for only dynamic content.

My dynamic content is PHP/MySQL

Edit:

To be clear: I now have Nginx as a reverse proxy. It delivers static and dynamic content. But I am moving my static files to a CDN. Do I then still need Nginx on my domain.

A: 
oraz
Can you give a little detail on what you mean. Because I only deliver dynamic content trough Apache. And I use Apache 2
Saif Bechan
A: 

What I've done for one website is :

  • set up nginx as a reverse proxy in front of Apache
  • configure it so :
    • Requests to PHP pages (i.e. dynamic content) are sent to Apache
    • Requests to static files (CSS, JS, ...) are directly served by nginx.

This without having to set up two domains : all is on the same domain.


Basically, what I've done is :

  • serve images from nginx, without gzip compression, with caching
  • serve js/css (i.e. text files) from nginx, with gzip compression, with caching
  • serve some other extensions (pdf, exeutables, ...) form nginx, without compression, without caching
  • pass the other requests to Apache


Here's how my nginx's configuration file looks like :

server {
    listen   80;
    server_name  MY_DOMAIN_NAME;

    access_log  /var/log/nginx/MY_DOMAIN_NAME.access.log;

    gzip on;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_types text/plain text/html text/css text/xml application/xml application/xml+rss application/xml+atom text/javascript application/x-javascript application/javascript;

    location ~* ^.+\.(jpg|jpeg|gif|png|ico)$  {
        root    /home/www/MY_DOMAIN_NAME;
        #access_log off;
        gzip off;
        expires 1d;
    }
    location ~* ^.+\.(css|js)$ {
        root    /home/www/MY_DOMAIN_NAME;
        #access_log off;
        expires 1d;
    }
    location ~* ^.+\.(pdf|gz|bz2|exe|rar|zip|7z)$ {
        root    /home/www/MY_DOMAIN_NAME;
        gzip off;
    }


    location / {
        proxy_pass   http://MY_DOMAIN_NAME:8080;
        proxy_redirect     off;

        proxy_set_header   Host             \$host;
        proxy_set_header   X-Real-IP        \$remote_addr;
        proxy_set_header   X-Forwarded-For  \$proxy_add_x_forwarded_for;
        proxy_max_temp_file_size 0;

        client_max_body_size       10m;
        client_body_buffer_size    128k;

        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;

        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
    }
}


Now, why do such a thing ?

Well, nginx is supposed to :

  • Need less memory
  • Be faster
  • Be able to handle more connections

So, I suppose it could help on a website with a bit of traffic, to lower the load that's put on Apache.

Pascal MARTIN
Ok maybe my question was not so clear. But this is the setup I have now. It is explained in my first paragraph. But now I am moving my static files to another domain. This way the recources I use drop even more. But what I wanted to know is if I then still need Nginx.
Saif Bechan
+1  A: 

No, you don't need nginx anymore.

chris
Can you give me some explanation why this is not needed anymore. Does it not help Apache anymore in any way.
Saif Bechan
I'm somewhat baffled at your question in the first place. You added nginx for the sole purpose of serving static content. You no longer serve static content, but you wonder if you still need nginx? It now serves no purpose, only adding overhead.
chris
I thought maybe it helped out for dynamic content also.
Saif Bechan
+1  A: 

You can also use nginx to offload SSL processing from the apache instances.

For example, we have one stack configured with nginx->haproxy->pool of apache servers. nginx and haproxy live together on a heartbeat cluster and feed requests into a pool of apache boxes on the backend. We install all the SSL certs on the nginx frontend.

Brian Bell
+3  A: 

Yes you absolutely do need nginx in front of Apache. Apache uses 1 thread or process per connection. Each of these threads occupy memory. If you have a few hundred people visiting your website and you have keepalive enabled, each of these browsers will keep an apache process or thread busy occupying memory on your server.

You can work around this by disabling keepalive on your apache server but this slows down the performance of your website because browsers can't reuse connections.

So instead you use nginx as a reverse proxy with keepalive enabled. It can maintain thousands of connections with a tiny memory footprint (about 8 megs). Because nginx is local to your apache server each request only occupies an apache child or thread for a few microseconds. That means you can serve thousands of people with only a tiny handful of apache processes.

Also nginx's configuration is much more flexible than apache and by having it on the front end it gives you a lot of flexibility.

Mark Maunder