How to serve static content with Apache in AppFog

2019-03-29 11:52发布

I'm using AppFog PaaS system for a few days, and I love it, It's probably the best PaaS system that I've tested (I've used other 3 ones previously), but didn't find information about how to serve static content with the Web server in frontend (Apache https or nginx) I'm not sure what server is being used.

My app is a Python WSGI with CherryPy and works perfectly in AppFog but I don't wan't CherryPy to serve static content, I think that Apache httpd or nginx is a better option for that.

2条回答
做自己的国王
2楼-- · 2019-03-29 12:21

With Ryan's support, I'm finally able to load static files! Here are the steps:

  1. Created a 'static' directory in the project root - here all static files will be collected running the collectstatic command.

  2. Edit the settings.py file:

    STATIC_ROOT = os.path.join( os.path.abspath( os.path.dirname(file) ), '../static' ) # May change depending on where your settings.py file is!

    STATIC_URL = '/static/'

  3. Add following line in urlpatterns variable in urls.py file:

    url(r'^static/(?P.*)$', 'django.views.static.serve', { 'document_root': settings.STATIC_ROOT} ) ,

  4. Finally, run collectstatic command in your local machine. This will copy all static files from the apps you are using:

    python manage.py collectstatic

That's it. Push in AF :)

Downside: Need to run collectstatic every time we have a new static file...

查看更多
迷人小祖宗
3楼-- · 2019-03-29 12:25

Edit your nginx.conf file. In the server section enter...

   # serve static files
      location ~ ^/(images|javascript|css)/  {
      root    /var/www/html/appname;
    }

images, javascript and css would be folders in your document root folder. Update all your urls accordingly.

查看更多
登录 后发表回答