Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Apache背后的扭曲Web-如何更正链接?_Python_Apache_Twisted_Reverse Proxy_Twisted.web - Fatal编程技术网

Python Apache背后的扭曲Web-如何更正链接?

Python Apache背后的扭曲Web-如何更正链接?,python,apache,twisted,reverse-proxy,twisted.web,Python,Apache,Twisted,Reverse Proxy,Twisted.web,我正在尝试使用TwistedFramework for python编写一个web应用程序。 我希望应用程序在作为独立服务器(alatwistd)运行或Apache反向代理到它时能够工作。例如 阿帕奇--> 在做了一些研究之后,我似乎需要使用vhost.VHostMonsterResource来完成这项工作。因此,我使用以下指令设置apache: ProxyPass /twisted https://localhost:8090/twisted/https/127.0.0.1:443 以下是我

我正在尝试使用TwistedFramework for python编写一个web应用程序。 我希望应用程序在作为独立服务器(alatwistd)运行或Apache反向代理到它时能够工作。例如

阿帕奇-->

在做了一些研究之后,我似乎需要使用vhost.VHostMonsterResource来完成这项工作。因此,我使用以下指令设置apache:

ProxyPass /twisted https://localhost:8090/twisted/https/127.0.0.1:443
以下是我的基本SSL服务器:

from twisted.web import server, resource, static
from twisted.internet import reactor
from twisted.application import service, internet
from twisted.internet.ssl import SSL
from twisted.web import vhost

import sys
import os.path
from textwrap import dedent

PORT = 8090
KEY_PATH = "/home/waldbiec/projects/python/twisted"
PATH = "/home/waldbiec/projects/python/twisted/static_files"

class Index(resource.Resource):
    def render_GET(self, request):
        html = dedent("""\
            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
            <html>
            <head>
                <title>Index</title>
            </head>
            <body>
                <h1>Index</h1>
                <ul>
                    <li><a href="/files/">Files</a></li>
                </ul>
            </body>
            </html>
            """)
        return html

class ServerContextFactory:
    def getContext(self):
        """
        Create an SSL context.

        Similar to twisted's echoserv_ssl example, except the private key
        and certificate are in separate files.
        """
        ctx = SSL.Context(SSL.SSLv23_METHOD)
        ctx.use_privatekey_file(os.path.join(KEY_PATH, 'serverkey.pem'))
        ctx.use_certificate_file(os.path.join(KEY_PATH, 'servercert.pem'))
        return ctx

class SSLService(internet.SSLServer):
    def __init__(self):
        root = resource.Resource()
        root.putChild("", Index())
        root.putChild("twisted", vhost.VHostMonsterResource())
        root.putChild("files", static.File(PATH))

        site = server.Site(root)
        internet.SSLServer.__init__(self, PORT, site, ServerContextFactory())

