Increase web server performance: we put nginx in front of apache

In one of the notes we described how to configure nginx + php-fpm. But nginx can be used in a slightly different way. For example, if you don't want to completely abandon apache, but you want to speed up the web server. (Perhaps you have implemented a complex redirect system that you don't want to port to nginx).

In this case, a possible solution is to use nginx only for serving static files (images, css, js, etc.). Other requests (to the php engine, for example) will still be redirected to apache (assuming you already have a site configured to work with it). To do this, configure nginx to serve static files, and proxy all other requests to apache. (In this case, you can do without php-fpm).The approximate nginx config (for http only):

# /etc/nginx/sites-enabled/site.com.conf

server {
    listen   80;
    server_name www.site.com site.com;

    access_log /var/log/nginx/site_access.log;
    error_log /var/log/nginx/site_error.log;

    location / {
        proxy_pass  http://backend;
        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_busy_buffers_size     64k;
        proxy_temp_file_write_size  64k;
    }

    location ~* \.(jpg|jpeg|gif|png|ico|css|bmp|swf|js|woff)$ {
        root /var/www/site.com;
        # Время жизни кеша ассетов
        expires 30d;
    }
}

http://backend - the configuration is moved to a separate file:

# /etc/nginx/conf.d/backend.conf

upstream backend {
  server 127.0.0.1:8080;
}

No special configuration is required for apache, except for changing the host to listen on a port other than the default:

<VirtualHost 127.0.0.1:8080>

Instead of 8080, you can use any available port, the main thing is to replace the default port 80 on which nginx will listen.

After configuration, don't forget to reload nginx and apache =)

If you have system.d:

systemctl reload apache2 nginx