Laravel routes issue with Nginx - laravel

I've put my Laravel app files into /var/www/html/api/v2directory.
My nginx conf looks like this:
listen 80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-de$
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi$
fastcgi_param PATH_INFO $fastcgi_path_info;
}
I want to access my routes like this: [host]/api/v2/[route]
But it is throwing 404 right now except for homepage.
Please let me know what could be wrong here.

Change this line
root /var/www/html;
to
root /var/www/html/..../public;
Then restart nginx
$ sudo systemctl restart nginx

Related

Laravel + nginX + Windows 10 + api routing issue (404 Error)

I am trying to make a very simple API request with above settings. It is working perfectly if I serve it with artisan serve command. But If I make the request with nginx, it gives 404 error. Thats why I guess the issue is with the nginX. my nginX conf is as follows.
server {
listen 80 ;
listen [::]:80 ;
root D:/sites/demo/public;
index index.php index.html index.htm ;
server_name laravel.local;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri /index.php = 404;
fastcgi_pass 127.0.0.1:9123;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}

How to fix "GET css/app.css net::ERR_ABORTED 404 (Not Found)"?

Installed php-fpm and nginx.
The site opens, but the CSS does not want to load.
Tell me how to solve the problem?
server {
listen 80 default_server;
root /var/www/public/;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_pass phpfpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
UPDATE: interesting moment. I have a debugger installed and the css is loading from it.
/_debugbar/assets/stylesheets?v=1553506748

Nginx and Laravel: How to restrict "location" block to from "/" to "/api"?

I have a Laravel application that I would like to use only for its /api routes. When /, /login, /profile, etc are accessed, I would like nginx to serve the index file located in the base root, /var/www/html/index.html.
Here's my current default.conf. The /api routes work, but it also serves / from the backend, which I don't want.
If I simply change location / to location /api, then the /api routes become inaccessible, and trying to access them returns the index file located in the base root, /var/www/html/index.html. This is the opposite of what I am trying to achieve. Haha.
How can I keep the /api routes accessible, while also preventing / from being served by the backend?
server {
listen 80;
root /var/www/html;
server_name _;
index index.php index.html index.htm;
# API routes should be handled by the backend (Laravel).
##### I want to change the following line from "/" to "/api".
location / {
root /var/www/backend/public;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
}
}
According to your configuration, that should already be happening.
the directive:
try_files $uri $uri/ /index.php?$query_string;
Will attempt first to locate the static file, and if the static file exists and is not a php file, it will be served by nginx directly. Only if it is a php file will it be served with the php backend.
If you want to prevent .php files from being executed at all, you can modify to separate the / location from the /api location:
location ~ ^/api/.*\.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
I accomplished this, but somewhat inelegantly.
I have 2 location blocks:
location ~ "^(?!/api).*$" matches all routes except those that begin with /api.
location / matches all other routes. The reason that it does not match all routes is simply that nginx matches location blocks with regular expressions first.
Strangely, using a regular expression for the second block to match all routes that do begin with /api did not work. I still don't understand why.
server {
listen 80;
root /var/www/html;
server_name _;
index index.php index.html index.htm;
# All non-API routes should be handled by the frontend.
# Use a regular expression to identify all requests
# that do NOT begin with "/api".
location ~ "^(?!/api).*$" {
root /var/www/frontend/public;
try_files $uri $uri/ /index.html =404;
}
# API routes should be handled by the backend (Laravel).
# (Since regex-based location blocks are matched first,
# this will be a fallback to the above location block).
location / {
root /var/www/backend/public;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
}
}

Nginx server laravel route directory 404 error on Windows

I'm getting a 404 every time I try to open a page other than the welcome page. I've read all the posts here on how to fix the problem, but none of the solutions worked for me.
I'm on Windows 10 and use WPN-XM for the Nginx server.
My laravel project is installed in C:/server/www/myapp
My configuration looks as follows:
server {
listen 127.0.0.1:80;
server_name localhost;
root www;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass php_pool;
fastcgi_index index.php;
#fastcgi_param PHP_FCGI_MAX_REQUESTS 1000;
#fastcgi_param PHP_FCGI_CHILDREN 100;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param REMOTE_ADDR $http_x_real_ip;
# mitigate namespace conflict, see httpoxy.org
fastcgi_param HTTP_PROXY "";
include fastcgi_params;
}
I tried replacing root www with "C:/server/www/myapp/public" and a couple of other options.
I used try_files $uri $uri/ /index.php$is_args$args instead of try_files $uri $uri/ /index.php?$query_string
Any help will be appreciated.

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

Resources