Nginx throws 404 error for a file route in Laravel - laravel

I have a simple Laravel route to provide to our users private files. We present files to users with the help of a middleware. Thus, we check whether the logged in user really has access to this file.
Everything is working as expected we on local environment. But we ran into a problem when we deployed it to the server. It does not see the Laravel route file in the Nginx location and tries to find the file in the root directory.
Laravel route example as below.
Route::get('lesson-attachment/{uuid}/{filename}', LessonAttachmentController::class)->name('lesson.attachment');
As you can see, we expect to receive a uuid and filename as url parameters. Actually the filaname parameter is required because we returns inline response on the exact url sometimes and we want to show the filename on the browser tab. Anyway, finally the codes here work fine on local machine.
But when we come to the nginx server, things get confused and when we look at the logs, we get the following error.
/var/www/vhosts/example.com/httpdocs/current/public/lesson-attachment/61cd7c8b-a7a1-49f8-86b8-d82825c9f55a/sample-file.pdf" failed (2: No such file or directory)
Here is the nginx conf:
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
The question is how can I run that url path on production server?
https://example.com/lesson-attachment/61cd7c8b-a7a1-49f8-86b8-d82825c9f55a/sample-file.pdf
For more information my development environment:
Macbook Pro
Laravel Valet
For more information our production environment:
Ubuntu 20
Nginx
Plesk Panel (core nginx configurations are provided from plesk)
Using php deployer tool for deployment to production.
UPDATE - The base server blocks of nginx configuration. Please note, it provides by Plesk.
#ATTENTION!
#
#DO NOT MODIFY THIS FILE BECAUSE IT WAS GENERATED AUTOMATICALLY,
#SO ALL YOUR CHANGES WILL BE LOST THE NEXT TIME THE FILE IS GENERATED.
map $sent_http_content_type $expires {
default off;
text/html epoch;
text/css max;
application/javascript max;
~image/ max;
~font/ max;
}
server {
listen ***** ssl http2;
expires $expires;
server_name example.com;
client_max_body_size 134217728;
# mailconfig
location ~* ^/autodiscover/autodiscover\.xml$ {
try_files $uri #mad;
}
location ~* ^(/\.well-known/autoconfig)?/mail/config\-v1\.1\.xml$ {
try_files $uri #mad;
}
location ~* ^/email\.mobileconfig$ {
try_files $uri #mad;
}
location #mad {
rewrite ^(.*)$ /mailconfig/ break;
proxy_pass http://127.0.0.1:8880;
proxy_set_header X-Host $host;
proxy_set_header X-Request-URI $request_uri;
}
# mailconfig
root "/var/www/vhosts/example.com/httpdocs/current/public";
access_log "/var/www/vhosts/system/example.com/logs/proxy_access_ssl_log";
error_log "/var/www/vhosts/system/example.com/logs/proxy_error_log";
#extension letsencrypt begin
location ^~ /.well-known/acme-challenge/ {
root /var/www/vhosts/default/htdocs;
types { }
default_type text/plain;
satisfy any;
auth_basic off;
allow all;
location ~ ^/\.well-known/acme-challenge.*/\. {
deny all;
}
}
#extension letsencrypt end
#extension sslit begin
#extension sslit end
#extension sectigo begin
location ^~ /.well-known/pki-validation/fileauth.txt {
try_files $uri $uri/ =404;
}
location ^~ /.well-known/pki-validation/ {
root /var/www/vhosts/default/htdocs;
types { }
default_type text/plain;
satisfy any;
auth_basic off;
allow all;
location ~ ^/\.well-known/pki-validation.*/\. {
deny all;
}
}
#extension sectigo end
error_page 400 "/error_docs/bad_request.html";
error_page 401 "/error_docs/unauthorized.html";
error_page 403 "/error_docs/forbidden.html";
error_page 404 "/error_docs/not_found.html";
error_page 500 "/error_docs/internal_server_error.html";
error_page 405 "/error_docs/method_not_allowed.html";
error_page 406 "/error_docs/not_acceptable.html";
error_page 407 "/error_docs/proxy_authentication_required.html";
error_page 412 "/error_docs/precondition_failed.html";
error_page 414 "/error_docs/request_uri_too_long.html";
error_page 415 "/error_docs/unsupported_media_type.html";
error_page 501 "/error_docs/not_implemented.html";
error_page 502 "/error_docs/bad_gateway.html";
error_page 503 "/error_docs/maintenance.html";
location ^~ /error_docs {
root "/var/www/vhosts/example.com";
}
location ~ /\.ht {
deny all;
}
location ~ ^/(plesk-stat|awstats-icon|webstat|webstat-ssl|ftpstat|anon_ftpstat) {
auth_basic_user_file "/var/www/vhosts/system/example.com/pd/d..httpdocs#plesk-stat";
autoindex on;
location ~ ^/plesk-stat(.*) {
alias /var/www/vhosts/system/example.com/statistics/$1;
}
location ~ ^/awstats-icon(.*) {
alias /usr/share/awstats/icon/$1;
}
location ~ ^/(.*) {
alias /var/www/vhosts/system/example.com/statistics/$1;
}
}
location ~ ^/~(.+?)(/.*?\.php)(/.*)?$ {
alias /var/www/vhosts/example.com/web_users/$1/$2;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
try_files $uri $fastcgi_script_name =404;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass "unix:/var/www/vhosts/system/example.com/php-fpm.sock";
include /etc/nginx/fastcgi.conf;
}
location ~ \.php(/.*)?$ {
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
try_files $uri $fastcgi_script_name =404;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass "unix:/var/www/vhosts/system/example.com/php-fpm.sock";
include /etc/nginx/fastcgi.conf;
}
index "index.html" "index.cgi" "index.pl" "index.php" "index.xhtml" "index.htm" "index.shtml";
disable_symlinks if_not_owner "from=/var/www/vhosts/example.com";
add_header X-Powered-By PleskLin;
include /etc/nginx/conf.d/expires.global;
include "/var/www/vhosts/system/example.com/conf/vhost_nginx.conf";
}

