由于一些糟糕的设计决策,我们的应用程序无法响应 HTTP HEAD 请求(返回“方法不允许”)。修改软件以正确返回 HEAD 请求会很棘手,虽然不是不可能,但需要额外的工作。该应用程序位于 NGINX 代理后面,我想知道是否有办法让 NGINX 将从客户端收到的 HEAD 请求转换为对后端的 GET 请求,然后丢弃除标头之外的响应并将其发送回客户端,就好像我们的应用程序服务器能够响应 HEAD 请求一样。
当前配置(相当标准)
upstream ourupstream{
server unix:/var/apps/sockets/ourapp.socket.thread1
server unix:/var/apps/sockets/ourapp.socket.thread2
server unix:/var/apps/sockets/ourapp.socket.thread3
[like 20 of these]
}
server {
listen 1.2.3.4:80;
server_name ourapp;
access_log /var/apps/logs/ourapp.nginx.plog proxy;
error_log /var/apps/logs/ourapp.nginx.elog info;
gzip on;
gzip_types text/plain text/html;
proxy_intercept_errors on;
proxy_connect_timeout 10;
proxy_send_timeout 10;
proxy_read_timeout 10;
proxy_next_upstream error timeout;
client_max_body_size 2m;
error_page 404 /static/404.html;
error_page 500 501 502 503 504 =500 /static/500.html;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://ourupstream/;
}
location /static/ {
root /var/apps/global/;
}
}
答案1
由于您正在使用 proxy_pass,我认为您必须使用一些 error_page 滥用(并且由于您已经在使用 error_pages,因此您还需要启用递归错误页面)。我认为这对您有用:
upstream ourupstream{
server unix:/var/apps/sockets/ourapp.socket.thread1
server unix:/var/apps/sockets/ourapp.socket.thread2
server unix:/var/apps/sockets/ourapp.socket.thread3
[like 20 of these]
}
server {
listen 1.2.3.4:80;
server_name ourapp;
access_log /var/apps/logs/ourapp.nginx.plog proxy;
error_log /var/apps/logs/ourapp.nginx.elog info;
gzip on;
gzip_types text/plain text/html;
proxy_intercept_errors on;
proxy_connect_timeout 10;
proxy_send_timeout 10;
proxy_read_timeout 10;
proxy_next_upstream error timeout;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 2m;
error_page 404 /static/404.html;
error_page 500 501 502 503 504 =500 /static/500.html;
location @force_get {
recursive_error_pages on;
proxy_method GET;
proxy_pass http://ourupstream;
}
location / {
error_page 550 = @force_get;
if ($request_method = HEAD) { return 550; }
proxy_pass http://ourupstream;
}
location /static/ {
root /var/apps/global/;
}
}