I have a default Nginx install on Ubuntu 22.04 LTS inside a Docker Container using
I want /var/www/html
to remain as the root but a CakePHP 4 app to be served as follows
requests to hostname.example.com
as served from /var/www/html
requests to hostname.example.com/subdir
are served from /var/www/tgn/webroot
This link https://serversforhackers.com/c/nginx-php-in-subdirectory is for a laravel config. But doesn't seem to work properly with my CakePHP 4 install.
Note: See below for the problem I found using the above config
The ApacheConf default .htaccess located in webroot
contains the following
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
So this is duplicated in Nginx as follows
Nginx Configuration to Serve CakePHP 4 from Sub Directory
This config is the working config!!!
The absolute_redirect
value set to off
stops nginx redirecting to an external URL. By default when a request is made for http://ipaddress:8080/wms it issues a redirect to http://ipaddress/wms/ which breaks my docker config. With absolue_redirect off it redirects from http://ipaddress:8080/wms => /wms/ and keeps the ipaddress:port combination correct and only issues a relative redirect
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html index.php;
server_name _;
absolute_redirect off;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
rewrite ^ /wms/ last;
try_files $uri $uri/ =404;
}
location /wms {
index index.php;
alias /var/www/wms/webroot;
try_files $uri $uri/ @wms;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
location @wms {
rewrite ^ /wms/index.php last;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ /\.ht {
deny all;
}
}
Partially working
This was an attempt but in this configuration it was issuing an external redirect and redirecting to port 80 instead of the original docker port due to a problem after login with it redirecting from http://10.19.23.87:6002/wms => http://10.19.23.87/wms/
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html index.php;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
rewrite ^ /wms/ last;
try_files $uri $uri/ =404;
}
location /wms {
index index.php;
alias /var/www/wms/webroot;
try_files $uri $uri/ @wms;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
# include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
# fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
}
location @wms {
rewrite ^/wms/(.+)$ /wms/index.php last;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ /\.ht {
deny all;
}
}
NOT working Laravel for Hackers Config Follows
This was also causing the request to be appended. By that I mean a request to
http://10.19.23.87:6002/wms/pallets/onhand
would return http://10.19.23.87:6002/wms/pallets/onhand?pallets/onhand
http://10.19.23.87:6002/wms/pallets/onhand?pallets/onhand?pallets/onhand
Until it reached the URL request length limit and NGinx returned an error
# /etc/nginx/sites-enabled/default
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html index.php;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location /subdir {
alias /var/www/tgn/webroot;
try_files $uri $uri/ @subdir;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
}
location @subdir {
rewrite /subdir/(.*)$ /subdir/index.php?/$1 last;
}
# pass PHP scripts to FastCGI server
# not needed if no php in /var/www/html
# location ~ \.php$ {
# include snippets/fastcgi-php.conf;
# fastcgi_pass unix:/run/php/php8.1-fpm.sock;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
0 Comments