4

I have Apache 2.2.15 configured with Mod_Proxy and Mod_SSL and serving CherryPy web application on RHEL 6.3. The only thing I'm struggling with is getting Apache to serve the website's static content (*.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 instance the path to my css file is the following...

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

Since I'm forcing the entire site to be in https when I navigate to

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

or

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

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

EDIT: it appears that Apache is still proxying /static... I found this redirect when accessing the /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"

anyone know why that is happening?

Thanks in advance!

Andrew

Andrew Kloos
  • 4,189
  • 4
  • 28
  • 36
  • 1
    Have you tried `ProxyPass /static/ !`? – j.w.r Sep 24 '12 at 04:47
  • Yes - with same result. I ended up using nginx. Got nginx configured pretty quickly... about 1 hr for ssl support and serving static content. However, still working to pass a nessus scan in order to be PCI compliant. Have a few errors remaining. – Andrew Kloos Oct 05 '12 at 14:03
  • @AndrewKloos Are you able to figure out the issue? If yes, can you please post the answer. – Abdul Azeez Aug 20 '13 at 06:21
  • @Abdul Azeez No I could not get apache to serve the static content. I ended up using nginx web server for my setup. If that interests you I could setup a tutorial for how I configured nginx with cherrypy. – Andrew Kloos Aug 20 '13 at 12:32
  • @AndrewKloos Thanks for the reply. I added Alias for the static location before proxypass and it worked fine. Alias /portal/images/ /var/www/portal/images/ – Abdul Azeez Aug 20 '13 at 12:45
  • @Abdul Azeez That's awesome! Unfortunately I don't have the setup to test your answer but if you give it as an answer I'll accept it. – Andrew Kloos Aug 20 '13 at 15:11

2 Answers2

2

Try adding an alias for your static folder before ProxyPass. It worked like champ for me,

Alias /static/ /var/www/html/mydomain/static/
Abdul Azeez
  • 1,073
  • 4
  • 13
  • 21
  • After adding Alias to my static folder, Apache started serving static files through http2 protocol. Before that only dynamic pages was served in http2 – Anton Belonovich Apr 10 '16 at 06:56
1

If the static files are under the "main" source folder, I'd suggest telling CherryPy to serve the specific folder(s) for stylesheets, images, js etc

Personally I generate the config and then use that config when mounting the application like:

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

An example of static file serving with CherryPy.

  • The /css is the path that you use from within your templates or HTML.
  • The os.path.abspath(os.path.join(os.path.dirname(__file__) generates a path pointing to your index file (or some other script if your not starting quickstart() from within the index)
  • The above path is then followed by 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'))
        }
    }
ellak
  • 2,531
  • 20
  • 26
  • 1
    I appreciate the answer but this doesn't answer my question. I'm trying to figure out why apache won't serve static content. – Andrew Kloos Dec 17 '12 at 17:21