首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nginx配置位置

Nginx配置位置
EN

Stack Overflow用户
提问于 2015-10-12 10:16:09
回答 3查看 1K关注 0票数 1

我有以下Nginx配置文件..。

代码语言:javascript
复制
server {
    listen 80;
    server_name 127.0.0.1 localhost;
    location = /index.html {
            root /etc/nginx/html/app1;
            index index.html;
    }

    location / {
            root /etc/nginx/html/app1;
            index index.html;
    }

    location /common/ {
            root /etc/nginx/html/common;
    }
}

文件夹结构就像..。

html\app1

html\公共

当我试图浏览..。

http://localhost/ > Works

http://localhsot/index.html > Works

http://localhost/common/somefile.txt >不工作

我遗漏了什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-10-12 15:36:14

您应该使用alias而不是root

代码语言:javascript
复制
server {
  listen 80;
  server_name 127.0.0.1 localhost;

  location / {
    root /etc/nginx/html/app1;
    index index.html;
  }

  location /common {
    alias /etc/nginx/html/common;
  }
}

如果在root中使用common127.0.0.1/common/somefile.txt将尝试/etc/nginx/html/common/common/somefile.txt (注意两个common)。如果你检查nginx的日志,你就能看到它。

票数 2
EN

Stack Overflow用户

发布于 2015-10-12 12:58:48

我正在添加我自己的答案,因为我终于使它发挥作用。把它贴在这里,这样也许能帮到别人.

代码语言:javascript
复制
server {
listen 80;
server_name 127.0.0.1 localhost;
location = /index.html {
        root /etc/nginx/html/app1;
        index index.html;
}

location / {
        root /etc/nginx/html/app1;
        index index.html;
}

location ^~ /common/ {
        root /etc/nginx/html;
}
}

基本上,Nginx尝试的方式是/etc/nginx/html/公用/通用。从根目录中移除公共部分有效。还发现http://localhost:8888/common/需要有一个跟踪/。

票数 1
EN

Stack Overflow用户

发布于 2015-10-12 11:24:52

因为它首先与location /匹配。你可以这样做:

代码语言:javascript
复制
server {
    listen 80;
    server_name 127.0.0.1 localhost;
    location = /index.html {
            root /etc/nginx/html/app1;
            index index.html;
    }

    location / {
            root /etc/nginx/html/app1;
            index index.html;
    }

    location ^~ /common/ {
            root /etc/nginx/html/common;
    }
} 

编辑:是的。好像有点复杂。你可以这样做:

首先,您需要创建一个新服务器:

代码语言:javascript
复制
server {
    listen 80;
    server_name common.com;   # A virtual host
    root /etc/nginx/html/common;
}

然后,您需要修改上面的配置如下:

代码语言:javascript
复制
server {
    listen 80;
    server_name 127.0.0.1 localhost;
    location = /index.html {
            root /etc/nginx/html/app1;
            index index.html;
    }

    location / {
            root /etc/nginx/html/app1;
            index index.html;
    }

    location ^~ /common/ {
             rewrite ^/common(/.*)$  $1  break; # rewrite the /common/
             proxy_set_header Host common.com;  # it will requests common.com which the server of 127.0.0.1. then will match the above server.
             proxy_pass http://127.0.0.1;
    }
} 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33078633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档