Nginx将http重定向到https并使用一个重定向删除尾部斜杠

时间:2017-05-11 09:48:26

标签: redirect nginx url-rewriting

我想将http重定向到https并使用一次重定向删除nginx中的尾部斜杠。我今天的解决方案如下:

server {
    listen 80;
    server_name www.example.com
    rewrite ^/(.*)/$ /$1 permanent;
    return 301 https://$host$request_uri;
}

此解决方案的问题在于它将提供两个重定向

提供两个重定向:

http://www.example.com/test/ --> http://www.example.com/test
http://www.example.com/test --> https://www.example.com/test

是否可以制定解决方案,只能获得一个像bellow一样的重定向?

http://www.example.com/test/ --> https://www.example.com/test

当我查看nginx重写和返回方法的文档时,我觉得应该可以通过一次重写来实现:

rewrite ^/(.*)/$ https://$host$request_uri permanent;

但是我没有尝试过给我正确的结果。

1 个答案:

答案 0 :(得分:2)

您已经拥有了正确解决方案的组件。使用方案和主机名以及捕获来构建目标URL:

breed [nodes node]
breed [walkers walker]

walkers-own [location path]  
nodes-own [ target? visited? ] 

to setup
  clear-all
  set-default-shape nodes "circle"
  create-nodes 30 [ 
    set color blue 
    set target? false
    set visited? false
  ]
  ask nodes [ create-link-with one-of other nodes ]
  repeat 500 [ layout ]
  ask nodes [ 
    setxy 0.95 * xcor 0.95 * ycor 
  ]
  ask n-of 5 nodes [
    set target? true
    set color white
  ]

  create-walkers 1 [
    set color red
    set location one-of nodes
    move-to location
    set path (list location path)
  ]
  reset-ticks
end

to layout
  layout-spring nodes links 0.5 2 1
end

to go
  ask links [ set thickness 0 ]
  ask walkers [
    let new-location one-of [link-neighbors] of location
    move-to new-location
    set location new-location
    set path lput location
    ;; This gets turtles to ask their current location 
    ;; to set visited and target to true.
    ask location [
      set visited? true
      if target? = true [
        set color red
      ]
    ]
  ]

  ;; Check for target nodes that have NOT been visited. 
  ;; If there aren't any, stop the model.
  if not any? nodes with [ target? = true and visited? = false ] [
   print ("All target nodes have been visited.")
   stop
  ] 
  tick
end