Skip to content

A self-hosted media server and file management system

feat: simplify docker nginx setup and remove manual nginx config files

aminnausin076653bbd387
diff --git a/Dockerfile b/Dockerfile
index 3fc1e495..fcdeab67 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -116,7 +116,8 @@ COPY --chown=www-data:www-data . .
COPY --chown=www-data:www-data .env.example .env
# Copy Nginx config
-COPY docker/etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf
+COPY docker/etc/nginx/nginx.conf /etc/nginx/nginx.conf.template
+COPY docker/etc/nginx/site-opts.d/http.conf /etc/nginx/site-opts.d/http.conf.template
# Set permissions for Laravel runtime and generate git info
# Setup NGINX
diff --git a/docker-compose.yaml b/docker-compose.yaml
index 912edb7f..51ee7d60 100644
--- a/docker-compose.yaml
+++ b/docker-compose.yaml
@@ -6,8 +6,6 @@ services:
- ./app:/var/www/html/storage/app/private
- ./data/media:/var/www/html/storage/app/public/media
- ./data/thumbnails:/var/www/html/storage/app/public/thumbnails
- - ./docker/etc/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf
- - ./docker/etc/nginx/nginx.conf:/etc/nginx/nginx.conf
- ./logs/mediaServer:/var/www/html/storage/logs
- ./logs/nginx:/var/log/nginx
- shared-env:/var/www/html/shared
@@ -17,6 +15,7 @@ services:
- PUID=${PUID:-1000}
- PGID=${PGID:-1000}
- HEALTHCHECK_PATH=/up
+ - NGINX_APP_HOST=${APP_HOST:-app.test}
restart: unless-stopped
healthcheck:
test: curl --insecure --silent --location --show-error --fail http://localhost:8080${HEALTHCHECK_PATH:-/up} || exit 1
diff --git a/docker/.env.docker b/docker/.env.docker
index abb76ffb..353d155c 100644
--- a/docker/.env.docker
+++ b/docker/.env.docker
@@ -12,6 +12,7 @@ APP_SCHEME=http
# App URL - Website will be available on http://${APP_HOST}:APP_PORT
APP_HOST=app.test
+NGINX_APP_HOST=${APP_HOST}
# App Port - The locally accessible port for your app at http://localhost:port (for reverse proxy and port forwarding)
APP_PORT=8084
diff --git a/docker/etc/nginx/conf.d/default.conf b/docker/etc/nginx/conf.d/default.conf
deleted file mode 100644
index 8b52b511..00000000
--- a/docker/etc/nginx/conf.d/default.conf
+++ /dev/null
@@ -1,107 +0,0 @@
-server {
- listen 8080;
- server_name _;
-
- resolver 127.0.0.11 valid=10s;
-
- root /var/www/html/public;
-
- location ~ ^/(app|apps)/ {
- set $ws_upstream "ws:6001";
- proxy_pass http://$ws_upstream;
-
- proxy_http_version 1.1;
-
- proxy_set_header Host $host;
- proxy_set_header Scheme $scheme;
- proxy_set_header SERVER_PORT $server_port;
- proxy_set_header REMOTE_ADDR $remote_addr;
-
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-Proto $scheme;
-
- proxy_set_header Upgrade $http_upgrade;
- proxy_set_header Connection "Upgrade";
-
- proxy_connect_timeout 60s;
- proxy_send_timeout 60s;
- proxy_read_timeout 60s;
-
- proxy_intercept_errors on;
- error_page 502 503 504 =404 /404;
- }
-
- index index.php;
-
- error_log /var/log/nginx/error.log warn;
- access_log /var/log/nginx/access.log custom if=$loggable;
-
- add_header X-Frame-Options "SAMEORIGIN";
- add_header X-Content-Type-Options "nosniff";
-
- include mime.types;
-
- location = /manifest.json {
- access_log off;
- add_header Cache-Control "public, max-age=3600" always;
- add_header Content-Type "application/json" always;
- add_header X-Served-By "nginx2" always;
- try_files $uri =404;
- }
-
- location / {
- try_files $uri $uri/ /index.php?$query_string;
- autoindex off;
- gzip_static on;
- }
-
- location ~ \.php$ {
- include fastcgi.conf;
- include fastcgi_params;
- try_files $uri =404;
-
- fastcgi_split_path_info ^(.+\.php)(/.+)$;
-
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- fastcgi_param PATH_INFO $fastcgi_path_info;
-
- fastcgi_read_timeout 3600;
- fastcgi_hide_header X-Powered-By;
- }
-
- charset utf-8;
-
- location = /favicon.ico {
- access_log off;
- log_not_found off;
- }
- location = /robots.txt {
- access_log off;
- log_not_found off;
- }
-
- location ~ /\.(?!well-known).* {
- deny all;
- }
-
- location ~* \.(?:css|js|woff2?|ttf|eot|otf|ico|jpe?g|png|gif|webp|svg)$ {
- add_header Cache-Control "public, max-age=604800, must-revalidate";
- try_files $uri =404;
- }
-
- location /storage/ {
- location ~* /\.storage/(thumbnails|posters|previews)/ {
- add_header Cache-Control "public, max-age=604800, must-revalidate";
- try_files $uri =404;
- allow all;
- }
-
- valid_referers 127.0.0.1, app.test;
- if ($invalid_referer) {
- return 403;
- }
- }
-}
diff --git a/docker/etc/nginx/nginx.conf b/docker/etc/nginx/nginx.conf
index 12163704..8a5b96bd 100644
--- a/docker/etc/nginx/nginx.conf
+++ b/docker/etc/nginx/nginx.conf
@@ -1,6 +1,6 @@
worker_processes auto;
-error_log /dev/stderr warn;
+error_log $NGINX_ERROR_LOG $LOG_OUTPUT_LEVEL;
pid /var/run/nginx.pid;
daemon off;
@@ -13,7 +13,7 @@ events {
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
- server_tokens off;
+ server_tokens $NGINX_SERVER_TOKENS;
# set_real_ip_from caddy;
real_ip_header X-Forwarded-For;
@@ -22,20 +22,28 @@ http {
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
- access_log /dev/stdout custom;
-
map $request_uri $loggable {
+ $HEALTHCHECK_PATH 0;
~^/up$ 0;
~^/(app|apps)/$ 0;
default 1;
}
+ access_log $NGINX_ACCESS_LOG custom if=$loggable;
+
+ client_max_body_size $NGINX_CLIENT_MAX_BODY_SIZE;
+
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
- #gzip on;
+ gzip on;
+ gzip_static on;
+ gzip_vary on;
+ gzip_proxied any;
+ gzip_comp_level 6;
+ gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/atom+xml image/svg+xml;
include /etc/nginx/conf.d/*.conf;
}
diff --git a/docker/etc/nginx/site-opts.d/http.conf b/docker/etc/nginx/site-opts.d/http.conf
index 3669a237..a95eafe3 100644
--- a/docker/etc/nginx/site-opts.d/http.conf
+++ b/docker/etc/nginx/site-opts.d/http.conf
@@ -1,45 +1,113 @@
-listen 8080 default_server;
-listen [::]:8080 default_server;
-
+listen ${NGINX_HTTP_PORT} default_server;
+listen [::]:${NGINX_HTTP_PORT} default_server;
+server_name _;
+resolver 127.0.0.11 valid=10s;
root /var/www/html/public;
-
index index.php;
-server_name _;
charset utf-8;
-client_max_body_size 2048M;
-include mime.types;
+error_log /var/log/nginx/error.log warn;
+access_log /var/log/nginx/access.log custom if=$loggable;
+
+add_header X-Frame-Options "SAMEORIGIN" always;
+add_header X-Content-Type-Options "nosniff" always;
+add_header Referrer-Policy "strict-origin-when-cross-origin" always;
+add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
-# Healthchecks: Set /healthcheck to be the healthcheck URL
-location /healthcheck {
+location = /favicon.ico { access_log off; log_not_found off; }
+location = /robots.txt { access_log off; log_not_found off; }
+location ~ /\.ht { deny all; }
+
+location ^~ /up {
access_log off;
+ try_files $uri $uri/ /index.php;
+}
- # set max 5 seconds for healthcheck
- fastcgi_read_timeout 5s;
+location ~ ^/(app|apps)/ {
+ set $ws_upstream "ws:6001";
+ proxy_pass http://$ws_upstream;
- include fastcgi_params;
- fastcgi_param SCRIPT_NAME /healthcheck;
- fastcgi_param SCRIPT_FILENAME /healthcheck;
- fastcgi_pass 127.0.0.1:9000;
+ proxy_http_version 1.1;
+
+ proxy_set_header Host $host;
+ proxy_set_header Scheme $scheme;
+ proxy_set_header SERVER_PORT $server_port;
+ proxy_set_header REMOTE_ADDR $remote_addr;
+
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Real-IP $remote_addr;
+ proxy_set_header X-Forwarded-Proto $scheme;
+
+ proxy_set_header Upgrade $http_upgrade;
+ proxy_set_header Connection "Upgrade";
+
+ proxy_connect_timeout 60s;
+ proxy_send_timeout 60s;
+ proxy_read_timeout 60s;
+
+ proxy_intercept_errors on;
+ error_page 502 503 504 =404 /404;
+}
+
+# --- storage (user content, private) ---
+
+location ~* ^/storage/media/ {
+ valid_referers 127.0.0.1, ${NGINX_APP_HOST};
+ if ($invalid_referer) { return 403; }
+ add_header Cache-Control "private, max-age=3600";
+ try_files $uri =404;
+}
+
+location ~* ^/storage/.*\.(jpg|jpeg|png|gif|webp|svg|woff|woff2|ttf)$ {
+ access_log off;
+ add_header Cache-Control "private, no-cache";
+ try_files $uri =404;
+}
+
+# --- build assets (public, immutable) ---
+
+location ~* \.(js|css|map|woff|woff2|ttf)$ {
+ access_log off;
+ add_header Cache-Control "public, max-age=31536000, immutable";
+ try_files $uri =404;
+ # 1y (hashed files so immutable)
+}
+
+location ~* \.(jpg|jpeg|png|gif|webp|svg|ico)$ {
+ access_log off;
+ add_header Cache-Control "public, max-age=2592000";
+ try_files $uri =404;
+ # 3d
}
+location = /manifest.json {
+ access_log off;
+ add_header Cache-Control "public, max-age=3600" always;
+ add_header Content-Type "application/json" always;
+ add_header X-Served-By "nginx2" always;
+ try_files $uri =404;
+}
+
+# --- base ---
+
location / {
try_files $uri $uri/ /index.php?$query_string;
+ autoindex off;
+}
+
+location ~* ^/storage/.*\.php$ {
+ deny all;
}
location ~ \.php$ {
- include fastcgi.conf;
- include fastcgi_params;
try_files $uri =404;
+ include fastcgi_params;
- fastcgi_split_path_info ^(.+\.php)(/.+)$;
-
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- fastcgi_param PATH_INFO $fastcgi_path_info;
-
- fastcgi_read_timeout 3600;
+ fastcgi_pass 127.0.0.1:9000;
+ fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
+ fastcgi_buffers $NGINX_FASTCGI_BUFFERS;
+ fastcgi_buffer_size $NGINX_FASTCGI_BUFFER_SIZE;
+ fastcgi_read_timeout $PHP_MAX_EXECUTION_TIME;
fastcgi_hide_header X-Powered-By;
}
diff --git a/startDocker.bat b/startDocker.bat
index b610d914..de5ee36a 100644
--- a/startDocker.bat
+++ b/startDocker.bat
@@ -5,7 +5,6 @@ SET "VOLUME_UID=1000"
SET "VOLUME_GID=1000"
SET "ENV_FILE=./.env"
SET "FALLBACK_DEFAULT_DOMAIN=app.test"
-SET "NGINX_CONF_FILE=docker\etc\nginx\conf.d\default.conf"
echo.
echo ============================================
echo mediaServer Docker Setup
@@ -45,19 +44,6 @@ if not exist docker-compose.yaml (
echo 'docker-compose.yaml' file.
)
echo.
-
-:: Check for nginx configuration
-if not exist "docker\etc\nginx\conf.d\default.conf" (
- call :ColorText "[ERROR]" Red
- echo Missing 'docker/etc/nginx/conf.d/default.conf' file.
- echo Please ensure this file is present in the correct directory.
- pause
- goto :end
-) else (
- call :ColorText "[FOUND]" Green
- echo 'nginx' configuration file.
-)
-echo.
rem --------------------------------------------------
:: Check for .env.docker
@@ -238,51 +224,6 @@ IF %ERRORLEVEL% NEQ 0 (
)
echo.
-REM Update Nginx configuration
-
-SET "SCRIPT_DIR=%~dp0"
-SET "NGINX_CONF_FILE=%SCRIPT_DIR%%NGINX_CONF_FILE%"
-
-SET "TEMP_NGINX_CONF_FILE=%NGINX_CONF_FILE%.tmp"
-
-(
- for /f "tokens=1,* delims=:" %%a in ('findstr /n "^" "%NGINX_CONF_FILE%"') do (
- if "%%b"=="" (
- echo.
- ) else (
- echo %%b | findstr /i "valid_referers 127.0.0.1," >nul
- if errorlevel 1 (
- echo %%b
- ) else (
- echo valid_referers 127.0.0.1, %APP_HOST%;
- )
- )
- )
-) > "%TEMP_NGINX_CONF_FILE%"
-
-ver >nul
-
-IF NOT EXIST "%TEMP_NGINX_CONF_FILE%" (
- call :ColorText "[ERROR]" RED
- echo Temp Nginx config file not created.
- pause
- goto :end
-)
-
-MOVE /Y "%TEMP_NGINX_CONF_FILE%" "%NGINX_CONF_FILE%" >nul
-
-IF %ERRORLEVEL% NEQ 0 (
- call :ColorText "[ERROR]" RED
- echo Failed to replace original Nginx config file.
- echo Please check script permissions.
- pause
- goto :end
-) ELSE (
- call :ColorText "[SUCCESS] " GREEN
- echo Nginx valid_referers updated to include: %APP_HOST%
-)
-echo.
-
call :ColorText "[STEP 3/5] " Yellow
echo Stopping and cleaning up existing mediaServer Docker containers...
echo.
@@ -382,7 +323,7 @@ echo ============================================
echo SETUP COMPLETED SUCCESSFULLY!
echo ============================================
echo.
-echo Your mediaServer will be available at https://%APP_HOST% or http://127.0.0.1:%APP_PORT%
+echo Your mediaServer will be available at https://%APP_HOST% or http://%APP_HOST%:%APP_PORT%
echo.
echo To add audio or video to your server, put the files in ./data/media organised by /LIBRARY/FOLDER/VIDEO.mp4
echo.
diff --git a/startDocker.sh b/startDocker.sh
index 6fe4bf1b..d523db46 100644
--- a/startDocker.sh
+++ b/startDocker.sh
@@ -9,7 +9,6 @@ PGID=${PGID:-1000}
SHARED_VOLUME_NAME="mediaserver-shared-env"
ENV_FILE="./.env" # Path to your .env file
FALLBACK_DEFAULT_DOMAIN="app.test" # Default if APP_URL is not found in .env
-NGINX_CONF_FILE="docker/etc/nginx/conf.d/default.conf" # Path to Nginx config
# --- End Configuration ---
# --- Define color variables ---
@@ -52,16 +51,6 @@ else
fi
echo
-# Check for nginx configuration
-if [[ ! -f "$NGINX_CONF_FILE" ]]; then
- echo -e "${RED}[ERROR]${RESET} Missing 'docker/etc/nginx/conf.d/default.conf' file."
- echo "Please ensure this file is present in the correct directory."
- exit 1
-else
- echo -e "${GREEN}[FOUND]${RESET} 'nginx' configuration file."
-fi
-echo
-
# Check for .env.docker
if [[ ! -f "docker/.env.docker" ]]; then
echo -e "${RED}[ERROR]${RESET} Missing 'docker/.env.docker' file."
@@ -167,19 +156,6 @@ else
fi
echo
-ESCAPED_APP_HOST=$(echo "$APP_HOST" | sed 's/\./\\./g') # Escape dots
-
-sed -i "s#valid_referers 127.0.0.1, .*;#valid_referers 127.0.0.1, ${ESCAPED_APP_HOST};#" "$NGINX_CONF_FILE"
-
-if [[ $? -ne 0 ]]; then
- echo -e "${RED}[ERROR]${RESET} Failed to update valid_referers in ${NGINX_CONF_FILE}."
- echo "Please check the Nginx config file format or script permissions."
- exit 1
-else
- echo -e "${GREEN}[SUCCESS]${RESET} Nginx valid_referers updated to include: ${APP_HOST}"
-fi
-echo
-
echo -e "${YELLOW}[STEP 3/5]${RESET} Stopping and cleaning up Existing mediaServer Docker containers..."
echo