【问题标题】:ProxyPass modifies 304 status to 200 and adds content-typeProxyPass 修改 304 状态为 200 并添加 content-type
【发布时间】:2018-11-23 06:38:50
【问题描述】:

问题

我正在通过 Apache2 反向代理运行节点服务器 (sqlpad)。

在某些请求上,节点服务器在直接访问时会返回没有Content-Type 的304 状态码。但是,当通过反向代理访问时,状态码将更改为 200,并在 Content-Type 中添加值 text/htmlContent-Type 的添加导致应用无法加载 JS 和 CSS,因为 X-Content-Type-Options: nosniff 在标头中。

我真的不想更改节点服务器,因为它不是我的项目。我想用反向代理配置来解决这个问题。

如何配置 Apache2 ProxyPass 以转发原始 304 响应,或者至少不使用默认值填写缺少的 Content-Type

这是我的具体情况:

配置

# /etc/apache2/sites-enabled/xxxx.conf
...
<Location "/sqlpad">
  ProxyPass http://x.x.x.x:xxxx # IP address of Node server
  ProxyPassReverse http://x.x.x.x:xxxx # IP address of Node server
</Location>


# /etc/apache2/apache2.conf (comments removed)

ServerName xxxxx.com
DefaultRuntimeDir ${APACHE_RUN_DIR}
PidFile ${APACHE_PID_FILE}
Timeout 600
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>
<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>
<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
AccessFileName .htaccess
<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf

标题

原始请求/响应(无代理):

Request Url: http://xxxx/sqlpad/static/js/main.266789c5.js
Request Method: Get
Status: 304 Not Modified

Response Headers:

HTTP/1.1 304 Not Modified
X-DNS-Prefetch-Control: off
Strict-Transport-Security: max-age=15552000; includeSubDomains
X-Download-Options: noopen
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Referrer-Policy: same-origin
Accept-Ranges: bytes
Cache-Control: public, max-age=0
Last-Modified: Sat, 26 Oct 1985 08:15:00 GMT
ETag: W/"1d86fc-7438674ba0"
Date: Wed, 13 Jun 2018 15:04:20 GMT
Connection: keep-alive

Request Headers:

GET /sqlpad/static/js/main.266789c5.js HTTP/1.1
Host: xxxx
Connection: keep-alive
If-None-Match: W/"1d86fc-7438674ba0"
If-Modified-Since: Sat, 26 Oct 1985 08:15:00 GMT
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36
Accept: */*
Referer: http://localhost:56173/sqlpad/signin
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9

代理请求/响应:

Request Url: http://xxxx/sqlpad/static/js/main.266789c5.js
Request Method: Get
Status: 200 OK

Response Headers:

Connection: Keep-Alive
Content-Encoding: gzip
Content-Length: 496
Content-Type: text/html; charset=utf-8
Date: Wed, 13 Jun 2018 15:18:29 GMT
ETag: W/"320-Lp3a/E+wIigPW+CnI/Elyd7OYoA-gzip"
Keep-Alive: timeout=5, max=98
Referrer-Policy: same-origin
Server: Apache/2.4.33 (Ubuntu)
Strict-Transport-Security: max-age=15552000; includeSubDomains
Vary: Accept-Encoding
X-Content-Type-Options: nosniff
X-DNS-Prefetch-Control: off
X-Download-Options: noopen
X-XSS-Protection: 1; mode=block

Request Headers:

GET /sqlpad/static/js/main.266789c5.js HTTP/1.1
Host: xxxx
Connection: keep-alive
If-None-Match: W/"320-Lp3a/E+wIigPW+CnI/Elyd7OYoA-gzip"
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36
Accept: */*
Referer: http://xxxx/sqlpad
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Cookie: __zlcmid=xxxx; PHPSESSID=xxxx

我在 Ubuntu 16.04.4 上使用 Apache/2.4.33

【问题讨论】:

    标签: apache2 reverse-proxy mod-proxy proxypass


    【解决方案1】:

    在 mod_proxy 中无法确定内容类型的情况下,请考虑设置 DefaultContentType

    此指令已被禁用。为了向后兼容 配置文件,可以指定为none,意思是 没有默认媒体类型。例如:

    DefaultType None
    

    资源:Apache Core Features

    【讨论】:

    • 我在我的&lt;Location&gt; 标签中添加了这个,还添加了RequestHeader unset If-None-Match,但它不起作用。
    • 另外,sqlpad pad 遵循 express 的默认行为(用于服务网页的节点包)。 304 响应不应该有content-type,所以 sqlpad 做的是正确的事情。我很惊讶这个问题以前没有出现过。也许我遗漏了一些明显的东西。
    • @TomAranda 你是对的,这就是 304 的行为方式。
    【解决方案2】:

    我仍在研究解决方案的另一部分,但您可以使用的解决方法如下

    RequestHeader unset If-None-Match
    

    【讨论】:

    • 不幸的是,解决方法不起作用。它确实去掉了标头,但这并没有阻止反向代理将text/html Content-Type 添加到 JS 文件中。我将编辑问题以删除解决方法。
    • 请分享完整的 httpd.conf 并隐藏服务器详细信息。
    • @ABDmaverick:我已经发布了我的apache2.conf。虽然赏金已过期,但我很高兴为正确答案奖励 50 代表。
    • 在这些 -IncludeOptional mods-enabled/*.load IncludeOptional mods-enabled/*.conf 中发生了什么?
    • @TomAranda,请运行socat -v tcp-l:3001,reuseaddr,fork tcp:127.0.0.1:3000,假设3000 是您的nodejs 端口。接下来将您的 proxypass 更改为 3001 而不是 3000,现在您将能够看到 apache 发送给 NodeJS 的内容以及 NodeJS 回复的内容。请分享此调试日志
    猜你喜欢
    • 2011-08-13
    • 2018-03-23
    • 2013-10-28
    • 2012-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-12
    相关资源
    最近更新 更多