Will Nginx as a reverse proxy for Apache help only for dynamic content

I plan to move all my static content to CDN, so on my server I only have dynamic content left. Now I have Nginx configured as a reverse proxy for Apache. A static request that came to where Nginx is directly delivered without having to switch to Apache.

In this case, Nginx processed most of the request, and I clearly see the need for Nginx.

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

Are there any other advantages of using Nginx and Apache for dynamic content only?

My dynamic content is PHP / MySQL

Edit:

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

+6
php apache nginx cdn
source share
5 answers

No, you no longer need nginx.

+2
source share

Yes, you absolutely need nginx before Apache. Apache uses 1 thread or process for each connection. Each of these threads takes up memory. If you have several hundred people visiting your site and you have keepalive enabled, each of these browsers will support the apache process or the thread occupied by the 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 as browsers cannot reuse connections.

So, instead of nginx, you are using a reverse proxy with keepalive enabled. It can support thousands of connections with a small amount of memory (about 8 megabytes). Since nginx is local to your apache server, each request only takes a child or apache thread for several microseconds. This means that you can serve thousands of people with only a tiny fraction of apache processes.

In addition, nginx configuration is much more flexible than apache, and thanks to its presence on the front panel, it gives you more flexibility.

+14
source share

What I did for one website:

  • configure nginx as a reverse proxy before Apache
  • configure it like this:
    • Requests for PHP pages (i.e. dynamic content) are sent to Apache
    • Requests for static files (CSS, JS, ...) are directly served by nginx.

This is without the need to configure two domains: everything is in the same domain.


I basically did this:

  • 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 other Apache requests


This is what my nginx 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; } } 


So why do this?

Well, nginx should:

  • Less memory required
  • Faster
  • Be able to handle more connections

So, I suppose this could help on a website with a bit of traffic to reduce the load on Apache.

+3
source share

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

For example, we have one stack configured with the nginx-> haproxy-> pool for Apache servers. nginx and haproxy live together in a heartbeat cluster and submit requests to the Apache pool on the backend. We install all SSL certificates on the nginx interface.

+2
source share

nginx in front is the best solution if you are using Apache 1.3:

nginx can easily handle thousands of connections, but Apache cannot

0
source share

All Articles