2015-06-25 92 views
1

我是nginx的新手,我試圖讓我的second.domain.com顯示first.domain.com/dir的內容,(在端口3000上運行)我的本地主機)網上通緝後,似乎這纔是解決nginx不會將根目錄更改爲子目錄

# this one works fine 
server { 
    listen 80; 

    server_name first.domain.com; 

    location/{ 
     proxy_pass http://localhost:3000; 
     proxy_http_version 1.1; 
     proxy_set_header Upgrade $http_upgrade; 
     proxy_set_header Connection 'upgrade'; 
     proxy_set_header Host $host; 
     proxy_cache_bypass $http_upgrade; 
    } 
} 

# this one doesn't work as expected 
server { 
    listen 80; 

    server_name second.domain.com; 

    location/{ 
     proxy_pass http://localhost:3000; 
     proxy_http_version 1.1; 
     proxy_set_header Upgrade $http_upgrade; 
     proxy_set_header Connection 'upgrade'; 
     proxy_set_header Host $host; 
     proxy_cache_bypass $http_upgrade; 
     root /dir; 
     index index.html; 
    } 
} 

,但是當我訪問second.domain.com我得到相同的根first.domain.com而非first.domain.com/dir ...任何人都可以看到我做錯了什麼?

+0

您的位置設置自/至/博客,並在其中添加重寫規則一樣'改寫/blog/(.*)/ $ 1 break;' –

+0

如何在同一服務器conf中使用它們,如果它們具有不同的server_name? – Nick

+0

對不起老兄,我半睡半醒。刪除該部分。 :) –

回答

1

riffing關閉MIM的評論中,我得到它的工作這樣

# this one is now working as planned :) 
server { 
    listen 80; 

    server_name second.domain.com; 

    location/{ 

     # added mim's suggestiong here 
     rewrite /folder/(.*) /$1 break; 

     # then added the folder after the port 
     proxy_pass http://localhost:3000/folder; 

     proxy_http_version 1.1; 
     proxy_set_header Upgrade $http_upgrade; 
     proxy_set_header Connection 'upgrade'; 
     proxy_set_header Host $host; 
     proxy_cache_bypass $http_upgrade; 
     root /dir; 
     index index.html; 
    } 
} 
+0

thnx提醒@ObscureGeek,我最初沒有因爲stackoverflow阻止你接受你自己的答案,當你第一次發佈他們^ __ ^ – Nick