jitesoft/docker-lighttpd

How to make SPA compatible?

Closed this issue · 6 comments

Although i am running Lighttpd on my dietpi server i am not that experienced with it. And even after Googling i did not found out how i can basically have something like https://github.com/SocialEngine/docker-nginx-spa just with your lovely alpine+lighttpd combo. I am refering to the fact that everything it routed to the root path so that i can just deploy my svelte SPA app with a index.html and still access mydomain.com/sub/route which should serve the index.html and not tryint to access a /sub/route folder.

I am grateful for any help here, as i think this package is better suited for small containers instead of the bigger nginx combo.

Hi!

You should be able to just mount (or copy) your application into the web-root of the lighty image and run it.
Basically:

FROM ghcr.io/jitesoft/lighttpd:latest

COPY ./ /var/www/html

I'm guessing that the issue you encounter is a configuration issue, where you actually have subfolders in your application which are named similar or the same as the actual routes?
Due to the nature of the webserver, we would want it to return stuff like images, css and javascript as well and not route it through the index file, so making a general rule where it only hits the index file is a bit hard.

If I recall correctly, it is possible to lock lighttpd do only serve one file, but I have never tried that myself, and it would require you to serve your assets from another server.

If you wish to set up rewrite rules for specific routes, lighttpd have a rewrite module which could be used. It have one piece you might want to take a closer look at if it's directories that is the issue:

https://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModRewrite#urlrewrite-repeat-if-not-file

Can basically be used as:

url.rewrite-if-not-file = ( "" => "/index.html${url.path}${qsa}" )

ok i think i have it working now

"FROM jitesoft/lighttpd:latest", 
"COPY ./public /var/www/html", 
"RUN rm /etc/lighttpd/conf.d/000-mime.conf", 
"COPY ./lighttpd.conf /etc/lighttpd/conf.d/000-default.conf", 
"RUN chmod -R 777 /var/www/html"

lighttpd.conf

server.modules += ("mod_access", "mod_accesslog", "mod_rewrite", "mod_auth", "mod_expire")
server.document-root = env.SERVER_ROOT
server.port = env.PORT
server.name = env.SERVER_NAME
server.username  = "www-data"
server.groupname = "www-data"
server.indexfiles = ("index.html")
server.max-fds = env.MAX_FDS
static-file.exclude-extensions = (".php", ".pl", ".cgi", ".fcgi",".env")
url.access-deny = ("~", ".inc")
$HTTP["url"] =~ "\/\..*" {
	url.access-deny = ( "" )
}
server.follow-symlink = "enable"
dir-listing.activate = "disable"
url.rewrite-once = (
  "^/(.+)[\.](css|js|html|gif|png|jpg|webp|avif|br|svg|woff|woff2|eof|otf|pdf).*$" => "", #pass static files through
  "^/(.*)$" => "/index.html${url.path}${qsa}" #rewrite the rest
)
# url.rewrite-if-not-file = ( "" => "/" )
accesslog.filename = "/dev/stdout"

