【问题标题】:Redirection: mod_wsgi and django重定向:mod_wsgi 和 django
【发布时间】:2017-05-20 05:05:32
【问题描述】:

按照guide,我已经在 Debian 服务器上安装了带有 Apache 和 mod_wsgi 的 Mailman 3。

我的虚拟主机的 .conf 文件:

ErrorLog /var/log/mailman-web/mailman-web.log
CustomLog /var/log/mailman-web/mailman-web_access.log combined
WSGISocketPrefix run/wsgi
<VirtualHost *:80>
    ServerName XXXXXXX
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # Here, use the value of the STATIC_ROOT variable in your Django configuration file (production.py)
    Alias /robots.txt  /usr/local/mailman/mailman-bundler/var/mailman-web/static/hyperkitty/robots.txt
    Alias /favicon.ico /usr/local/mailman/mailman-bundler/var/mailman-web/static/hyperkitty/favicon.ico
    Alias /static      /usr/local/mailman/mailman-bundler/var/mailman-web/static

    <Directory "/usr/local/mailman/mailman-bundler/var/mailman-web/static">
        Order deny,allow
        Allow from all
        Require all granted
    </Directory>

    WSGIScriptAlias / /usr/local/mailman/mailman-bundler/bin/mailman-web.wsgi
    WSGIDaemonProcess mailman-web display-name=mailman-web maximum-requests=1000 processes=1 threads=1 python-path=/usr/local/mailman/venv/lib/python2.7/site-packages

        <Directory "/usr/local/mailman/mailman-bundler/bin">
            <Files mailman-web.wsgi>
                    Order deny,allow
                    Allow from all
                    Require all granted
            </Files>
            WSGIProcessGroup mailman-web
        </Directory>
</VirtualHost>

问题我在这个设置中遇到的问题是,当我转到 http://myhost/ 时,wsgi 脚本会将我的浏览器重定向到 http://myhost/archives。我希望将http://myhost/ 重定向到http://myhost/mailman3 而不是http://myhost/archives

为了找出 mailman 决定将存档子目录返回到哪里,我查看了 apache .conf 文件中定义的 wsgi 脚本,但没有发生更多事情,然后导入一些类并调用另一个脚本。按照这个脚本,我进入了文件'./eggs/Django-1.10.4-py2.7.egg/django/core/handlers/wsgi.py`,特别是这部分:

148 class WSGIHandler(base.BaseHandler):
149     request_class = WSGIRequest
150 
151     def __init__(self, *args, **kwargs):
152         super(WSGIHandler, self).__init__(*args, **kwargs)
153         self.load_middleware()
154 
155     def __call__(self, environ, start_response):
156         set_script_prefix(get_script_name(environ))
157         signals.request_started.send(sender=self.__class__, environ=environ)
158         try:
159             request = self.request_class(environ)
160         except UnicodeDecodeError:
161             logger.warning(
162                 'Bad Request (UnicodeDecodeError)',
163                 exc_info=sys.exc_info(),
164                 extra={
165                     'status_code': 400,
166                 }
167             )
168             response = http.HttpResponseBadRequest()
169         else:
170             response = self.get_response(request)
171 
172         response._handler_class = self.__class__
173 
174         status = '%d %s' % (response.status_code, response.reason_phrase)
175         response_headers = [(str(k), str(v)) for k, v in response.items()]
176         for c in response.cookies.values():
177             response_headers.append((str('Set-Cookie'), str(c.output(header=''))))
178         start_response(force_str(status), response_headers)
179         if getattr(response, 'file_to_stream', None) is not None and environ.get('wsgi.file_wrapper'):
180             response = environ['wsgi.file_wrapper'](response.file_to_stream)
181         return response

我猜返回的子目录的决定发生在这里的某个地方,但我可能是错的,不确定。

到目前为止我尝试了什么:在 apache conf 文件中,我添加了一行 Redirect permanent / http://myhost/mailman3 但这使得 apache 进入了一个以 http://myhost/mailman3mailman3mailman3.... 之类的 url 结尾的重定向循环

希望有人能帮我解决这个问题。

提前致谢!

【问题讨论】:

    标签: django apache redirect mod-wsgi mailman


    【解决方案1】:

    我猜是子目录的决定返回了

    这是一个 Django 项目,因此 url 与子目录无关,它们与 Django 中的“查看”功能相匹配。在这种情况下,“决定”发生在这里:

    https://gitlab.com/mailman/mailman-bundler/blob/master/mailman_web/urls.py

    urlpatterns = patterns('',
        url(r'^$',RedirectView.as_view(url=reverse_lazy('hyperkitty.views.index.index'))),
        url(r'^mailman3/', include('postorius.urls')),
        url(r'^archives/', include('hyperkitty.urls')),
        url(r'', include('social.apps.django_app.urls', namespace='social'), {"SSL": True}),
        url(r'', include('django_browserid.urls'), {"SSL": True}), 
    )
    

    您可以看到“root” url ("r'^$'") 被设置为重定向到 hyperkitty.views.index.index(hyperkitty 是归档器)

    如果您想让根 url 重定向到“/mailman3/”,则必须将第一行 url() 更改为

    url(r'^$',RedirectView.as_view(url="/mailman3/")),
    

    请注意,您必须在每次更新软件包时维护此分叉。

    注意:有“更好”(更安全/更清洁/更易于维护)的方法来做同样的事情(通过使用您自己的 django 设置模块而不是默认模块并将其设置为 ROOT_URL_CONF 指向您的所有者复制/粘贴这个urls.py 文件的/edited 版本,最后使用正确的reverse_lazy 而不是硬编码url),但是如果您对Django 不太了解,那么上述(有点Q&D)解决方案是迄今为止最简单的。

    【讨论】:

    • 确保记录您的修改,以便任何需要维护您的安装的人都知道它,并且可能需要时间来学习“更好的方法”(这意味着阅读 Django 的文档,了解如何将设置模块具体化到使用等),因为这将使您的设置更易于维护。
    猜你喜欢
    • 2014-11-26
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多