Reputation: 55
I try to configure Nginx to distribute my laravel api on a subfolder of my domain
server {
listen 80;
server_name stackoverflow.com;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
add_header X-XSS-Protection "1; mode=block";
charset utf-8;
location /dev {
alias /home/debian/www/stackoverflow/api/dev/public;
try_files $uri $uri/ /dev/index.php?$query_string;
location = /dev/favicon.ico { access_log off; log_not_found off; }
location = /dev/robots.txt { access_log off; log_not_found off; }
error_page 404 /dev/index.php;
error_log /home/debian/logs/nginx/error_log;
access_log /home/debian/logs/nginx/access_log;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
include fastcgi_params;
}
}
location ~ /\.(?!well-known).* {
deny all;
}
}
I tried different configuration, with /dev on try_files, without /dev, but always same result "File not found" and in error log :
*1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 109.221.246.178, server: xxx, request: "GET /dev/ HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php8.1-fpm.sock:", host: "xxx"
or
*1 open() "/home/debian/www/xxx/api/dev/public/login" failed (2: No such file or directory), client: xxx, server: xxx, request: "GET /dev/login HTTP/1.1", host: "xxx"
My poor knowledge on Nginx stop there. I know its about a file path issue, but can't understand where... The configuration works perfectly when I try to set up on location / { }
Thanks a lot for your help !
Upvotes: 1
Views: 1198
Reputation: 427
This is my based conf when i put laravel app in domain subfolder, you can try to add your custom settings on it.
My laravel app root is in "subsite" folder, so you may change "subsite" to "dev" and test it.
location /subsite {
alias /var/www/project/subsite/public/;
try_files $uri $uri/ @subsite;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
}
}
location @subsite {
rewrite /subsite/(.*)$ /subsite/index.php?/$1 last;
}
Upvotes: 3