Kunnskapsbase

Nginx, Apache Web Server Configuration & Optimization

Nginx (pronounced "engine-x") is a lightweight, high-performance web server and reverse proxy server. It was originally created as a solution to the C10K problem (handling 10,000 concurrent connections) and has since grown to become one of the most popular web servers in the world. Nginx is designed to serve static content quickly and efficiently and is widely used as a reverse proxy, load balancer, and HTTP cache.

Apache:

Apache HTTP Server, commonly referred to as Apache, is the most widely used web server software in the world. It was developed by the Apache Software Foundation and has been in use since 1995. Apache is known for its flexibility, extensive feature set, and strong community support. It supports dynamic content generation through modules such as PHP, Python, and others, making it a great choice for many types of web applications.

Installing Nginx and Apache

Installing Nginx:

On Ubuntu or Debian-based systems, you can install Nginx using the following commands:

sudo apt update
sudo apt install nginx

On CentOS or RHEL-based systems:

sudo yum install epel-release
sudo yum install nginx

After installation, you can start and enable Nginx with:

sudo systemctl start nginx
sudo systemctl enable nginx

Installing Apache:

On Ubuntu or Debian-based systems:

sudo apt update
sudo apt install apache2

On CentOS or RHEL-based systems:

sudo yum install httpd

Once installed, start and enable Apache:

sudo systemctl start apache2  # For Ubuntu/Debian
sudo systemctl enable apache2

sudo systemctl start httpd  # For CentOS/RHEL
sudo systemctl enable httpd

Basic Configuration of Nginx

Configuration Files:

Nginx's configuration file is typically located at /etc/nginx/nginx.conf. This is where most of the global server settings are made. The configuration is hierarchical, and each block (server, location) has its specific context.

Virtual Hosts in Nginx:

Nginx does not use the VirtualHost directive like Apache but instead uses server blocks to define each virtual host. A basic configuration for a virtual host in Nginx might look like this:

server {
    listen 80;
    server_name example.com;

    root /var/www/example.com/html;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

This configuration listens for HTTP requests on port 80 for the domain example.com, serves content from /var/www/example.com/html, and defines the index file to be index.html.

Common Directives:

  • listen: Specifies the IP address and port the server should listen on.
  • server_name: Defines the domain or IP address the server should respond to.
  • root: Defines the root directory where content is stored.
  • index: Specifies the default file served when a directory is accessed.

Basic Configuration of Apache

Configuration Files:

Apache's main configuration file is located at /etc/httpd/httpd.conf or /etc/apache2/apache2.conf, depending on the distribution. Additionally, virtual host configurations are often stored in /etc/apache2/sites-available/.

Virtual Hosts in Apache:

Apache uses the VirtualHost directive to configure multiple websites on the same server. Here’s an example:

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    DocumentRoot /var/www/example.com/html
    ServerName example.com

    <Directory /var/www/example.com/html>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

This configuration listens on port 80 for requests to example.com, serves files from /var/www/example.com/html, and provides error and access log configurations.

Common Directives:

  • DocumentRoot: Specifies the directory where the website’s files are located.
  • ServerName: Defines the domain name or IP address the virtual host responds to.
  • ErrorLog: Specifies the location for the error log.
  • CustomLog: Specifies the location for the access log.

Optimizing Nginx

Tuning Worker Processes:

Nginx is designed to be highly efficient in handling multiple concurrent connections. You can optimize it by configuring the number of worker processes:

worker_processes auto;
worker_connections 1024;

The worker_processes directive determines how many worker processes Nginx uses, and worker_connections sets the maximum number of simultaneous connections each worker can handle. You can adjust these values based on your server’s CPU and memory.

Gzip Compression:

Enabling Gzip compression reduces the size of the content sent to clients, speeding up page load times. In Nginx, this can be done by adding the following to the http block:

gzip on;
gzip_types text/plain text/css application/javascript;
gzip_comp_level 6;

Caching:

Nginx provides caching mechanisms that can reduce the load on your server by caching frequently requested content. Here's an example of a simple cache configuration:

location / {
    proxy_cache my_cache;
    proxy_cache_valid 200 1h;
    proxy_cache_use_stale error timeout updating;
}

Optimizing Apache

Tuning MPM (Multi-Processing Module):

Apache uses different MPMs to handle incoming requests. For optimal performance, it’s important to choose the right MPM and configure it. The two most commonly used MPMs are mpm_event and mpm_worker.

For example, with the mpm_event module:

<IfModule mpm_event_module>
    StartServers            2
    MinSpareThreads        25
    MaxSpareThreads       75
    ThreadLimit           64
    ThreadsPerChild       25
    MaxRequestWorkers    150
    MaxConnectionsPerChild   0
</IfModule>

Enabling Gzip Compression:

Just like Nginx, you can enable Gzip compression in Apache by adding the following to the configuration:

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

Caching:

Apache can cache content using modules like mod_cache and mod_cache_disk. Here’s an example of enabling disk caching:

<IfModule mod_cache.c>
    CacheRoot /var/cache/apache2/mod_cache
    CacheEnable disk /
</IfModule>

Security Best Practices for Nginx

  • Limit Request Size: Prevent large requests from overloading your server by limiting request size.

    client_max_body_size 10M;
    
  • Disable Unused Modules: Disable any modules you don’t need to reduce the attack surface.

  • SSL Configuration: Always serve your site over HTTPS. Use strong SSL configurations and let’s encrypt certificates.

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:...';
    

Security Best Practices for Apache

  • Disable Directory Listing: Prevent users from viewing directory listings.

    Options -Indexes
    
  • Disable Unused Modules: Apache comes with many modules that may not be necessary. Disable unused ones to minimize vulnerabilities.

  • SSL Configuration: Similar to Nginx, ensure that SSL is configured properly:

    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/example.com.crt
    SSLCertificateKeyFile /etc/ssl/private/example.com.key
    

Comparing Nginx and Apache

  • Performance: Nginx tends to perform better in scenarios with heavy static content due to its event-driven architecture. Apache, however, is often preferred for dynamic content, especially with PHP or other modules.

  • Configuration: Nginx has a more concise and less complex configuration, while Apache is more flexible and has a larger ecosystem of modules.

  • Resource Usage: Nginx is generally more efficient in terms of memory and CPU usage, especially under heavy load.

  • Community Support: Both Nginx and Apache have strong community support, but Apache has a longer track record.

Nginx and Apache are both powerful, widely-used web servers, each with its strengths and configuration options. By carefully configuring and optimizing your server, you can achieve optimal performance and security. Choose Nginx if you need high performance with low resource consumption, especially for static content and reverse proxying. Choose Apache if you need flexibility, a robust module ecosystem, and extensive support for dynamic content.Proper server configuration and optimization are essential to ensure that your websites run efficiently and securely. Whether you're running a small blog or a high-traffic enterprise application, Nginx and Apache offer the tools you need to meet your web server needs.

  • 0 brukere syntes dette svaret var til hjelp
Var dette svaret til hjelp?