4

I faced with problem in configuring nginx server for yii2 basic app.

Here is my service block file :

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;
        }

}

My project located in another folder "another_folder". And i want that when user goes to url : http://ip/fetch files nginx will serve files from another folder.

My error log file returns me :

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

And brother shows : No input file specified.

Can you give help me with this issue?

Thank you!

6
  • Do you want /fetch/index.php to be located at /usr/share/nginx/html/another_folder/web/index.php or /usr/share/nginx/html/another_folder/web/fetch/index.php? Commented Feb 11, 2017 at 13:15
  • In this folder :/usr/share/nginx/html/another_folder/web/index.php Commented Feb 11, 2017 at 13:18
  • 1
    You need to use an alias directive. See this answer. Commented Feb 11, 2017 at 13:23
  • Thank you. It resolved problem. But it cant find urls using pretty Url in yii2. it again looks index.php file in /usr/share/nginx/html folder Commented Feb 11, 2017 at 13:33
  • Is this within the another_folder root? And will the pretty URIs be prefixed with /fetch? Commented Feb 11, 2017 at 14:13

1 Answer 1

3

Further to your comment, any URI beginning with /fetch that does not match a static file within the aliased path, should be redirected to /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;
    }
}

We avoid using try_files with alias because of this long term issue.

See this caution regarding the use of if.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.