Конфигурация Nginx Yii2 в разных папках

Я столкнулся с проблемой настройки сервера nginx для базового приложения yii2.

Вот мой файл сервисного блока:

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, 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

И брат показывает: Не указан входной файл.

Можете ли вы помочь мне с этим вопросом?

Благодарю вас!


person Farhad    schedule 11.02.2017    source источник
comment
Вы хотите, чтобы /fetch/index.php располагался в /usr/share/nginx/html/another_folder/web/index.php или /usr/share/nginx/html/another_folder/web/fetch/index.php?   -  person Richard Smith    schedule 11.02.2017
comment
В этой папке:/usr/share/nginx/html/another_folder/web/index.php   -  person Farhad    schedule 11.02.2017
comment
Вам нужно использовать директиву alias. См. этот ответ.   -  person Richard Smith    schedule 11.02.2017
comment
Спасибо. Это решило проблему. Но он не может найти URL-адреса, используя красивый URL-адрес в yii2. он снова ищет файл index.php в папке /usr/share/nginx/html   -  person Farhad    schedule 11.02.2017
comment
Это внутри корня another_folder? И будут ли красивые URI иметь префикс /fetch?   -  person Richard Smith    schedule 11.02.2017
comment
Да. Правильный. Извините за долгий ответ.   -  person Farhad    schedule 13.02.2017


Ответы (1)


В дополнение к вашему комментарию любой URI, начинающийся с /fetch, который не соответствует статическому файлу в пути с псевдонимом, должен быть перенаправлен на /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;
    }
}

Мы избегаем использования try_files с alias из-за этой долгосрочной проблемы.

См. это предостережение относительно использования if.

person Richard Smith    schedule 13.02.2017