OpenShift,与htaccess的蟒蛇2.7和静态文件蟒蛇、静态、文件、OpenShift

2023-09-02 20:32:17 作者:越界

我想配置Apache来提供静态文件,如 site.com/img/bla.jpg 网​​址。 Python的墨盒+瓶中。

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

我知道有一个preconfigured别名 WSG /静态目录中,所以我们可以使用 site.com/static/bla .JPG 。但我需要更多的静态目录。

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.

项目结构:

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

在我绑定的后端 mysite.com/img/<filename> 进行测试,如果Apache或后端处理文件 - 它返回的确定[文件名] 的字符串。

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

我曾在htaccess的以下的configs:

I have tried the following configs in htaccess:

1 : site.com/img/1.jpg - >确定1.JPG。

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://m.xsw88.com/allimgs/daicuo/20230902/1.png [L]

据我了解正则表达式不匹配,请求URL和Apache只是传递的东西到后端?

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

2 : site.com/img/1.jpg - >确定1.JPG。

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

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /img/1.jpg https://m.xsw88.com/allimgs/daicuo/20230902/1.png [L]

3 : site.com/img/1.jpg - >打开谷歌的i1_3d265689.png

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

RewriteEngine On
RewriteBase /
RewriteRule /img/1.jpg https://m.xsw88.com/allimgs/daicuo/20230902/1.png [L]

注:没有的RewriteCond

Note: no RewriteCond.

那么,如何使它工作?

我又希望Apache以服务 mysite.com/img/<filename> WSG / IMG /&LT;文件名&GT;

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

什么不对的openshift还是我错过了什么?

Is something wrong with openshift or I miss something?

推荐答案

看起来像我想通了。这个问题就解决了​​两个步骤:

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

所有静态文件必须放在 WSGI /静态如果你想Apache来为他们服务的,你(而不是你的后台脚本)。您不能配置Apache来使用另一个目录,既然你可以操纵只能用的.htaccess 文件至极不是允许配置这样的东西。因此,我不能让Apache从 WSGI / IMG 服务资产或 $ OPENSHIFT_DATA_DIR / IMG 。 我要创建符号链接到那些显示目录里面的 WSGI /静态这样的:

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

现在我们可以通过 site.com/static/img/1.jpg 获得的图像。

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

现在我们需要的地图 site.com/static/img/1.jpg site.com/img/1.jpg WSGI /的.htaccess 。是这样的:

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]

和它不会工作监守正则表达式的力量从URL路径的开头来搜索( ^ )。 问题是什么OpenShift的apache的URL路径是&LT; WSGI应用程序名称&gt; /%{REQUEST_URI} (至少重写规则)。 应用程序/ IMG /在我的处境1.JPG 。因此,不管是 ^ IMG /(.+)$ ^ / IMG /(.+)$将不匹配URL路径。我不是一个Apache的专家,但也许与模板配置此链接可以帮助一个人弄清楚URL路径的问题。 因此,解决方法是删除 ^

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]

现在我们可以使用 site.com/img/1.img site.com/randomstuff/img/1.img 将工作。所以我用的RewriteCond 过滤这样的URL。

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]