application = service.Application("SSLServer")
ssl_service = SSLService()
ssl_service.setServiceParent(application)
从twisted.web导入服务器,资源,静态
从twisted.internet导入
从twisted.application导入服务,internet
从twisted.internet.ssl导入ssl
从twisted.web导入vhost
导入系统
导入操作系统路径
从textwrap导入dedent
端口=8090
KEY_PATH=“/home/waldbiec/projects/python/twisted”
PATH=“/home/waldbiec/projects/python/twisted/static_文件”
类索引(resource.resource):
def render_GET(自我,请求):
html=dedent(“”)\
指数
指数
""") 返回html 类ServerContextFactory: def getContext(self): """ 创建SSL上下文。 与twisted的echoserv_ssl示例类似,除了私钥 和证书在不同的文件中。 """ ctx=SSL.Context(SSL.SSLv23_方法) 使用私有密钥文件(os.path.join(密钥路径'serverkey.pem')) 使用证书文件(os.path.join(KEY\u path,'servercert.pem')) 返回ctx SSLService类(internet.SSLServer): 定义初始化(自): root=resource.resource() root.putChild(“,Index()) root.putChild(“twisted”,vhost.VHostMonsterResource()) root.putChild(“文件”,static.File(路径)) 站点=服务器。站点(根) internet.SSLServer.\uuuu init\uuuuuu(self、PORT、site、ServerContextFactory()) 应用程序=服务。应用程序(“SSLServer”) ssl_服务=SSLService() ssl_service.setServiceParent(应用程序)
它几乎可以工作——但索引页上的“文件”链接在使用apache作为反向代理时的行为与我希望的不同,因为它是一个绝对链接

我的主要问题是,除了使用相对链接之外,是否有某种方法可以计算链接的完整URL路径应该是什么,从而使链接仍然在独立服务器模式下工作?
第二个问题是,我是否正确使用vhostmonster资源?我没有找到太多文档,我从网上找到的示例中拼凑了代码。

配置Twisted应用程序,使其知道自己的根位置。它可以使用这些信息正确地生成URL。

这似乎太多工作了。为什么要使用虚拟资源呢?你可能有非常具体的理由想要这样做,但是……大多数时候:

  • 让apache处理ssl。然后,apache会转到您的twisted应用程序,将非SSL商品返回apache。网络上所有关于apache配置的文档

  • 如果确实需要,可以在ssl端口上添加另一台服务器

尚未测试,但结构更像:

root = resource.Resource()
root.putChild("", Index())
root.putChild("files", static.File(PATH))

http = internet.TCPServer(8090, server.Site(root))
# change this port # to 443 if no apache
https= internet.SSLServer(8443, server.Site(root), ServerContextFactory())

application = service.Application("http_https_Server")
http.setServiceParent(application)
https.setServiceParent(application)
开发提示: 在开发过程中,您可以添加一个ssl服务器,这样您就可以通过ssh连接到正在运行的web_服务器中,并检查变量和其他状态。太酷了

ssl = internet.TCPServer(8022, getManholeFactory(globals(), waldbiec ='some non-system waldbiec passwork')) 
ssl.setServiceParent(application)

因此,在深入研究vhost.VHostMonsterResource源之后,我决定可以创建另一个资源,让反向代理URL前缀由Apache ProxyPass URL中的附加标记指定

首先,我最终发现vhost.VHostMonsterResource应该是后端网站中的一个特殊URL,它根据URL路径中编码的数据计算出反向代理主机和端口。URL路径(SAN方案和网络位置)如下所示:

/$PATH_TO_VHMONST_RES/$REV_PROXY_SCHEME/$REV_PROXY_NETLOC/real/url/components/

$PATH_TO_VHMONST : Path in the (internal) twisted site that corresponds to the VHostMonsterResource resource.
$REV_PROXY_SCHEME : http or https that is being used by the reverse proxy (Apache).
$REV_PROXY_NETLOC : The net location (host and port) or the reverse proxy (Apache).
因此,您可以通过在URL中编码此信息,从反向代理控制配置。结果是twisted站点将理解来自反向代理的HTTP请求

但是,如果您按照我最初的示例代理外部站点的子树,则此信息将丢失。所以我的解决方案是创建一个额外的资源来解码额外的路径信息。新的代理URL路径变为:

/$PATH_TO_MANGLE_RES/$REV_PROXY_PATH_PREFIX/$VHOSTMONST_MARKER/$REV_PROXY_SCHEME/$REV_PROXY_NETLOC/real/url/components/

$PATH_TO_MANGLE_RES : The path to the resource that decodes the reverse proxy path info.
$REV_PROXY_PATH_PREFIX : The subtree prefix of the reverse proxy.
$VHOSTMONST_MARKER : A path component (e.g. "vhost") that signals a VHostMonster Resource should be used to further decode the path.

这些url是如何寻址的?通过多种方式,主要是通过消除
VHostMosterResource
if消除OP必须经历的一些url混乱。在我的示例中,我甚至删除了
/twisted
路径,尽管它可以毫无伤害地放回。
https
的虚拟主机从来没有真正意义,因为443或任何端口上只能提供一个域名。
VHostMonsterResource
不是问题的原因。必须生成任何URL才是问题的原因。抛开SSL和vHostmonster不谈,如果apache从某个非根路径代理,这实际上解决了问题。在我最初的示例中,如果apache代理从内部服务器上的/twisted到/on,那么“/files/”的完整路径是否仍然会变成“/files/”而不是apache端的“/twisted/files/”呢?我考虑过创建一个配置文件/设置,其前缀类似于WEBROOT\u,但它不允许应用程序同时在本地和代理模式下正常运行。我不确定在我最初的帖子中是否已经清楚地说明了这一点。我希望能够浏览到apache地址或内部地址,并且仍然让应用程序工作,而无需每次重新配置应用程序。我不确定我是否正确理解了您的建议,或者它是否不能解释我试图描述的场景。这并不是我不想要配置。我想做的是能够访问