【问题标题】:Apache reverse proxy with basic authentication具有基本身份验证的 Apache 反向代理
【发布时间】:2011-06-28 00:30:28
【问题描述】:

在将流量转发到我的后端服务器之前,尝试使用基本身份验证配置我的反向代理。谁能给我一个解决方案。

这里的例子:

User(internet) -> 反向代理/vhosts 服务器(这里需要添加基本认证)-> 后端服务器(未认证)

【问题讨论】:

    标签: apache authentication proxy reverse reverse-proxy


    【解决方案1】:

    您可以按照此处的说明进行操作:Authentication, AuthorizationAccess Control。反向代理的主要区别在于,您需要将身份验证内容放在 Location 块中,即使文档说它们只允许在 Directory 块中使用:

    <Location />
        AuthType Basic
        ...
    </Location>
    

    您可以在 Location 块之外放置您的代理命令,例如:

    ProxyPass / http://localhost:8080/
    

    【讨论】:

    • 作为记录,doc 确实表明它在这种情况下有效。 “目录 在此上下文中标记为有效的指令可以在服务器配置文件中的 容器内使用,但须遵守配置部分中概述的限制。”跨度>
    • 谢谢!我已经在这个 DigitalOcean 教程中链接到这个:digitalocean.com/community/tutorials/…
    • 对于像我这样的任何其他新手,_不要删除“”末尾的“/”,这是故意的,实际上是一个 URL 路径,而不是答案中的错字,如果你删除它,你会得到“位置指令需要额外的参数”。
    【解决方案2】:

    首先,检查你的apache2是否有utils包

    sudo apt-get install apache2-utils
    

    然后,设置用户名和密码。

    sudo htpasswd -c /etc/apache2/.htpasswd <username>
    

    之后,编辑您的反向代理以使用身份验证

    <VirtualHost *:80>
        ProxyPreserveHost On
    
        ProxyPass / http://someaddress:1234/
        ProxyPassReverse / http://someaddress:1234/
    
        Timeout 5400
        ProxyTimeout 5400
    
        ServerName dev.mydomain.com
        ServerAlias *.dev.mydomain.com
    
        <Proxy *>
            Order deny,allow
            Allow from all
            Authtype Basic
            Authname "Password Required"
            AuthUserFile /etc/apache2/.htpasswd
            Require valid-user
        </Proxy>
    </virtualhost>
    

    至少,更新你的 apache

    sudo service apache2 reload
    

    【讨论】:

    • 我就是这样做的,我知道它正在读取 .htpasswd 文件(如果我输入错误的文件名,或者删除它,apache 会抱怨)。但是,当我添加带有密码的用户时,用户名/密码将不起作用。知道为什么吗?
    • 这是最简单的解决方案,这就是我所遵循的。
    • @StefanHendriks - 带有 htpasswd 的“-c”开关创建文件。如果您在输入第一个用户后使用它,那么只有最后一个创建的用户才有正确的凭据。
    【解决方案3】:

    这是我用来通过 https 对数据库完成基本身份验证的配置。我的后端服务器正在运行 Tomcat,我使用 AJP 连接到它。好笑的端口号(4443)是因为标准端口(443)已经被使用了,我不想在同一个端口上配置多个https服务。

    <IfModule mod_ssl.c>
    NameVirtualHost *:4443
    <VirtualHost *:4443>
            ServerAdmin webmaster@localhost
            ServerName ws.myserver.se
            ServerAlias ws.myserveralias.se
            ErrorLog /var/log/apache2/ajpProxy.error.log
    
            # Possible values include: debug, info, notice, warn, error, crit,
            # alert, emerg.
            LogLevel info
    
            CustomLog /var/log/apache2/ajpProxy.log combined
    
            DBDriver mysql
            DBDParams "host=127.0.0.1 port=3306 user=proxyAuthUser pass=yourDbPasswordHere dbname=yourDbName"
            DBDMin  4
            DBDKeep 8
            DBDMax  20
            DBDExptime 300        
    
            <Proxy *>
                  # core authentication and mod_auth_basic configuration
                  # for mod_authn_dbd
                  AuthType Basic
                  AuthName "Backend auth name"
                  AuthBasicProvider dbd
    
                 # core authorization configuration
                  Require valid-user
    
                  # mod_authn_dbd SQL query to authenticate a user
                  AuthDBDUserPWQuery \
                    "SELECT password FROM user WHERE emailAddress = %s"
    
                  AddDefaultCharset Off
                  Order deny,allow
                  Allow from all
            </Proxy>
    
            ProxyPass / ajp://localhost:8009/
            ProxyPassReverse / ajp://localhost:8009/
    
            #   SSL Engine Switch:
            #   Enable/Disable SSL for this virtual host.
            SSLEngine on
    
            #   A self-signed (snakeoil) certificate can be created by installing
            #   the ssl-cert package. See
            #   /usr/share/doc/apache2.2-common/README.Debian.gz for more info.
            #   If both key and certificate are stored in the same file, only the
            #   SSLCertificateFile directive is needed.
            SSLCertificateFile    /etc/apache2/ssl/yourCertificateFile.crt
            SSLCertificateKeyFile /etc/apache2/ssl/yourPrivateKeyFile.key
            <FilesMatch "\.(cgi|shtml|phtml|php)$">
                    SSLOptions +StdEnvVars
            </FilesMatch>
            <Directory /usr/lib/cgi-bin>
                    SSLOptions +StdEnvVars
            </Directory>
    
            BrowserMatch "MSIE [2-6]" \
                    nokeepalive ssl-unclean-shutdown \
                    downgrade-1.0 force-response-1.0
            # MSIE 7 and newer should be able to use keepalive
            BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
    </VirtualHost>
    </IfModule>
    

    【讨论】:

      猜你喜欢
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      • 2021-08-21
      • 2015-11-05
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      • 2017-07-27
      相关资源
      最近更新 更多