Crmgo Blog

Apache & Nginx

Apache & Nginx

You can certainly search for information on why the Apache & Nginx combination is needed in other corners of the vast Internet, but I'm a cyclist, so I'll write it in my own words. During the operation of the Apache web server, it generates a new...

Apache & Nginx

The combination of Apache & Nginx can certainly be searched for in other corners of the vast Internet, but I am a cyclist, so I will write in my own words. During the operation of the Apache web server, it spawns a new process for every new connection to it. Even if there are no connections to the server, there are invariably 4-5 httpd processes in the system. Thus, Apache spawns an n-number of processes, which, of course, affects the amount of free RAM and increases the load on the server's CPU. Nginx allows you to reduce this load many times over. This web server combination works roughly like this: Nginx is installed on the frontend (frontend) and catches all incoming connections from user-agents of your visitors. Next, if static content such as HTML pages, photos, or shared files is requested, Nginx handles the request itself and serves it to the client as quickly as possible. If processing of some PHP script is required, then after catching the client's request, Nginx passes it to Apache (backend), which in turn processes the script using its mod_php and sends the ready-made data back to Nginx. At the same time, the number of running Apache processes does not increase (in such a combination, as I understand it, Apache has only one connection from 127.0.0.1, on which it accepts requests from Nginx). To put it even more simply, Nginx is a lightweight layer between the client and the main powerful web server, which prevents the spawning of Apache processes, thereby saving precious RAM. Now about the configuration. Since I do not use Debian and other Ubuntus, I will describe the installation and configuration process within Arch Linux.

sudo pacman -S nginx

After installing Nginx, you need to configure (you probably already have it) Apache. Open the config file:

sudo nano -w /etc/httpd/conf/httpd.conf

Find the line

ServerRoot "/etc/httpd"
Listen 80

and change it to 8080 (by default, Apache listens on the main port 80). Now Apache will wait for connections on port 8080, where our Nginx will send requests for script processing. In general, we are done with the Apache config; at least I did not change anything else there. Now let's move on to Nginx:

sudo nano -w /etc/nginx/conf/nginx.conf

You will need to uncomment some lines. In the very first one, uncomment the "user" line and enter your user name, most often it is http. Next, specify where Nginx will write error logs:

error_log  /var/log/httpd/error.log  notice;

Next, if we want to use Awstats to calculate visit statistics, uncomment this:

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';

The following settings are taken from some reputable resource, and at least for now I have not delved into their details.

access_log on;
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
client_max_body_size 20m;
tcp_nodelay on;
client_header_timeout 30;
client_body_timeout 30;
send_timeout 30;
client_header_buffer_size 4k;
large_client_header_buffers 4 4k;
output_buffers 10 64K;
postpone_output 1460;
keepalive_timeout 75 20;

I disabled gzip compression. Although everyone advises enabling it, after enabling it, my disk started spinning wildly, so I decided to turn it off. Now, the server settings: Configure Nginx to listen on port 80. I want to draw your attention that if you set the listen directive to 127.0.0.1:8080 in the Apache config, then here you also need to specify not listen 80, but listen 127.0.0.1:80.

 

server {
listen 80;
server_name syslinux.ru www.syslinux.ru;
charset utf-8;
access_log /var/log/httpd/host.access.log main;

 

This line specifies the log file where information about hosts connecting to nginx will be recorded. In all the resources I read about nginx configuration, people tried to enable statistics using various additional modules. However, in this case, if you already have awstats running on Apache, you only need to specify a new log file (in this case, host.access.log) in the awstats settings to collect statistics. Leave the location directive unchanged.

location / {

Next, look for the proxy line:

proxy_pass http://127.0.0.1:8080/;

Here we specify the address and port to which requests will be forwarded. This port is being listened to by Apache, which is exactly what we need. Another important line is root, which defines the location of the server files relative to the file system. In my case, it is set as follows:

root /srv/http;

Next, restart Apache

sudo /etc/rc.d/httpd restart

and try to start nginx:

sudo /etc/rc.d/nginx start
ссылка на статью

Note: This post may be AI-assisted and then human-edited. Editorial responsibility: Dmytro Lomaka, Kaltenkirchen.