expire.mimetypes = (
	"text/plain" => "access plus 30 days",
	"text/css" => "access plus 30 days",
	"text/xml" => "access plus 30 days",
	"image/" => "access plus 30 days",
	"font/" => "access plus 30 days",
	"application/" => "access plus 30 days",
	"audio/" => "access plus 30 days",
	"video/" => "access plus 30 days"
)
mimetype.assign = (
  ".pdf"          =>      "application/pdf",
  ".sig"          =>      "application/pgp-signature",
  ".spl"          =>      "application/futuresplash",
  ".class"        =>      "application/octet-stream",
  ".ps"           =>      "application/postscript",
  ".torrent"      =>      "application/x-bittorrent",
  ".dvi"          =>      "application/x-dvi",
  ".gz"           =>      "application/x-gzip",
  ".pac"          =>      "application/x-ns-proxy-autoconfig",
  ".swf"          =>      "application/x-shockwave-flash",
  ".tar.gz"       =>      "application/x-tgz",
  ".tgz"          =>      "application/x-tgz",
  ".tar"          =>      "application/x-tar",
  ".zip"          =>      "application/zip",
  ".mp3"          =>      "audio/mpeg",
  ".m3u"          =>      "audio/x-mpegurl",
  ".wma"          =>      "audio/x-ms-wma",
  ".wax"          =>      "audio/x-ms-wax",
  ".ogg"          =>      "application/ogg",
  ".wav"          =>      "audio/x-wav",
  ".gif"          =>      "image/gif",
  ".jpg"          =>      "image/jpeg",
  ".jpeg"         =>      "image/jpeg",
  ".png"          =>      "image/png",
  ".xbm"          =>      "image/x-xbitmap",
  ".xpm"          =>      "image/x-xpixmap",
  ".xwd"          =>      "image/x-xwindowdump",
  ".css"          =>      "text/css",
  ".html"         =>      "text/html",
  ".htm"          =>      "text/html",
  ".js"           =>      "text/javascript",
  ".asc"          =>      "text/plain",
  ".c"            =>      "text/plain",
  ".cpp"          =>      "text/plain",
  ".log"          =>      "text/plain",
  ".conf"         =>      "text/plain",
  ".text"         =>      "text/plain",
  ".txt"          =>      "text/plain",
  ".spec"         =>      "text/plain",
  ".dtd"          =>      "text/xml",
  ".xml"          =>      "text/xml",
  ".mpeg"         =>      "video/mpeg",
  ".mpg"          =>      "video/mpeg",
  ".mov"          =>      "video/quicktime",
  ".qt"           =>      "video/quicktime",
  ".avi"          =>      "video/x-msvideo",
  ".asf"          =>      "video/x-ms-asf",
  ".asx"          =>      "video/x-ms-asf",
  ".wmv"          =>      "video/x-ms-wmv",
  ".bz2"          =>      "application/x-bzip",
  ".tbz"          =>      "application/x-bzip-compressed-tar",
  ".tar.bz2"      =>      "application/x-bzip-compressed-tar",
  ".odt"          =>      "application/vnd.oasis.opendocument.text",
  ".ods"          =>      "application/vnd.oasis.opendocument.spreadsheet",
  ".odp"          =>      "application/vnd.oasis.opendocument.presentation",
  ".odg"          =>      "application/vnd.oasis.opendocument.graphics",
  ".odc"          =>      "application/vnd.oasis.opendocument.chart",
  ".odf"          =>      "application/vnd.oasis.opendocument.formula",
  ".odi"          =>      "application/vnd.oasis.opendocument.image",
  ".odm"          =>      "application/vnd.oasis.opendocument.text-master",
  ".ott"          =>      "application/vnd.oasis.opendocument.text-template",
  ".ots"          =>      "application/vnd.oasis.opendocument.spreadsheet-template",
  ".otp"          =>      "application/vnd.oasis.opendocument.presentation-template",
  ".otg"          =>      "application/vnd.oasis.opendocument.graphics-template",
  ".otc"          =>      "application/vnd.oasis.opendocument.chart-template",
  ".oti"          =>      "application/vnd.oasis.opendocument.image-template",
  ".oth"          =>      "application/vnd.oasis.opendocument.text-web",
  ".svg"          =>      "image/svg+xml",
  ".svgz"         =>      "image/svg+xml",
  ".ttf" => "font/ttf",
  ".otf" => "font/otf",
  ".eot" => "application/vnd.ms-fontobject",
  ".woff"=> "font/font-woff",
  ".woff2"=>"font/font-woff2",

# make the default mime type application/octet-stream.
  ""              =>      "application/octet-stream",
)

strangely i don't get any logs in my caprover view, but that is too offtopic i think. thank you !

Np at all and happy you got it working!

It's possible that the access-log is not piped to stdout as the access-log is off by default, you can try set the accesslog.filename parameter to stdout, but I can't promise it's only that which is required:

accesslog.filename = "/dev/stdout"

Any errors should be logged to stderr if I remember correctly, else it should be possible to do that as well:

server.errorlog = "/dev/stderr"

mhm unforunately that did not help(i always only see the starting log). but appreciate you trying to help.
grafik

Been debugging this issue this morning. It seems like it's a permission issue that is the root of the problem.
The user which runs the lighttpd process (www-data) is not able to output to /proc/1/fd/1 as it's owned by root, and changing the permission to output to it might be a bit of a security issue.

Trying to figure out the best way to handle this and will keep the issue open until it's solved.

Thank you!

Closing in favor of #5 as the initial issue is solved here.