iT邦幫忙

0

請教nginx的二級域名轉址設定問題

afgn 2013-08-16 10:38:2213412 瀏覽

先聲明我不確定是不是『rewrite』或『重定向(redirect)』的技術。

我現在用 CodeIgniter + Nginx 開發網站, 最近有開發一些搜尋條件, 例如全台各縣市的美食搜尋。我想做到輸入 http://taipei.abc.tw 後可以去讀取 http://www.abc.tw/search/taipei 頁面, 我試過重定向(rewrite)但最後顯示的網址會是後者,設定如下:

server
{ listen 80;
server_name taipei.abc.tw;
#return 301 http://www.abc.tw/search/taipei;
rewrite ^ http://www.abc.tw/search/taipei permanent;
}

順便請教一下這應該叫什麼技術?

還是只能用轉址的方式來做? 或是只能用 iframe 來包??

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
6
逮丸逮丸
iT邦大師 1 級 ‧ 2013-08-18 22:07:11
最佳解答

我用你想要做出的概念,
實作成「偽線上字典」的網站,參考看看.:
要查詢某個字,用這個網址:
http://fish.dict.tagbible.net/

其實就跟這網址,是同一個AP跑的:
http://www.dict.tagbible.net/search/fish

在DNS上的主要設定是有關 wildcard:

<pre class="c" name="code">*.dict.tagbible.net.    IN      A     1.2.3.4

在 nginx 上的設定,基本上沒有用到 rewrite的方式,
主要是用 proxy_pass 的方式來做 reverse proxy。

而在 nginx 上其實只有設兩個 virtual host server
一個是固定名稱的 www.dict.tagbible.net
另個是接收所有 *.dict.tagbible.net 的 query。
我用的網頁程式是 ruby 的 sinatra 實作,
程式碼可參考 https://github.com/twtw/fake-dict

而在 nginx 上用這樣的設定:
<pre class="c" name="code">upstream dict {
server 127.0.0.1:3000;
#server unix:/tmp/dict.sock;
}

只用在 www.dict 的裡面,nginx裡的設定很單純:

&lt;pre class="c" name="code">server {
    server_name www.dict.tagbible.net;
    access_log /home/DICT/WWW/logs/access.log;
    error_log /home/DICT/WWW/logs/error.log;
    root /home/DICT/WWW/htdocs;

    location / {
      proxy_set_header  X-Real-IP  $remote_addr;
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      if (-f $request_filename/index.html) {
        rewrite (.*) $1/index.html break;
      }
      if (-f $request_filename.html) {
        rewrite (.*) $1.html break;
      }
      if (!-f $request_filename) {
        proxy_pass http://dict;
        break;
      }
    }
}

而在 *.dict 的 host 就靠nginx的設定,
再加上 $subdomain 的變數做為 search 的參數。

&lt;pre class="c" name="code">server {
    server_name ~^(?&lt;subdomain>.+)\.dict\.tagbible\.net;
    access_log /home/DICT/Default/logs/access.log;
    error_log /home/DICT/Default/logs/error.log;
    root /home/DICT/Default/public;

    location / {
      proxy_set_header  X-Real-IP  $remote_addr;
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      if (!-f $request_filename) {
         proxy_pass http://dict/search/$subdomain;
        break;
      }
    }
}

*.dict 的 root 裡需放 www.dict 裡的 js,css,img 裡的東西,
不然會漏掉一堆所需要的檔案,或者指到 www.dict 裡的 root 也可以。

當然此範例的缺點是 xxx.dict.tagbible.net 後面不管加什麼路徑,
通通都是把 www.dict.tagbible.net/search/xxx 吐出來的結果秀出,
如果裡面有 link 是只有相對路徑的話,
除非是在 root 所指的 static 檔案,例如像:
http://xxx.dict.tagbible.net/js/bootstrap.js
否則都是回應 http://www.dict.tagbible.net/search/xxx 的內容。

如果你只想只有幾個 term 像 taipei, tainan 特定名稱,
在 DNS 上就不設 wildcard 而要一個一個設;
而 nginx 也可用前面所講的方式設定。

afgn iT邦新手 5 級 ‧ 2013-08-20 10:04:31 檢舉

兜了一大圈, 其實我最後只用一個 vhost 的設定就搞定了

/usr/local/nginx/conf/vhost/taipei.abc.tw.conf

內容

server
{ listen 80;
server_name taipei.abc.tw;
proxy_redirect http://taipei.abc.tw/ /;
location / {
proxy_pass http://www.abc.tw/search/taipei;
}
}

不過還是謝謝大家的幫助 ^_^

小弟 蘇介吾 啟
http://fb.me/afgnsu
afgnsu@gmail.com
0921-380-997

4
uuger
iT邦新手 5 級 ‧ 2013-08-16 11:02:24

小弟不是很清楚這方面的技術

如果您要輸入taipei.abc.tw後 畫面顯示www.abc.tw/search/taipei的頁面

可以先新建全省的站台,並在每個站台內新增一個index,內容就崁入www.abc.tw/search/<城市名稱> 的頁面

例如 taipei.abc.te站台 index就崁入www.abc.tw/search/taipei的頁面

這樣可以達到您要的效果,但較麻煩且不是很完整的做法

大大可以參考看看或尋找相近似的方法 達到您要效果

uuger iT邦新手 5 級 ‧ 2013-08-16 11:04:06 檢舉

很像可以把所有城市的子網域對應到一個檔案,用讀取的方式解析,但這方面小弟就沒辦法仔細回答了

4

看起來做法像 URL Routing
請參考
[http://stackoverflow.com/questions/1316040/how-to-implement-url-routing-in-php-im-a-newcomer-to-know-this-concept-help-me
](http://stackoverflow.com/questions/1316040/how-to-implement-url-routing-in-php-im-a-newcomer-to-know-this-concept-help-me<br />
)

4
bizpro
iT邦大師 1 級 ‧ 2013-08-16 15:59:53

afgn提到:
#return 301 http://www.abc.tw/search/taipei;
rewrite ^ http://www.abc.tw/search/taipei permanent;

permanent就是301, 永久性重導向(permanent redirect), Google建議用301.
另外一個redirect, 也就是302, 臨時性重導向(temporarily redirect), 如
rewrite ^ http://www.abc.tw/search/taipei redirect;

2
我好弱
iT邦新手 4 級 ‧ 2013-08-18 13:08:52

http://wiki.nginx.org/HttpRewriteModule
http://jeffsebring.com/2012/nginx-301-redirects/

可以參考以上鏈結,我是用301去導的,可以成功。

我要發表回答

立即登入回答