Related

Why Laravel9 + vue3 app production build keeps adding /build/ path in my URL?

This is my nginx config:
server {
listen 80;
listen [::]:80;
server_name test.com;
root /var/www/test.com/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
#include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
#fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
eveything is working except my url keeps displaying like this:
http://test.com/build` instead of `http://test.com/
http://test.com/build/profile/2` instead of `http://test.com/profile/2
is there a problem in my configuration?
and the worst part is when I try to refresh browser the page it returns 404.
Thank you!

Nginx 404 Not Found Error. How can I fix it?

I'm using Laravel 8 for my project.
Php: 7.4
Using AWS Nginx Server
My Application's Homepage is fine. The homepage is working well. But, when I'm clicking a link then I'm getting a 404 Not Found error from Nginx.
Here is my server config in nginx.conf
server {
listen 80;
listen [::]:80;
server_name something.com;
root /srv/something.com;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
What to do? What to change?
How to fix this error?

Can´t reach laravel endpoints with Nginx (Windows)

I´m trying to reach laravel endpoints with Nginx in Laragon and in Windows. I have an Arduino Leonardo connected to wifi by a wifishield, and it reaches the service but always the index.php page or 404 not found. I´ve tried all nginx configurations but none helped.
This is the arduino code for connecting the server:
void httpRequest() {
client.stop();
Serial.println("\nStarting connection to server...");
if(client.connect(server, 8080)) {
client.println("GET /api_terrarium/api/get-actuators");
client.println("Connection: close");
client.println();
lastConnectionTime = millis();
} else{
Serial.println("connection failed");
}
}
The nginx config files are:
Server file:
server {
listen 8080;
server_name api_terrarium.test *.api_terrarium.test;
root "D:/laragon/www/api_terrarium/public/";
index index.html index.htm index.php;
#location / {
# try_files $uri $uri/ /index.php$is_args$args;
#autoindex on;
#}
location ~ \.php$ {
#try_files $uri /index.php =404;
try_files $uri $uri/ /index.php?q=$uri&$args;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php_upstream;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
charset utf-8;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location ~ /\.ht {
deny all;
}
}
# This file is auto-generated.
# If you want Laragon to respect your changes, just remove the [auto.] prefix
# If you want to use SSL, enable it at: Menu > Nginx > SSL > Enabled
Default conf:
server {
listen 8080 default_server;
server_name localhost ;
root "/var/www/public";
index index.html index.htm index.php;
# Access Restrictions
allow 127.0.0.1;
allow all;
include "D:/laragon/etc/nginx/alias/*.conf";
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
location ~ \.php$ {
allow all;
include snippets/fastcgi-php.conf;
fastcgi_pass php_upstream;
#fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
charset utf-8;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location ~ /\.ht {
}
}
And the laravel web.php:
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::post('/add-new-parameter', [ 'uses'=>'ParametersController#addParameter']
);
Route::get('/get-parameter', ['uses'=>'ParametersController#getParameter']
);
Route::get('/get-historic', ['uses'=>'ParametersController#getHistoric']
);
Route::delete('/delete-historic', ['uses'=>'ParametersController#deleteHistoric']
);
Route::get('/get-actuators', ['uses'=>'ActuatorController#getActuators']
);
Nginx didn´t get the api.conf and perma choose the default one, so in Nginx.conf I commented
include "D:/laragon/etc/nginx/sites-enabled/*.conf";
and added
include "D:/laragon/etc/nginx/sites-enabled/api_terrarium.test.conf";

Nginx Configuration for Wordpress AND Laravel Application

I'm trying to get my Wordpress blog hosted on blog.domain.com to work on domain.com/blog.
I have been able to do this with proxy_pass successfully but the issue is certain scripts such as the login page for admins (/wp-login.php) does not work - it gives me File not found.. I have isolated the issue to my Laravel applications FastCGI setup in my Nginx configuration. When I comment it out, my blog works but then my Laravel app is inaccessible. I've been trying different configurations but I have little knowledge with Nginx.
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name mydomain.com;
server_tokens off;
root /home/appfolder/mydomain.com/public;
...
...
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/mydomain.com/server/*;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# WHEN I COMMENT OUT THIS BLOCK, BLOG WORKS BUT LARAVEL BREAKS
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.4-fpm-mydomain.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location /blog {
proxy_redirect off;
proxy_set_header Host $host;
proxy_pass https://blog.mydomain.com;
}
location /blog/wp-content {
proxy_pass https://blog.mydomain.com/wp-content;
}
location /blog/wp-includes {
proxy_pass https://blog.mydomain.com/wp-includes;
}
location /blog/wp-content/uploads {
proxy_pass https://blog.mydomain.com/wp-content/uploads;
}
location /blog/wp-login.php {
proxy_pass https://blog.mydomain.com/wp-login.php;
}
location /blog/wp-admin {
proxy_pass https://blog.mydomain.com/wp-admin;
}
location /bitnami {
proxy_pass https://blog.mydomain.com/bitnami;
}
location /mod_pagespeed_beacon {
proxy_pass https://blog.mydomain.com/mod_pagespeed_beacon;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/mydomain.com-error.log error;
error_page 404 /index.php;
location ~ /\.(?!well-known).* {
deny all;
}
}
...
Hosted on an Ubuntu server managed by Laravel Forge. Any ideas?
Update: As per Richards answer, I have started getting it working a little bit but I need to add rules for all files which doesn't seem efficient.
I'm trying to get all .PHP files related to Wordpress (after /blog/) to be forwarded to the proxy. I have tried:
location ~ /blog/wp-admin/(\d+) {
add_header X-debug-message "$1";
proxy_pass https://blog.mydomain.com/wp-admin/$1;
}
But this doesn't seem to work.
I sorted it out by creating a regular expression to match anything after /wp-admin/ on forward it to the proxy. The configuration I use is:
location ~ (blog\/wp-admin)\/(.*)\.php$ {
proxy_pass https://blog.mydomain.com/wp-admin/$2.php?$query_string;
}
$2 is the second capture group (.*) which is appended to the proxy_pass URL and added any further arguments with ?$query_string.

Nginx / Laravel config to redirect from non index.php route

I've just deployed a newly built website running Laravel 5 and nginx and struggling to setup my nginx config to 301 redirect some old URLs - specifically those with .php extensions.
I'm looking to 301 redirect the below...
domain.com/foobar.php?id=123 -> domain.com/foobar/123
The default config assumes everything runs from index.php, so Laravel does not trigger on foobar.php.
I have tried adding the following rewrite rule, but it doesn't trigger (I assume because nginx isn't listening out for foobar.php requests?
rewrite ^/foobar.php(.*)$ /foobar/$1/ permanent;
Relevant snippet of my current nginx config below...
server {
listen 443 ssl;
server_name domain.com;
root /home/domain.com/public;
index index.html index.htm index.php;
charset utf-8;
if ($request_method = GET ) {
rewrite ^([^.]*[^/])$ $1/ permanent;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/domain.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
This should work:
rewrite ^/foobar.php /foobar/$arg_id/? permanent;
You can get any argument like this $arg_{your_argument}. Just replace {your_argument} with the actual argument name.

Resources