如何配置 nginx 重寫規則以剝離單頁 JS 應用程式的索引頁​​並將非哈希 URL 重定向到哈希 URL?

如何配置 nginx 重寫規則以剝離單頁 JS 應用程式的索引頁​​並將非哈希 URL 重定向到哈希 URL?

我是一名編寫單頁 JS 應用程式的軟體開發人員。我絕對不是系統管理員,所以請耐心等待。

我想要實現的是,只要使用者使用兩種可能常見/不需要的 URL 模式之一造訪我的網站,就會發生重新導向。

  1. mysite.com/index.html -> 重定向到 mysite.com
  2. mysite.com/path/they/remember -> 重定向到雜湊版本:mysite.com/#/path/they/remember

我嘗試了各種方法,但總是遇到重定向循環。這是我目前的配置:

注意:您會注意到 /api 還需要透過輕量級 oauth 代理程式在內部重定向所有請求,因此無論我使用什麼解決方案都無法中斷此重定向。

server {

# Redirect all requests through /api/ to apiproxy.php
location /api {
    rewrite ^/api/?(.*)$ /apiproxy.php/$1 last;
}

location / {
    index index.html
}

# use case #1 (combined with use case #2 optionally, a url such as mysite.com/index.html/path/they/remember)
location ~ /index.html/?(.*) {
    return 301 http://mysite.com\#/$1;
}

location ~ /(.*) {
    return 301 http://mysite.com\#/$1;
}

這個設定給了我重定向循環。有誰有任何建議或知道如何完成我正在嘗試以 nginx 方式完成的任務?

答案1

首先,增加日誌等級並檢查日誌。

你最後一個位置區塊似乎是假的。

而且,return 301 http://mysite.com\#/$1;似乎不合法;你想做什麼嗎 return 301 http://mysite.com/\#/$1/;

你需要逃跑嗎#

相關內容