【问题标题】:Nginx alias not workingNginx 别名不起作用
【发布时间】:2014-05-21 18:46:33
【问题描述】:

我有一个位置块,我为它定义了一个不同的别名,我希望它的index.php 文件是来自同一目录的服务器。问题是 Nginx 没有使用别名提供索引文件,而是试图在根目录中找到该文件(并且它不存在)。

配置:

http {
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 15;

    upstream php {
        server unix:/run/php-fpm.sock;
    }

    server {
        server_name my.domain.com;

        root /var/www;

        location / {
            alias /var/www/site/;
            index index.php;
            try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_pass php;
        }

        location ~ /\.ht {
            deny  all;
        }
    }
}

所以根应该指向根内的site/目录(/var/www),但试图提供的文件是/var/www/index.php:

2014/05/21 15:41:19 [error] 9591#0: *190 FastCGI sent in stderr: "Unable to open primary script: /var/www/index.php (No such file or directory)" while reading response header from upstream, client: 127.0.0.1, server: my.domain.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php-fpm.sock:", host: "localhost:666"

相反,我希望从/var/www/index.php 提供文件。

【问题讨论】:

  • 别名和root相同的原因是什么?另外,fastcgi_params 里有什么?
  • 对不起,它们不一样,我修好了。 fastcgi_params是gist.github.com/ranisalt/b0eb02dc07f8d23649d1
  • location / 中的alias 是什么原因? alias 只是位置的替代品,不是root。
  • 很多位置都有不同的根文件夹,我使用别名,因为其中一些与目录不匹配。

标签: php nginx alias


【解决方案1】:

Nginx locations 是独占的。一旦您离开位置,nginx 就会忘记所有上下文。在您的情况下,try_files 中的最后一项是内部重定向,因此 nginx 忘记了别名并转到与 location ~ \.php$ 匹配的 /index.php,其中 root /var/www 继承自服务器块。

我只需将root 指令更改为root /var/www/site,但如果您真的想坚持这种繁琐的方式,只需将您的try_files 更改为try_files $uri $uri/ /site/index.php?$args。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-21
    • 2011-09-21
    • 2012-12-17
    • 2015-01-28
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多