【问题标题】:Nginx Yii2 configuration in different foldersNginx Yii2 在不同文件夹中的配置
【发布时间】:2017-06-29 18:48:30
【问题描述】:

我在为 yii2 基本应用配置 nginx 服务器时遇到了问题。

这是我的服务块文件:

server {
    listen       80 ;

    access_log /var/log/nginx/access-server.log;
    error_log /var/log/nginx/error-server.log;

    charset utf-8;

    location  /fetch {
            root /usr/share/nginx/html/another_folder/web/;
            try_files $uri $uri/ /index.php$is_args$args;
    }

         location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

}

我的项目位于另一个文件夹“another_folder”中。 我希望当用户访问 url 时:http://ip/fetch files nginx 将提供来自另一个文件夹的文件。

我的错误日志文件返回给我:

2017/02/11 12:38:52 [error] 4242#0: *12 FastCGI sent in stderr: "Unable to open primary script: /usr/share/nginx/html/index.php (No such file or directory)" while reading response header from upstream

兄弟显示: 没有指定输入文件。

你能帮我解决这个问题吗?

谢谢!

【问题讨论】:

  • 您希望/fetch/index.php 位于/usr/share/nginx/html/another_folder/web/index.php 还是/usr/share/nginx/html/another_folder/web/fetch/index.php
  • 在这个文件夹中:/usr/share/nginx/html/another_folder/web/index.php
  • 您需要使用alias 指令。见this answer
  • 谢谢。它解决了问题。但它无法在 yii2 中使用漂亮的 Url 找到 url。它再次在 /usr/share/nginx/html 文件夹中查找 index.php 文件
  • 这是在another_folder 根目录中吗?漂亮的 URI 会以 /fetch 为前缀吗?

标签: nginx configuration-files yii2-basic-app nginx-location


【解决方案1】:

根据您的评论,任何以 /fetch 开头但与别名路径中的静态文件不匹配的 URI 都应重定向到 /fetch/index.php

location ^~ /fetch {
    alias /usr/share/nginx/html/another_folder/web;

    if (!-e $request_filename) { rewrite ^ /fetch/index.php last; }

    location ~ \.php$ {
        if (!-f $request_filename) { return 404; }

        include        fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME $request_filename;
        fastcgi_pass   127.0.0.1:9000;
    }
}

由于this long term issue,我们避免使用try_filesalias

有关if 的使用,请参阅this caution

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-15
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多