Can´t reach laravel endpoints with Nginx (Windows) - laravel

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";

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 throws 404 error for a file route in 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";
}

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?

HTTPServer only on a certain directory of project - NGINX

Goal
My goal is to see something like this, when I visit a certain URL of my app :
Try
location /backup {
autoindex on;
index index.html;
root /home/app/public/backup;
}
Result
Config
My entire vi /etc/nginx/sites-available/default
server {
listen 80 default_server;
server_name default;
root /home/app/public;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
index index.html index.htm index.php;
charset utf-8;
location /backup {
autoindex on;
index index.html;
root /home/app/public/backup;
}
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; }
access_log off;
error_log /var/log/nginx/default-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;
}
}
With your configuration, nginx is trying to locate /home/app/public/backup/backup path, hence 404.
These 3 solutions will work for you
# remove root directive (recommended in your case)
location /backup {
autoindex on;
index index.html;
#root /home/app/public/backup;
}
# set root to public (redundant since already done)
location /backup {
autoindex on;
index index.html;
root /home/app/public;
}
# replace root with alias
location /backup {
autoindex on;
index index.html;
alias /home/app/public/backup/;
}
For more information check how root and alias work, you can also read how nginx handles user requests

Rewrite laravel query parameters with nginx

I have a laravel web app that acts as a CMS and has several sites in one app. I access these sites with the following url:
my-site.com/site/1 - Leads to site 1
I want to rewrite NGINX so that i can point several top domains to their respective site in my CMS.
my-site-1.com -> my-site.com/site/1
How can this be achieved? All questions i can find wants to point a domain to a subfolder, i want to point it to a query parameter.
I use Larave forge + Digitalocean. This is my configuration file
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/default/before/*; #*/
server {
listen 80 default_server;
server_name default;
root /home/forge/default/public;
# FORGE SSL (DO NOT REMOVE!)
# ssl_certificate;
# ssl_certificate_key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
index index.html index.htm index.php;
charset utf-8;
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/default/server/*; #*/
location / {
try_files $uri $uri/ /index.php?$query_string;
if ($http_host ~ "^(www.)?my-site-1.com$"){
#I tried adding som conditional rules here
}
}
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/default-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_connect_timeout 18000;
fastcgi_send_timeout 18000;
fastcgi_read_timeout 18000;
}
location ~ /\.ht {
deny all;
}
}
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/default/after/*; #*/

Resources