### Start Basic Server Source: https://context7.com/emikulic/darkhttpd/llms.txt Serve files from a specified directory on the default port. The server binds to port 80 when running as root, or port 8080 otherwise. ```bash ./darkhttpd /var/www/htdocs # Output: # darkhttpd/1.17.from.git, copyright (c) 2003-2025 Emil Mikulic. # listening on: http://0.0.0.0:8080/ ``` -------------------------------- ### Start darkhttpd as root and drop privileges Source: https://context7.com/emikulic/darkhttpd/llms.txt Start the server as root to bind to privileged ports (like 80) and then drop privileges to a specified user and group for security. ```bash sudo ./darkhttpd /var/www/htdocs --port 80 --uid www --gid www ``` -------------------------------- ### Darkhttpd Response for Range Request Source: https://context7.com/emikulic/darkhttpd/llms.txt Example HTTP response headers for a successful partial content request. ```text HTTP/1.1 206 Partial Content Content-Range: bytes 0-999/1048576 Content-Length: 1000 ``` -------------------------------- ### Serve Files with Darkhttpd and Test Range Requests Source: https://context7.com/emikulic/darkhttpd/llms.txt Starts darkhttpd serving files from a directory and demonstrates requesting a specific byte range using curl. The server automatically handles Range requests. ```bash ./darkhttpd ~/videos --port 8080 ``` ```bash curl -H "Range: bytes=0-999" http://localhost:8080/video.mp4 -o partial.mp4 ``` -------------------------------- ### Hide dotfiles Source: https://context7.com/emikulic/darkhttpd/llms.txt Prevent darkhttpd from serving hidden files (those starting with a dot) by using the --hide-dotfiles flag. ```bash ./darkhttpd /var/www/htdocs --hide-dotfiles ``` -------------------------------- ### Build darkhttpd Source: https://github.com/emikulic/darkhttpd/blob/master/README.md Compile the source code using make. ```bash make ``` ```bash CC=gcc make ``` -------------------------------- ### Run darkhttpd Source: https://github.com/emikulic/darkhttpd/blob/master/README.md Execute the binary with various command-line arguments to configure server behavior. ```bash ./darkhttpd /var/www/htdocs ``` ```bash ./darkhttpd ~/public_html --port 8081 ``` ```bash ./darkhttpd ~/public_html --addr 192.168.0.1 ``` ```bash ./darkhttpd ~/public_html --maxconn 4 ``` ```bash ./darkhttpd ~/public_html --log access.log ``` ```bash ./darkhttpd /var/www/htdocs --chroot ``` ```bash ./darkhttpd /var/www/htdocs --index default.htm ``` ```bash $ cat extramime text/plain dat $ ./darkhttpd /var/www/htdocs --mimetypes extramime ``` ```bash ./darkhttpd /var/www/htdocs --uid www --gid www ``` ```bash kldload accf_http ./darkhttpd /var/www/htdocs --accf ``` ```bash ./darkhttpd /var/www/htdocs --pidfile /var/run/httpd.pid --daemon ``` ```bash ./darkhttpd ~/public_html/index.html --single-file ``` ```bash ./darkhttpd /var/www/htdocs --forward example.com http://www.example.com \ --forward secure.example.com https://www.example.com/secure ``` ```bash ./darkhttpd /var/www/htdocs --forward example.com http://www.example.com \ --forward-all http://catchall.example.com ``` ```bash ./darkhttpd /var/www/htdocs --header 'Access-Control-Allow-Origin: *' ``` ```bash ./darkhttpd ~/public_html --port 8080 --addr 127.0.0.1 ``` ```bash ./darkhttpd ``` -------------------------------- ### Run darkhttpd in Docker Source: https://github.com/emikulic/darkhttpd/blob/master/README.md Build and execute the server within a containerized environment. ```bash docker build -t darkhttpd . ``` ```bash docker run -p 8080:80 -v ~/dev/mywebsite:/var/www/htdocs:ro darkhttpd ``` -------------------------------- ### Build Docker Image for Darkhttpd Source: https://context7.com/emikulic/darkhttpd/llms.txt Builds a Docker image for darkhttpd. Ensure you have a Dockerfile in the current directory. ```bash docker build -t darkhttpd . ``` -------------------------------- ### Enable IPv6 Support Source: https://context7.com/emikulic/darkhttpd/llms.txt Enable IPv6 listening with the --ipv6 flag. The server can listen on both IPv4 and IPv6 simultaneously when binding to '::'. ```bash # Listen on IPv6 (dual-stack, accepts both IPv4 and IPv6) ./darkhttpd /var/www/htdocs --ipv6 --port 8080 # Output: # darkhttpd/1.17.from.git, copyright (c) 2003-2025 Emil Mikulic. # listening on: http://[::]:8080/ ``` -------------------------------- ### Enable Basic Authentication Source: https://context7.com/emikulic/darkhttpd/llms.txt Enable HTTP Basic Authentication with the --auth flag, providing a username and password. Note that credentials are sent unencrypted over HTTP. ```bash ./darkhttpd /var/www/htdocs --auth admin:secretpassword ``` ```bash curl -u admin:secretpassword http://localhost:8080/ ``` -------------------------------- ### Test darkhttpd Source: https://github.com/emikulic/darkhttpd/blob/master/README.md Run the test suite to verify functionality. ```bash make test ``` ```bash ASAN_OPTIONS=" " PYTHON=python3.11 make test ``` -------------------------------- ### Enable Chroot Security Source: https://context7.com/emikulic/darkhttpd/llms.txt Lock the server into the document root directory using the `--chroot` flag for enhanced security. This requires root privileges on most systems. ```bash # Run with chroot (requires root) sudo ./darkhttpd /var/www/htdocs --chroot # Output: # darkhttpd/1.17.from.git, copyright (c) 2003-2025 Emil Mikulic. # chrooted to `/var/www/htdocs' # listening on: http://0.0.0.0:80/ ``` -------------------------------- ### Build Darkhttpd from Source Source: https://context7.com/emikulic/darkhttpd/llms.txt Commands for building darkhttpd from source using make. You can specify the C compiler and add custom CFLAGS for debugging or feature toggling. ```bash make ``` ```bash CC=clang make ``` ```bash make CFLAGS="-g -DDEBUG" ``` ```bash make CFLAGS="-O2 -DNO_IPV6" ``` -------------------------------- ### Configure Custom Port Source: https://context7.com/emikulic/darkhttpd/llms.txt Bind the server to a specific port using the --port flag. Use port 0 to let the system assign any available port. ```bash # Serve ~/public_html on port 8081 ./darkhttpd ~/public_html --port 8081 # Output: # darkhttpd/1.17.from.git, copyright (c) 2003-2025 Emil Mikulic. # listening on: http://0.0.0.0:8081/ ``` -------------------------------- ### Enable FreeBSD accept filter Source: https://context7.com/emikulic/darkhttpd/llms.txt On FreeBSD systems, enable the accept filter for improved network performance using the --accf flag. This requires the accf_http kernel module to be loaded. ```bash sudo kldload accf_http ``` ```bash ./darkhttpd /var/www/htdocs --accf ``` -------------------------------- ### Run Darkhttpd in Docker Source: https://context7.com/emikulic/darkhttpd/llms.txt Runs a darkhttpd container, mapping host ports and volumes. Use ':ro' for read-only volume mounts. ```bash docker run -d \ -p 8080:80 \ -v ~/mywebsite:/var/www/htdocs:ro \ darkhttpd ``` ```bash docker run -d \ -p 8080:80 \ -v ~/mywebsite:/var/www/htdocs:ro \ darkhttpd . --no-listing --hide-dotfiles ``` -------------------------------- ### Configure URL forwarding (301 Redirects) Source: https://context7.com/emikulic/darkhttpd/llms.txt Use the --forward flag to redirect requests for specific hosts to different URLs. Multiple --forward options can be chained. ```bash ./darkhttpd /var/www/htdocs \ --forward example.com http://www.example.com \ --forward old.example.com http://new.example.com ``` -------------------------------- ### Run darkhttpd as a daemon with PID file Source: https://context7.com/emikulic/darkhttpd/llms.txt Run darkhttpd in the background using the --daemon flag and specify a PID file to manage the process. Use 'cat' to view the PID and 'kill' to stop the daemon. ```bash ./darkhttpd /var/www/htdocs --daemon --pidfile /var/run/darkhttpd.pid ``` ```bash cat /var/run/darkhttpd.pid ``` ```bash kill $(cat /var/run/darkhttpd.pid) ``` -------------------------------- ### Configure Privilege Dropping Source: https://context7.com/emikulic/darkhttpd/llms.txt Drop root privileges after initialization using `--uid` and `--gid` flags. This is essential for secure production deployments. ```bash ``` -------------------------------- ### Production Deployment of Darkhttpd Source: https://context7.com/emikulic/darkhttpd/llms.txt A comprehensive production deployment command for darkhttpd, including security and operational flags. Ensure the log directory exists and has correct permissions. ```bash sudo ./darkhttpd /var/www/htdocs \ --port 80 \ --chroot \ --uid www-data \ --gid www-data \ --daemon \ --pidfile /var/run/darkhttpd.pid \ --log /var/log/darkhttpd/access.log \ --maxconn 500 \ --timeout 30 \ --no-listing \ --hide-dotfiles \ --header 'X-Content-Type-Options: nosniff' \ --header 'X-Frame-Options: SAMEORIGIN' ``` -------------------------------- ### Enable Access Logging Source: https://context7.com/emikulic/darkhttpd/llms.txt Enable access logging to a file using the --log flag. Logs are written in Common Log Format (CLF), compatible with standard log analysis tools. ```bash # Log all accesses to a file ./darkhttpd ~/public_html --log /var/log/darkhttpd/access.log # Log format example: # 192.168.1.100 - - [15/Jan/2025:10:30:45 +0000] "GET /index.html HTTP/1.1" 200 1234 "-" "Mozilla/5.0" ``` -------------------------------- ### Integrate with Syslog Source: https://context7.com/emikulic/darkhttpd/llms.txt Send access logs to syslog instead of a file using the --syslog flag. This is useful for centralized logging infrastructure. ```bash # Log to syslog facility ./darkhttpd /var/www/htdocs --syslog ``` -------------------------------- ### Configure Custom Index File Source: https://context7.com/emikulic/darkhttpd/llms.txt Change the default directory index file from `index.html` to another filename using the `--index` flag. Requests to directories without an index file will now serve the custom index file if it exists. ```bash # Use default.htm as index file ./darkhttpd /var/www/htdocs --index default.htm # Request to /docs/ will serve /docs/default.htm if it exists ``` -------------------------------- ### Configure Custom MIME Types Source: https://context7.com/emikulic/darkhttpd/llms.txt Add or override MIME type mappings by providing a mime types file using the `--mimetypes` flag. This allows serving files with specific extensions under custom MIME types. ```bash # Create custom mime types file cat > /etc/darkhttpd/extramime << 'EOF' text/plain dat log application/json api video/webm webm EOF # Start server with custom mime types ./darkhttpd /var/www/htdocs --mimetypes /etc/darkhttpd/extramime # .dat files will now be served as text/plain ``` -------------------------------- ### Serve a single file Source: https://context7.com/emikulic/darkhttpd/llms.txt Configure darkhttpd to serve a single specified file for all requests using the --single-file flag. This is useful for single-page applications. ```bash ./darkhttpd /var/www/index.html --single-file --port 8080 ``` -------------------------------- ### Configure connection timeout Source: https://context7.com/emikulic/darkhttpd/llms.txt Set the idle connection timeout in seconds using the --timeout flag. A value of 0 disables timeouts. ```bash ./darkhttpd /var/www/htdocs --timeout 60 ``` ```bash ./darkhttpd /var/www/htdocs --timeout 0 ``` -------------------------------- ### Forward all requests to a new domain Source: https://context7.com/emikulic/darkhttpd/llms.txt Redirect all incoming requests to a single specified URL using the --forward-all flag. ```bash ./darkhttpd /var/www/htdocs --forward-all http://newsite.example.com ``` -------------------------------- ### Bind to Specific Network Interface Source: https://context7.com/emikulic/darkhttpd/llms.txt Restrict the server to listen on a specific IP address using the --addr flag. This is useful for local development or when running on systems with multiple network interfaces. ```bash # Only listen on localhost (useful for local development) ./darkhttpd ~/public_html --port 8080 --addr 127.0.0.1 # Output: # darkhttpd/1.17.from.git, copyright (c) 2003-2025 Emil Mikulic. # listening on: http://127.0.0.1:8080/ ``` -------------------------------- ### Test Darkhttpd Server Source: https://context7.com/emikulic/darkhttpd/llms.txt Tests if the darkhttpd server is running and accessible via curl. ```bash curl http://localhost:8080/ ``` -------------------------------- ### Limit simultaneous connections Source: https://context7.com/emikulic/darkhttpd/llms.txt Use the --maxconn flag to set a limit on the number of concurrent connections the server will handle, preventing resource exhaustion. ```bash ./darkhttpd /var/www/htdocs --maxconn 100 ``` -------------------------------- ### Add custom response headers Source: https://context7.com/emikulic/darkhttpd/llms.txt Use the --header flag to append custom HTTP headers to all responses. This flag can be used multiple times for different headers. ```bash ./darkhttpd /var/www/htdocs \ --header 'Access-Control-Allow-Origin: *' \ --header 'X-Content-Type-Options: nosniff' \ --header 'X-Frame-Options: DENY' ``` -------------------------------- ### Trust proxy IP for X-Forwarded-For header Source: https://context7.com/emikulic/darkhttpd/llms.txt Specify trusted proxy IPs using --trusted-ip to ensure the client's real IP address from the X-Forwarded-For header is logged, rather than the proxy's IP. ```bash ./darkhttpd /var/www/htdocs --trusted-ip 10.0.0.1 --log access.log ``` -------------------------------- ### Redirect HTTP to HTTPS behind a reverse proxy Source: https://context7.com/emikulic/darkhttpd/llms.txt Use the --forward-https flag to redirect HTTP requests to HTTPS when darkhttpd is running behind an SSL-terminating reverse proxy. This relies on the X-Forwarded-Proto header. ```bash ./darkhttpd /var/www/htdocs --forward-https ``` -------------------------------- ### Disable Directory Listings Source: https://context7.com/emikulic/darkhttpd/llms.txt Prevent automatic directory listing generation when no index file exists using the `--no-listing` flag. This enhances security by not exposing directory contents. ```bash # Disable directory listings for security ./darkhttpd /var/www/htdocs --no-listing # Requests to directories without index.html now return: # HTTP/1.1 404 Not Found ``` -------------------------------- ### Darkhttpd Caching with If-Modified-Since Source: https://context7.com/emikulic/darkhttpd/llms.txt Demonstrates how darkhttpd handles If-Modified-Since headers for caching. The first request retrieves the file, while a subsequent conditional request with the same 'Last-Modified' header returns a 304 Not Modified status. ```bash curl -v http://localhost:8080/style.css # Response: HTTP/1.1 200 OK # Last-Modified: Mon, 15 Jan 2025 10:00:00 GMT ``` ```bash curl -v -H "If-Modified-Since: Mon, 15 Jan 2025 10:00:00 GMT" http://localhost:8080/style.css # Response: HTTP/1.1 304 Not Modified ``` -------------------------------- ### Set Default MIME Type Source: https://context7.com/emikulic/darkhttpd/llms.txt Set the MIME type for files with unrecognized extensions using the `--default-mimetype` flag. This overrides the default `application/octet-stream`. ```bash # Serve unknown files as plain text instead of application/octet-stream ./darkhttpd /var/www/htdocs --default-mimetype "text/plain" ``` -------------------------------- ### Hide server identity Source: https://context7.com/emikulic/darkhttpd/llms.txt Remove server identification details from HTTP headers and directory listings by using the --no-server-id flag. ```bash ./darkhttpd /var/www/htdocs --no-server-id ``` -------------------------------- ### Disable Keep-Alive connections Source: https://context7.com/emikulic/darkhttpd/llms.txt Use the --no-keepalive flag to disable HTTP Keep-Alive, ensuring a new connection is established for each request. This can be useful for debugging or specific proxy configurations. ```bash ./darkhttpd /var/www/htdocs --no-keepalive ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.