Laravel nginx default file - laravel

I try to configure my laravel installation on a digital ocean instance with nginx running.
The default config looks like this:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/mfserver/public;
index index.php index.html index.htm;
server_name IPADDRESS;
location / {
try_files $uri $uri/ /index.php$is_args&args =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/mfserver/public;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php$is_args&args =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
}
The problem is, that when I call a route like /v1/aeds I get an error 404. Is the config not setup properly?
The route file:
Route::group(['domain' => 'SERVERIP', 'namespace' => 'API'], function() {
Route::group(['prefix' => 'v1', 'namespace' => 'v1'], function() {
// AED ROUTES
Route::get('/aeds', 'AED\APIAEDController#index');
Route::post('/aeds', 'AED\APIAEDController#store');
Route::get('/aeds/{aeds}', 'AED\APIAEDController#show');
}

Try replacing this:
location / {
try_files $uri $uri/ /index.php$is_args&args =404;
}
With this:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /index.php;

Related

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 different root for only mainpage

I am trying to configure nginx to set two roots for one domain. My folder structure looks like:
/var/www/
mainpage/
project/index.php
backend/
public/index.php
I need to have root url example.com uses project in folder mainpage and all other urls uses project in folder backend (this is laravel app).
I have started to try to access mainpage project by url example.com/mainpage, but I'm receiving 403 or "Input file not specified". My nginx config:
server {
server_name example.com;
root "/var/www/backend/public";
index index.html index.htm index.php;
charset utf-8;
location /mainpage/ {
alias /var/www/mainpage/project;
try_files $uri $uri/ /index.php$is_args$args;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
access_log off;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
This config return "403 forbidden". How should I configure nginx to work properly? And how to configure it to access mainpage for "/" url and other urls to use backend project
You can set up root directory inside each location e.g.:
location / {
try_files $uri $uri/ /index.php?$query_string;
root /your/root/directory;
}

I get a 404 on a laravel app running on Nginx

I have this nginx config.
server {
listen 80;
listen [::]:80;
server_name apps.myapp.com;
root /var/www/apps.myapp.com/public;
index index.php;
location / {
try_files $uri $uri/ index.php?$query_string;
}
}
I have this route:
Route::get('/lead', 'LeadsController#index' );
When I access to http://apps.myapp.com/lead, I get 404.
You can run nginx -t to check if there any error in the file configuration, in addition to that you need to add fastcgi_pass
Below is nginx configuration from my production server
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/laravel/public/;
index index.php index.html index.htm;
server_name xxx.xxx.xxx.xxx;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
You can read this tutorial is very helpful:
https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-in-ubuntu-16-04
This is the configuration I use for my Laravel app.
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php?$query_string;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

nginx one domain and multiple projects setting alias

I want to use one domain to map different project.
mydomain.com.tw => /var/www/project1/public
mydomain.com.tw/test => /var/www/project2
but mydomain.com.tw/project2/detail always direct to mydomain.com.tw project route.
project1 => laravel
project2 => vue and using vue-route
listen 80;
listen [::]:80;
server_name mydomain.dev;
index index.php index.html index.htm;
root /var/www/project1/public;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /lottery {
alias /var/www/project2/dist;
try_files $uri $uri/ #rewrites;
}
location #rewrites {
rewrite ^(.+)$ /index.html last;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass php-upstream;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
I solved this question.
Solution:
https://gist.github.com/Maras0830/dc6f627eba005bdfc6b741f7f2ea3178

nginx config for two Laravel projects in different location

I'm trying to run two Laravel projects in a different location without using domain name here my config file.
server {
listen 80 default_server;
server_name ip ;
index index.php index.html index.htm;
location /project1 {
root /var/www/project1/public;
try_files $uri $uri/ /index.php?$query_string;
}
location /project2 {
root /var/project2/public;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)\$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
That returns 404 for both and from the error log return /(nginx root)/favicon.ico failed to open.
I solved using another port
some thing like that
server {
listen 85 ;
listen [::]:85 ;
server_name ip:85 ;
root /var/www/html/projectfolder1;
}
server {
listen 90 ;
listen [::]:90 ;
server_name ip:90 ;
root /var/www/projectfolder2;
}
hope that help some one

Resources