2015-06-25 94 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