How do I get NGINX to properly rewrite and execute on a custom PHP application? -
we have custom php application wrote , runs on apache .htaccess files handle url rewrites. trying convert work under nginx fpm under plesk onyx.
the application generates links like:
https://somedomain.com/mypage (same index/mypage) https://somedomain.com/index/sitemap https://somedomain.com/blog/some-article-name these url's map index.php files take request_uri , use render page responses.
the structure of application nested follows:
docroot (/) ./index.php //handler request in / ./blog/index.php //handler request /blog each index.php expects receive ?path={request_uri} can map request controllers , actions.
i have tried multiple ways nginx using tryfiles , rewrite, no luck. using rewrite can / work, wont render /mypage or /index/sitemap.
if try hit /index/sitemap downloads index.php instead of executing it, , if try blog same thing happens. in fact path works /, others download index.php file.
here configuration now, going wrong?
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ { expires 30d; add_header pragma public; add_header cache-control “public”; try_files $uri @fallback; } location / { #index index.php index.html index.html; rewrite ^/([^?]*) /index.php?path=$1 break; rewrite ^blog/([^?]*) /blog/index.php?path=$1 break; #try_files $uri @fallback; }
your configuration has multiple issues. ignore first location block seems have nothing question.
the first rewrite match, second rewrite never consulted. second rewrite never match anyway, nginx uris begin /. [^?] meaningless, because rewrite uses normalised uri not include ? or query string. using rewrite...break means rewritten uri processed within same location, error location not equipped process php files. see this document more.
a solution using try_files might this:
location / { try_files $uri $uri/ /index.php?path=$uri&$args; } location /blog { try_files $uri $uri/ /blog/index.php?path=$uri&$args; } location ~ \.php$ { ... } see this document more.
Comments
Post a Comment