【问题标题】:nginx won't change root to sub directorynginx 不会将根目录更改为子目录
【发布时间】:2015-06-25 23:07:32
【问题描述】:

我是 nginx 新手,我正在尝试让我的 second.domain.com 显示 first.domain.com/dir 的内容,(在我的 localhost 的端口 3000 上运行)在网上查看后似乎是这样是解决办法

# 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 ...谁能看到我做错了什么?

【问题讨论】:

  • 将您的位置从 / 设置为 /blog 并在其中添加重写规则,例如 rewrite /blog/(.*) /$1 break;
  • 如果它们有不同的 server_name,我如何在同一个服务器配置中使用它们?
  • 对不起伙计,我睡着了。删除了那部分。 :)
  • 谢谢咪咪!下面的完整代码:)

标签: nginx path root subdirectory


【解决方案1】:

根据 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;
    }
 }

【讨论】:

  • thnx 提醒@ObscureGeek,我最初没有因为stackoverflow 阻止您在第一次发布时接受自己的答案^__^
猜你喜欢
  • 2011-08-24
  • 2015-03-09
  • 1970-01-01
  • 2017-05-13
  • 2017-09-17
  • 1970-01-01
  • 1970-01-01
  • 2018-04-29
  • 2019-11-17
相关资源
最近更新 更多