我正在尝试在 nginx 服务器上运行一个简单的重定向。我收到了 302 响应代码,但Location
标头未设置。我遗漏了什么?
我的最小案例(带有硬编码图像):
location /redirectme {
return 302 https://www.google.com/logos/doodles/2016/childrens-day-2016-brazil-5739205150900224-hp2x.jpg;
}
我的实际用例(将用户重定向到传递给查询参数的 URL url
):
location /redirectme {
return 302 $arg_url
}
我尝试的另一种配置(手动设置位置标头):
location /redirectme {
add_header Location $arg_url;
return 302;
}
这些都无法真正将客户端重定向到新 URL(仅返回 302)。我的其余 Web 应用程序运行良好,只是这个重定向部分不行。如果这有影响,我正在使用 ssl 块server {...}
。有什么想法吗?谢谢!
- - - 更新 - - -
这是整个server
街区...
server {
listen 443;
server_name myserver.com;
access_log /path-to/myserver.com-ssl.access.log;
ssl on;
ssl_certificate /path-to/myserver.com.crt;
ssl_certificate_key /path-to/myserver.com.key;
ssl_client_certificate /path-to/clientkey.pem;
ssl_verify_client on;
ssl_verify_depth 1;
keepalive_timeout 5;
root /srv/www/myserver/public/;
location /redirectme {
return 302 $arg_url;
}
location / {
try_files $uri/index.html $uri/index.htm @unicorn;
}
location @unicorn {
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-CLIENT-VERIFY $ssl_client_verify;
proxy_redirect off;
proxy_read_timeout 60;
proxy_send_timeout 60;
# If you don't find the filename in the static files
# Then request it from the unicorn server
if (!-f $request_filename) {
proxy_pass http://unicorn_myserver.com;
break;
}
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /srv/www/myserver/public/;
}
}
这样做的结果是curl -i
:
HTTP/1.1 302 Found
Content-Type: application/json
Content-Length: 0
Connection: keep-alive
Date: Fri, 14 Oct 2016 18:37:32 GMT
x-amzn-RequestId: 44e865b2-923d-11e6-a60a-5f6291eee9a8
X-Cache: Miss from cloudfront
Via: 1.1 aa89533ad2ec5e0edba466c9920bd000.cloudfront.net (CloudFront)
X-Amz-Cf-Id: 7dzQ9JQK380hFD-nFyJxm6mIWT5D4mWzvCbSAhDaa-pHwpF4oZHszw==
这也可能相关(我不明白如何,但也许),我让 Amazon API Gateway 位于我的请求前面,直接将它们代理到我的服务器(注意:配置ssl_client_certificate /path-to/clientkey.pem;
为仅接受来自 API Gateway 的请求)。