我想使用 Apache 的反向代理进行安装

亲爱的 @Teraterayuki

以下是用于将 Apache2 虚拟主机反向代理到 Discourse 容器中 Unix 域套接字的工作配置示例:

端口 80

<VirtualHost *:80>
        ServerName mysite.mydomain.com
        ServerAdmin webmaster@localhost
        ProxyPreserveHost On

        #ProxyPass / http://127.0.0.1:8888/
        #ProxyPassReverse / http://127.0.0.1:8888/

        # 可用的日志级别:trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg。
        # 也可以为特定模块配置日志级别,例如:
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/community_errors.log
        CustomLog ${APACHE_LOG_DIR}/community.log combined

        # 对于大多数来自 conf-available/ 的配置文件(这些文件在全局级别启用或禁用),
        # 可以为特定虚拟主机包含一行配置。例如,以下行仅为此主机启用 CGI 配置,
        # 前提是该配置已通过 "a2disconf" 全局禁用。
        #Include conf-available/serve-cgi-bin.conf
        ModPagespeed Off
        RewriteEngine on
        RewriteCond %{SERVER_NAME} =mysite.mydomain.com
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

端口 443

<IfModule mod_ssl.c>
<VirtualHost *:443>
        ServerName mysite.mydomain.com
        ServerAdmin webmaster@localhost
        #SSLProxyEngine on   #在反向代理上配置好 Let's Encrypt 后启用此项
  	    RewriteEngine On
        
        RewriteCond %{HTTP_USER_AGENT}  (Bytespider|Yandex|Wget|seocompany|CCBot|Cincraw) [NC]
        RewriteRule . - [R=403,L]

  	    ProxyPreserveHost On
  	    ProxyRequests Off
  	    RequestHeader set X-Forwarded-Proto expr=%{REQUEST_SCHEME}
 	    RequestHeader set X-Real-IP expr=%{REMOTE_ADDR}

        #ProxyPass / http://127.0.0.1:8888/
        #ProxyPassReverse / http://127.0.0.1:8888/
        ProxyPass / unix:/var/discourse/shared/socket-only/nginx.http.sock|http://localhost/
        ProxyPassReverse  / unix:/var/discourse/shared/socket-only/nginx.http.sock|http://localhost/

        # 可用的日志级别:trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg。
        # 也可以为特定模块配置日志级别,例如:
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/community_errors_ssl.log
        #CustomLog ${APACHE_LOG_DIR}/community_ssl.log combined

        # 对于大多数来自 conf-available/ 的配置文件(这些文件在全局级别启用或禁用),
        # 可以为特定虚拟主机包含一行配置。例如,以下行仅为此主机启用 CGI 配置,
        # 前提是该配置已通过 "a2disconf" 全局禁用。
        #Include conf-available/serve-cgi-bin.conf
        ModPagespeed Off
        SSLCertificateFile /etc/letsencrypt/live/mysite.mydomain.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/mysite.mydomain.com/privkey.pem
        Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

@Teraterayuki

请注意,在此配置中,代理使用的是 unix socket(Unix 套接字),而不是 web socketws)。

另请注意,在此配置中,您无需手动添加 Let’s Encrypt 信息。您可以从以下配置开始:

运行 Certbot 之前的端口 80

<VirtualHost *:80>
        ServerName mysite.mydomain.com
        ServerAdmin webmaster@localhost
        ProxyPreserveHost On

        #ProxyPass / http://127.0.0.1:8888/
        #ProxyPassReverse / http://127.0.0.1:8888/

        # 可用的日志级别:trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg。
        # 也可以为特定模块配置日志级别,例如:
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/community_errors.log
        CustomLog ${APACHE_LOG_DIR}/community.log combined

        # 对于大多数来自 conf-available/ 的配置文件(这些文件在全局级别启用或禁用),
        # 可以为特定虚拟主机包含一行配置。例如,以下行仅为此主机启用 CGI 配置,
        # 前提是该配置已通过 "a2disconf" 全局禁用。
        #Include conf-available/serve-cgi-bin.conf
        ModPagespeed Off
     
</VirtualHost>

运行 Certbot 之前的端口 443

<IfModule mod_ssl.c>
<VirtualHost *:443>
        ServerName mysite.mydomain.com
        ServerAdmin webmaster@localhost
        #SSLProxyEngine on   #在反向代理上配置好 Let's Encrypt 后启用此项
  	    RewriteEngine On
        


  	    ProxyPreserveHost On
  	    ProxyRequests Off
  	    RequestHeader set X-Forwarded-Proto expr=%{REQUEST_SCHEME}
 	    RequestHeader set X-Real-IP expr=%{REMOTE_ADDR}

        #ProxyPass / http://127.0.0.1:8888/
        #ProxyPassReverse / http://127.0.0.1:8888/
        ProxyPass / unix:/var/discourse/shared/socket-only/nginx.http.sock|http://localhost/
        ProxyPassReverse  / unix:/var/discourse/shared/socket-only/nginx.http.sock|http://localhost/

        # 可用的日志级别:trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg。
        # 也可以为特定模块配置日志级别,例如:
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/community_errors_ssl.log
        #CustomLog ${APACHE_LOG_DIR}/community_ssl.log combined

        # 对于大多数来自 conf-available/ 的配置文件(这些文件在全局级别启用或禁用),
        # 可以为特定虚拟主机包含一行配置。例如,以下行仅为此主机启用 CGI 配置,
        # 前提是该配置已通过 "a2disconf" 全局禁用。
        #Include conf-available/serve-cgi-bin.conf
        ModPagespeed Off

</VirtualHost>
</IfModule>

然后,如果您运行:

certbot -d mysite.mydomain.com

友好的 certbot 将自动为您添加所需的 SSL 代码。

当我们的朋友 certbot 添加了其配置文件代码且一切正常后,您可以取消注释以下行:

#SSLProxyEngine on   #在反向代理上配置好 Let's Encrypt 后启用此项

并再次重启 apache2。

希望这对您有帮助,@Teraterayuki

祝好。


结语:

  1. 请注意,在我们的 Apache2 反向代理配置中,我们不使用 haproxyhaproxy 会引入不必要的复杂性,而几乎没有任何实际好处(对我们而言)。因此,我们以“简单的方式”运行 Apache2 作为反向代理。如果您想使用 haproxy,结果可能因人而异(YMMV);但老实说,我们在多个生产环境的 Apache2 反向代理设置中从未使用过 haproxy,也从未遇到过 Apache2 作为反向代理的问题。
  1. 如果您未使用 mod_pagespeed,请注释掉相关行。但是,如果您正在运行 mod_pagespeed,请在反向代理到 Discourse 时按虚拟主机关闭 mod_pagespeed

祝一切顺利……希望这能在某些方面对您有所帮助。

6 个赞