【问题标题】:why does nginx sets root folder to wrong location?为什么 nginx 将根文件夹设置为错误的位置?
【发布时间】:2016-01-18 20:35:11
【问题描述】:

我想在我的机器上安装两台服务器,一台专用,只能在 127.0.0.1 本地访问,另一台在 LAN 上可见(它的根文件夹是专用服务器的子文件夹)。所以我在sites-available 中创建了两个配置文件,并将它们链接到sites-enabled 文件夹。档案accessible

server {
        listen 80;

        root /usr/share/nginx/html/accessible/;
        index index.php index.html index.htm;

        server_name accessible;

        location / {
                try_files $uri $uri/ =404;
        }

        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html/accessible/;
        }

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
}

和文件localhost:

server {
        listen 127.0.0.1:80;
        listen [::1]:80;

        root /usr/share/nginx/html;
        index index.php index.html index.htm;

        server_name localhost;

        location / {
                try_files $uri $uri/ =404;
        }

        error_page 404 /404.html;

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;
        }

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
}

之后我更新了 /etc/hosts 以便将 http://accessible/ 转发到 127.0.0.1:127.0.0.1 accessible 是该行。

现在,当我尝试连接到 http://localhost/ 时,一切正常,我按预期得到了 /usr/share/nginx/html/index.html。但是当我尝试连接到http://accessible/ 时,会显示相同的文件/usr/share/nginx/html/index.html。这怎么可能?根文件夹显然已设置。

【问题讨论】:

    标签: nginx configuration


    【解决方案1】:

    问题是 localhost 服务器运行在与我为可访问而配置的 IP 相同的 IP 上,这显然是不可能的(或者至少我不知道如何)。 我已将可访问重定向到 127.0.0.2(将 /etc/hosts 中的行更改为 127.0.0.2 accessible),这是本地机器提供的另一个地址,并且在 nginx 配置文件 accessible 进行了微小更改后,允许所有 LAN IP 地址(allow 192.168.1.0/32; ) 一切正常(对于我在本地机器上和网络上的计算机)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-01
      • 2019-02-07
      • 2015-11-28
      • 2015-08-12
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      相关资源
      最近更新 更多