Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve static files from a different directory than the static path?

I am trying this:

favicon_path = '/path/to/favicon.ico'  settings = {'debug': True,              'static_path': os.path.join(PATH, 'static')}  handlers = [(r'/', WebHandler),             (r'/favicon.ico', tornado.web.StaticFileHandler, {'path': favicon_path})]  application = tornado.web.Application(handlers, **settings) application.listen(port) tornado.ioloop.IOLoop.instance().start() 

But it keeps serving the favicon.ico that I have in my static_path (I have two different favicon.ico's in two separate paths, as indicated above, but I want to be able to override the one in the static_path).

like image 328
shino Avatar asked Apr 15 '12 20:04

shino


People also ask

Where do I serve static files?

Serve static files. Static files are stored within the project's web root directory. The default directory is {content root}/wwwroot , but it can be changed with the UseWebRoot method. For more information, see Content root and Web root.

Can .NET core HTTP pipeline be configured to server static files whose directory hierarchy reside outside of the web root?

ASP.NET Core application cannot serve static files by default. We must include Microsoft. AspNetCore. StaticFiles middleware in the request pipeline.

How does Nginx serve static files?

To serve static files with nginx, you should configure the path of your application's root directory and reference the HTML entry point as the index file. In this example, the root directory for the snake deployment is /home/futurestudio/apps/snake which contains all the files.


2 Answers

You need to wrap favicon.ico with parenthesis and escape the period in the regular expression. Your code will become

favicon_path = '/path/to/favicon.ico' # Actually the directory containing the favicon.ico file  settings = {     'debug': True,      'static_path': os.path.join(PATH, 'static')}  handlers = [     (r'/', WebHandler),     (r'/(favicon\.ico)', tornado.web.StaticFileHandler, {'path': favicon_path})]  application = tornado.web.Application(handlers, **settings) application.listen(port) tornado.ioloop.IOLoop.instance().start() 
like image 33
user1876508 Avatar answered Sep 19 '22 00:09

user1876508


Delete static_path from the app settings.

Then set your handler like:

handlers = [             (r'/(favicon\.ico)', tornado.web.StaticFileHandler, {'path': favicon_path_dir}),             (r'/static/(.*)', tornado.web.StaticFileHandler, {'path': static_path_dir}),             (r'/', WebHandler) ] 
like image 66
Not_a_Golfer Avatar answered Sep 23 '22 00:09

Not_a_Golfer