【问题标题】:Nginx double slash after domain rewrite域重写后的Nginx双斜杠
【发布时间】:2016-11-21 07:32:49
【问题描述】:

我在生产环境中使用 Nginx,我们在 uri 中有一些带有双斜杠的 url,如下所示:

http://foo.com//bar/foo

我想重写或重定向到:

http://foo.com/bar/foo

选项“merge_slashes”已经启用,我试过这个:

merge_slashes off;
rewrite (.*)//+(.*) $1/$2 permanent;

但不起作用,你知道吗?

谢谢

编辑:

所以,如果斜杠在第一个 "/" 之后,则重写工作,即:

http://pippo.it/foo//bar

但如果双斜杠在域后不起作用,即:

http://pippo.it//foo/bar

我尝试转储变量 $request_uri,结果如下:

url: http://pippo.it//foo/bar
expect: $request_uri -> //foo/bar
result: $request_uri -> /foo/bar

url: http://pippo.it///foo/bar
expect: $request_uri -> ///foo/bar
result: $request_uri -> //foo/bar

可能是一个错误?

编辑 2:

我发现了错误,问题是 elb (aws),如果我直接调用 ec2 实例(安装了 nginx)我可以看到前 2 个斜杠,但如果我调用 elb,负载均衡器删除第一个斜线,我在支持中心开了一个案例。

编辑 3:

发现问题:如果你用HTTP监听器设置elb,我不知道为什么但请求没有全斜线,如果你在TCP模式下设置监听器,请求工作正常。

谢谢

【问题讨论】:

  • 你改的是elb协议还是实例协议?

标签: linux ubuntu amazon-web-services nginx url-rewriting


【解决方案1】:

设置实际上是正确的。用 nginx/1.9.12 测试过

root@8317e542a878:/etc/nginx/sites-enabled# curl -I localhost//              
HTTP/1.1 301 Moved Permanently
Server: nginx/1.9.12
Date: Mon, 18 Jul 2016 20:34:29 GMT
Content-Type: text/html
Content-Length: 185
Location: http://localhost/
Connection: keep-alive

你是否在运行 nginx 时重新加载了设置?

nginx -s reload

【讨论】:

  • 我不知道为什么,但我有 nginx/1.10 但不起作用:` MillenniumFalcon-2:Desktop Gianmarco$ curl -I pippo.com//it/milano HTTP/1.1 200 OK Access-Control-Allow-来源:* Cache-Control: max-age=3660, public, s-maxage=600 Content-Type: text/html; charset=UTF-8 日期:2016 年 7 月 19 日星期二 13:46:15 GMT 服务器:nginx/1.10.0 Set-Cookie:Vary:Accept-Encoding Vary:Accept-Language Vary:Origin Connection:keep-alive`跨度>
  • 让我们看看完整的 nginx 配置?也许其他一些指令会制动逻辑。
  • 这是配置:listen 80 default_server; server_name pippo.it; root /var/www/pippo.it/current/web; location / { try_files $uri /app.php$is_args$args; } location ~ ^/app\.php(/|$) { fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_split_path_info ^(.+\.php)(/.+)$; include fastcgi_params; fastcgi_param REQUEST_URI $request_url; fastcgi_param SCRIPT_FILENAME $realpath_root$ fastcgi_script_name; fastcgi_param DOCUMENT_ROOT $realpath_root; fastcgi_param HTTPS on; fastcgi_buffering off; } }
【解决方案2】:

试试这个:

echo 'http://foo.com//bar/fool' | sed 's/\/\//\//g'

这是使用sed unix 命令搜索// 并将它们替换为/

【讨论】:

  • 你的表达式不起作用,正确的语法是这个 sed 's/\/\//\//g'
猜你喜欢
  • 1970-01-01
  • 2015-01-27
  • 1970-01-01
  • 2021-09-10
  • 1970-01-01
  • 2016-06-04
  • 1970-01-01
  • 1970-01-01
  • 2017-06-15
相关资源
最近更新 更多