OpenShift, python 2.7 and static files with htacce

2020-06-24 08:14发布

I'm trying to configure apache to serve static files for URLs like site.com/img/bla.jpg. Python cartridge + flask.

I know what there is an preconfigured alias for wsg/static directory, so we can use site.com/static/bla.jpg. But I need additional static directory.

Project structure:

/wsgi
  .htaccess
  /img -> $OPENSHIFT_DATA_DIR/some/path (soft link)
  /static
  application

In the backend I binded mysite.com/img/<filename> for testing if apache or backend handles files - it returns "ok [filename]" string.

I have tried the following configs in htaccess:

1: site.com/img/1.jpg -> "ok 1.jpg"

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f  # with and without this condition
RewriteRule ^img/(.+)$ /static/$1 [L]
# RewriteRule ^img/(.+)$ /img/$1 [L]
# RewriteRule ^/img/(.+)$ /img/$1 [L]
# RewriteRule ^img/(.+)$ https://ssl.gstatic.com/gb/images/i1_3d265689.png [L]

As I understand regex doesn't match request URL and apache just passes stuff to backend?

2: site.com/img/1.jpg -> "ok 1.jpg"

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /img/1.jpg https://ssl.gstatic.com/gb/images/i1_3d265689.png [L]

3: site.com/img/1.jpg -> opens google's i1_3d265689.png

RewriteEngine On
RewriteBase /
RewriteRule /img/1.jpg https://ssl.gstatic.com/gb/images/i1_3d265689.png [L]

Note: no RewriteCond.

So how to make it work?

Again I want apache to serve mysite.com/img/<filename> as wsg/img/<filename>.

Is something wrong with openshift or I miss something?

1条回答
戒情不戒烟
2楼-- · 2020-06-24 08:35

Looks like I figured out. The issue is solved in two steps:

  1. All static files must be placed into wsgi/static if you want apache to serve them for you (and not your backend script). You can't configure apache to use another directory for that since you can manipulate only with .htaccess file wich is not allows to configure such stuff. So I can't make apache serve assets from wsgi/img or $OPENSHIFT_DATA_DIR/img. I have to create symlinks to those dirs inside wsgi/static like:

    /wsgi
        /static
            /img -> (symlink) $OPENSHIFT_DATA_DIR/img
    

    Now we can access to images via site.com/static/img/1.jpg.

  2. Now we need map site.com/static/img/1.jpg to site.com/img/1.jpg in wsgi/.htaccess. Something like:

    RewriteRule ^img/(.+)$ /static/img/$1 [L]
    or
    RewriteRule ^/img/(.+)$ /static/img/$1 [L]
    

    And it will not work becuase regexp forces to search from the beginning of URL-path (^). Problem is what OpenShift's apache URL-path is <wsgi app name>/%{REQUEST_URI} (at least for RewriteRule). application/img/1.jpg in my situation. So neither ^img/(.+)$ nor ^/img/(.+)$ will not match the URL-path. I'm not an apache expert but maybe this link with template config may help someone to figure out URL-path issue. So solution is to remove ^:

    RewriteRule /img/(.+)$ /static/img/$1 [L]
    

    Now we can use site.com/img/1.img but also site.com/randomstuff/img/1.img will work. So I use RewriteCond to filter such URLs.

Here is my final solution:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/img/
RewriteRule /img/(.+)$ /static/img/$1 [L]
查看更多
登录 后发表回答