How to use php with base path with nginx,
when the main php project BB is nested inside php project AA,
with php extension removed from url.Where
notebook_app_wsp
is project AA.
Wherenotebook_app
is project BB.Laragon is used.
-
server {
listen 80;
server_name localhost ;
root "C:/usp/jobhp/notebook_app_wsp";
error_log logs/error.log debug;
rewrite_log on;
# index index.html index.htm index.php;
# Access Restrictions
allow 127.0.0.1;
deny all;
location / {
try_files $uri $uri/ =404;
autoindex on;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass php_upstream;
#fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
include "C:/usp/jobhp/notebook_app_wsp/notebook_app/docs/nginx_example/nginx-notebook_app.conf";
}
-
nginx-notebook_app.conf
location = / {
# expires 12h;
# return 301 /web/;
rewrite ^ /web/index.php last;
}
location ^~ /web/ {
alias C:/usp/jobhp/notebook_app_wsp/notebook_app/public/;
index index.php;
try_files $uri $uri/ @append_php_ext;
location ~ \.php$ {
# mark as internal, prevent user from accessing with .php extension in url.
internal;
try_files $uri =404;
include fastcgi_params;
fastcgi_pass php_upstream;
# fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
# fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
location @append_php_ext {
# @main:
# you must append the `/web/`, when rewrite the uri to add `.php` extension.
# cuz after this, nginx will redo the location scan globally, not inside this nested scope.
# during that, the `/web/` will cause the uri go to the nested block again `location ^~ /web/ {` > `location ~ \.php$ {`.
# otherwise, the uri will go to the global `location ~ \.php$ {`, which is using the document root, not the alias path.
rewrite ^/web/(.*)$ /web/$1.php last;
}
You may also use
try_files $uri $uri/ /web/$uri.php$is_args$args;
(PS: devto markdown syntax is incompatible with my docs.)
Top comments (0)