Failed to configure Apache for static content for CherryPy application. What am I doing wrong?

I have Apache 2.2.15 configured with Mod_Proxy and Mod_SSL and serving the CherryPy web application on RHEL 6.3. The only thing I'm struggling with is getting Apache to serve the static content of the website (* .js, * .css, * .jpg). Here is my VirtualHost entry ...

<VirtualHost mydomain.com:443> ServerAdmin support@mydomain.com ServerName mydomain.com ProxyPreserveHost On SSLProxyEngine On DocumentRoot /var/www/html/mydomain SSLEngine on SSLCertificateKeyFile /etc/ssl/myserver.key SSLCertificateFile /etc/ssl/mydomain_com.crt SSLCertificateChainFile /etc/ssl/mydomain_com.ca-bundle # this prevents the follow URL path from being proxied ProxyPass static/ ! # setup the proxy <Proxy *> Order allow,deny Allow from all </Proxy> ProxyPass / http://www.mydomain.com:8080/ ProxyPassReverse / http://www.mydomain.com:8080/ </VirtualHost> 

For example, the path to my css file is as follows:

 /var/www/html/mydomain/static/style.css 

Since I force the whole site to be in https when I go to

 https://www.mydomain.com/static/style.css 

or

 http://www.mydomain.com/static/style.css 

My browser informs me that the page was not found (404). Can anyone see what I'm doing wrong?

EDIT: It looks like Apache is still a proxy / static ... I found this redirect when accessing /static/style.css in my Apache access log ...

 xxx.xxx.xxx.xxx - - [17/Sep/2012:08:46:06 -0400] "GET /static/style.css HTTP/1.1" 301 337 "https://www.mydmain.com/Home/" "Mozilla/5.0 (X11; Linux x86_64; rv:10.0.7) Gecko/20120826 Firefox/10.0.7" 

Does anyone know why this is happening?

Thanks in advance!

Andrew

+4
source share
2 answers

Try adding an alias for your static folder before ProxyPass. For me it worked like a champion

 Alias /static/ /var/www/html/mydomain/static/ 
+2
source

If the static files are in the "main" source folder, I suggest passing CherryPy to work with specific folders for stylesheets, images, js, etc.

Personally, I generate the configuration, and then use this configuration when installing the application, for example:

 app = cherrypy.tree.mount(Root(), script_name="", config=somefile.get_config()) 

An example of a static file working with CherryPy.

  • /css is the path you use in your templates or HTML.
  • os.path.abspath(os.path.join(os.path.dirname(__file__) generates a path pointing to your index file (or some other script if you do not start quickstart () from the index)
  • Then the above path corresponds to the relative path to your static content folder

File:

 ##somefile def get_config(): return { '/': { 'tools.encode.on': True, 'tools.encode.encoding': 'UTF-8' }, '/css': { 'tools.staticdir.on': True, 'tools.staticdir.dir': os.path.abspath(os.path.join(os.path.dirname(__file__), 'relative/path/to/your/css/folder')) }, '/js': { 'tools.staticdir.on': True, 'tools.staticdir.dir': os.path.abspath(os.path.join(os.path.dirname(__file__), 'relative/path/to/your/js/folder')) } } 
+1
source

Source: https://habr.com/ru/post/1434222/


All Articles