ProxyPass将304状态修改为200并添加内容类型

时间:2018-06-13 15:45:52

标签: apache2 reverse-proxy mod-proxy proxypass

问题

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

在某些请求中,当直接访问时,节点服务器返回304状态代码而没有Content-Type。但是,通过反向代理访问时,状态代码更改为200,Content-Type添加值text/html。添加Content-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

2 个答案:

答案 0 :(得分:1)

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

  

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

class Channel extends React.Component {
  constructor(props) {
    super(props);
    this.onClick = this.onClick.bind(this);
  }
...
}

资源:Apache Core Features

答案 1 :(得分:0)

我仍在处理解决方案的其他部分,但您可以使用的解决方法在

之下
RequestHeader unset If-None-Match