A problem with alias and PHP scripts

I am updating my apps and I have rebuilt them with a bit different structure where public fles html/css/js and PHP files are on web subdirectory and vendor, possible logs, .env and so on are on the directory below web.

But the problem I am facing is that the PHP files are not working on the web subdirectory. I have made many apps that work like that, but now I am starting to update multiple apps that work side by side on the same host and the PHP reference depends if the app is based on the new structure or old.

I have searched the web and talked with chatgpt all my daily dose but nothing seems to solve the problem.

Here is my nginx conf.file:

server {

listen 443 ssl; # managed by Certbotserver_name example.com;
root /var/www/example.com;

index index.php index.html index.htm;

# Logging
error_log /var/log/nginx/example.com.error.log;
access_log /var/log/nginx/example.com.access.log;

# Gzip compression
gzip on;
gzip_vary on;
gzip_min_length 1000;
gzip_comp_level 5;
gzip_types application/json text/css application/x-javascript application/javascript image/svg+xml;
gzip_proxied any;

# Upload limit
client_max_body_size 32M;

# --- BEGIN /kam/ CONFIG ---

# Handle PHP files under /kam/ (e.g., /kam/info.php)
location ~ ^/kam/(.*\.php)$ {
    alias /var/www/example.com/kam/web/$1;
    fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    include fastcgi_params;
    include snippets/fastcgi-php.conf;
}

# Handle static files and routing under /kam/
location /kam/ {
    alias /var/www/example.com/kam/web/;
    index index.php index.html;
    try_files $uri $uri/ /index.php$is_args$args;
}

# --- END /kam/ CONFIG ---

# Default app handling
location / {
    try_files $uri $uri/ @rewrites;
}

# Fallback rewrite handler
# Global PHP handler for everything else (excluding /kam/)
location ~ \.php$ {
    # Prevent it from hijacking /kam/ PHP requests

    fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    include snippets/fastcgi-php.conf;
}

# SSL config - managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

}

The problem is that in /kam directory the PHP files cannot be found, nginx gives 404 error.

Can anyone find where the problem lies.

wbr

hank

2 Likes

I finally got it working:

The significant section of the config file as now with this solution:

location /kam/ {
    alias /var/www/example.com/kam/web/;
    index index.php index.html;
    try_files $uri $uri/ /index.php$is_args$args;
    location ~ \.php$ {
      fastcgi_pass unix:/run/php/php8.3-fpm.sock;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $request_filename;
      fastcgi_param PATH_INFO $fastcgi_path_info;
      fastcgi_index index.php;
  }
}   

Sorry and thank you

-hank

2 Likes

Hi hank,

Glad you were able to resolve this and a huge “Thank You” for sharing the solution.

davemc

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.