Difference Apache vs Nginx - Nginx overtakes Apache

 

For a long time, I used Apache as a web server for running my websites, and later on Nginx every now and then in Docker. The alternative web server Nginx is becoming increasingly popular and has recently replaced Apache more and more as a web server in many areas. One reason for this is its performance: The somewhat younger, but nevertheless long grown-up Nginx scores in this area. For Apache, I used .htaccess files for direct control of folders in my setups: However, Nginx does not interpret these, accordingly an alternative configuration must be created when switching. For common requirements no problem, for special setups, for example using different paths based on a cookie, it became tricky, I could then only implement this with an unsightly workaround.

Webserver market share

First of all an overview about the distribution of the different web servers. If you believe w3techs.com, Nginx has surpassed Apache in May 2021 with a razor-thin lead:

webserver market share in %
Nginx 34.2%
Apache 30.1%
Cloudflare Server 21.9%
LiteSpeed 12.9%
Microsoft-IIS 4.9%
Source: w3techs.com; List last updated: 2024-04-12

Apache - HTTPd

The Apache HTTP Server has been around since 1995, currently in version: 2.4.59 (found: 2024-04-05).

Through the .htaccess file, certain properties of the web server can be configured at the folder level. As an example, a website operator purely needs folder access via FTP. Thanks to the .htaccess file, all relevant server settings, such as folder redirects, PHP settings, etc. can be controlled via the file in the web directory: Ideal for a web hosting several different sites and operators on one server. For this reason Apache is still very popular with shared hosters.

Advantages

  • Thanks to .htaccess files, access to specific folders can be controlled. The configuration can be stored directly in the respective folder.
  • modules can be loaded during operation
  • flexible configuration and more possibilities compared to nginx: especially for shared hosters.

Nginx

The clearly younger Nginx was published for the first time in the year 2004 and is up-to-date in the version 1.25.5 (found: 2024-04-17). Nginx can be used not only as a web server, but also as a reverse or email proxy. Among other things, ignoring the .htaccess file speeds up access: Apache has to look for the .htaccess files of all possible folders every time it is called, and read and interpret them if they exist; Nginx, on the other hand, uses a central configuration that resides in RAM after startup.

Advantages

  • Speed, especially with static files and high access numbers Nginx is usually faster than Apache

Config examples in comparison: Apache vs Nginx

As already mentioned, Apache can be configured via the .htaccess file. Nginx, on the other hand, uses a config file outside the web folder: nginx.conf.

Header Cache Control

Both Apache and Nginx can add a cache header to certain file types.

Apache

<FilesMatch "\.(ico|pdf|jpg|png|gif|jpeg|js|css)$">
 Header set Cache-Control "max-age=172800, public, must-revalidate"
</FilesMatch>

Nginx

http {
    ...
    server {
     ...
        location ~* \.(js|css|png|jpg|jpeg|gif|svg|ico)$ {
            expires 7d;
            add_header Cache-Control "public, no-transform";
        }

GZIP Compression

To save bandwidth, the page can be delivered compressed.

Apache

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
</IfModule> 

Nginx

http {
    ...
    gzip  on;
    gzip_vary on;
    gzip_min_length 10240;
    gzip_proxied any;
    gzip_comp_level 1;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

Redirect to www.

Non-www. pages can be redirected to a www. page as follows:

Apache

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

Nginx

server {
    listen       80;
    server_name  domain.tld;
    return       301 $scheme://www.domain.tld$request_uri;
}

server {
    listen       80;
    server_name  www.domain.tld;
    ...
}

Basic Authentication

Nginx

    server {
...
        auth_basic           "please enter a Username and Password";
        auth_basic_user_file /var/www/.htpasswd;

Cookies - Cache

Apache

In Apache cookies can be easily retrieved using a RewriteCondition:

RewriteCond %{HTTP_COOKIE} cookiename=cookievalue; [NC].

As an example, the following rules check if a cookie is set and a cache file exists and uses it for the call:

            RewriteCond %{HTTP_COOKIE} cache=no; [NC] 
            RewriteCond %{DOCUMENT_ROOT}/page-cache/pc__index__pc.html -f
            RewriteRule .? page-cache/pc__index__pc.html [L]

Nginx - Cookies and if

To control paths depending on a cookie, I've run into some limitations with Nginx. In Nginx cookies can be used in an IF-query, but the IF-query does not behave as I would have expected, so I could only use the query via the detour of a variable and named location:

http {
        #set variables for Cache...
        set $shouldusecache4root @usecache4root;

        #if Cookie
        if ($http_cookie ~* "cache=no(?:;|$)") {
            set $shouldusecache4root @nocache4root;
        }

        #hack locations...
        location = / {
            try_files /dev/null $shouldusecache4root;
        }

        #use named location from hacked location...
        location @nocache4root {
            try_files $uri /index.php?$is_args$args;
        }
        
...

see also: serverfault.com/questions/908086/nginx-directly-send-from-location-to-another-named-location/965779#965779 and www.nginx.com/resources/wiki/start/topics/depth/ifisevil/

Anyone who knows a better solution at this point, please feel free to post in the comments ...

Conclusion

Both web servers, Nginx as well as Apache come with an OpenSource license, offer numerous features and about the same security. In practice, it does not matter which of the two web servers is used for most requirements. Apache has its strengths when used in a shared hosting environment, Nginx when serving static content for high traffic sites. To measure the performance of a site, see also Website Stress Test - Performance Measure Requests/Second.

positive Bewertung({{pro_count}})
Rate Post:
{{percentage}} % positive
negative Bewertung({{con_count}})

THANK YOU for your review!

Questions / Comments


By continuing to browse the site, you agree to our use of cookies. More Details