2

this is my nginx configuration file

server {
    listen      90;
    server_name localhost;
    root        /var/www/wypok/;
    index       index.php index.html index.htm;

    location / {
        if ($request_uri = /) {
            return 301 /mirkoplusy;
        }
        try_files $uri $uri/ @rewriteToMirkoplusy;
    }

    location @rewriteToMirkoplusy {
        return 301 /mirkoplusy;
    }

    location ~* /mirkoplusy {
        rewrite (?i)^/mirkoplusy/wpis/(.*)/(css|js|fonts)/(.+)/?$ /mirkoplusy/$2/$3;
        rewrite (?i)^/mirkoplusy/wpis/(.*)/?$ /mirkoplusy/;
    }

    location ~* /api {
        rewrite (?i)^/api/v(\d+)/(.+)/?$ /mirkoplusy/api/v$1/main.php?_url=/$2;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

The problem is that php files are not executed under the api directory. When I connect to http://localhost:90/api/v2/init I get back the source code of my main.php file which is called by the rewrite rule

location ~* /api {

   rewrite (?i)^/api/v(\d+)/(.+)/?$ /mirkoplusy/api/v$1/main.php?_url=/$2;

}

I created test.php file under the main root and it works so there must be some problem in that rewrite rule. Genreally I want every api call like /api/v2/entry/23224 to be passed to /api/v2/main.php file.

1 Answer 1

1

http://nginx.org/en/docs/http/ngx_http_core_module.html#location
...
Then regular expressions are checked, in the order of their appearance in the configuration file.
...


I think that rewritten url is catched by location ~* /mirkoplusy and is not processed by location ~ \.php$.
Try to define locations mirkoplusy and api by prefix:

location /mirkoplusy {
    rewrite (?i)^/mirkoplusy/wpis/(.*)/(css|js|fonts)/(.+)/?$ /mirkoplusy/$2/$3;
    rewrite (?i)^/mirkoplusy/wpis/(.*)/?$ /mirkoplusy/;
}

location /api {
    rewrite (?i)^/api/v(\d+)/(.+)/?$ /mirkoplusy/api/v$1/main.php?_url=/$2;
}

Or add location php to mirkoplusy

location ~* /mirkoplusy {
    rewrite (?i)^/mirkoplusy/wpis/(.*)/(css|js|fonts)/(.+)/?$ /mirkoplusy/$2/$3;
    rewrite (?i)^/mirkoplusy/wpis/(.*)/?$ /mirkoplusy/;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I deleted the ~* chars from both rules and everything works, thank you.

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.