Nginx giving 404 Not Found (ubuntu) - php

I'm trying to configure nginx to display a page (index.html) inside my folder /var/www/Exercises/PHP/taskList but it's only displaying 404 Not Found. This is my configuration file inside sites-enabled folder:
server {
listen localhost:80;
server_name taskList.dev *.taskList.dev;
root /var/www/Exercises/PHP/taskList;
access_log /var/log/taskList.access.log;
error_log /var/log/taskList.error.log warn;
add_header Strict-Transport-Security "max-age=2592000; includeSubdomains";
add_header X-Frame-Options DENY;
add_header 'Access-Control-Allow-Origin' '*';
include conf.d/server/remove-trailing-slash.conf;
include conf.d/server/symfony2.conf;
include conf.d/server/fastcgi.conf;
include conf.d/server/chromeframe.conf;
include conf.d/server/deny.conf;
}
And I've got this in my host file:
127.0.0.1 localhost
127.0.0.1 taskList.dev
I'm not sure what I'm doing, I did some research but I'm stuck with the 404 error page.

Add a location block, you'll probably want to use try_files as well, something like this:
location / { try_files $uri =404; }

Related

Laravel assets and routes returning 403 after deploying on production server using nginx

The routes and assets are loading properly on local server. However, after I deployed on production server using nginx, only the root url is working which is http://calculator.example.com. But all the assets are returning 403. Also when I try to access any routes for eg: http://calculator.example.com/page-1/ it is also returning 403.
Nginx config:
server {
listen 80;
server_name calculator.example.com;
root /var/www/html/calculator/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;
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.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /.(?!well-known).* {
deny all;
}
}
If you have directory indexing off, and is having this problem, it's probably because the try_files you are using has a directory option:
location / {
try_files $uri $uri/ /index.php?$query_string;
} ^ that is the issue
Remove it and it should work:
location / {
try_files $uri /index.php?$query_string;
}
Why this happens
TL;DR: This is caused because nginx will try to index the directory, and be blocked by itself. Throwing the error mentioned by OP.
try_files $uri $uri/ means, from the root directory, try the file pointed by the uri, if that does not exists, try a directory instead (hence the /). When nginx access a directory, it tries to index it and return the list of files inside it to the browser/client, however by default directory indexing is disabled, and so it returns the error "Nginx 403 error: directory index of [folder] is forbidden".
Directory indexing is controlled by the autoindex option: https://nginx.org/en/docs/http/ngx_http_autoindex_module.html
First, you need check if requests have been passed to app.
if 403 be returned in nginx, i think issue is user run nginx not have permission to access or execute code.
if 403 be returned in php app, please debug normally.
This issue got resolved after I removed the deny all for other location:
location ~ /.(?!well-known).* {
deny all;
}

Nginx - Laravel and Reactjs in single config

I need to setup staging for my react/laravel project.The laravel serves as api and the react is an stand alone reactjs create-react-app.
What I want to achieve is to have a domain like this
project.staging-site.com // as the main site
project.staging-site.com/backend // as the laravel site
Inside laravel, I have also a dashboard that is also built with reactjs(not by create-react-app) but by the default configuration by running the laravel built in frontend scaffolding.
So in my localhost, backend looks likes this
localhost:8000 // where the dashboard lives
localhost:8000/api/V1 //where the api routes lives(consumed by the reactjs frontend)
localhost:8000/admin/V1 // where the admin routes lives(consumed by the reactjs dashboard)
And my frontend looks like this
localhost:3000
I tried this nginx config, the frontend works, but the backend part returns 404
project.staging-site.com/backend
project.staging-site.com/backend/admin
file not found.
Here is my nginx
server {
listen 80;
server_name project.staging-site.com;
root /portal/front/build;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
charset utf-8;
location /backend/ {
root /portal/backend/current/public;
try_files $uri $uri/ /index.php?$query_string;
}
location / {
root /portal/front/build;
try_files $uri /index.html;
}
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;
}
}
And here's my route for the backend dashboard
Route::get('/', function () {
return view('index');
});
///////React
Route::group(["prefix" => "admin"], function () {
/*
|---------------------------------------------------------------------------
| React Routes
|---------------------------------------------------------------------------
*/
Route::view('/{path?}', 'index');
});
I had similar configuration in one of my projects - with front-end react app, and back-end in Laravel(no React in back-end though).
Here is what I did to deploy them on nginx.
I created separate server configs for front-end and back-end, with different listen ports for each.
Verify the route configs and ensure that the front-end hits the right back-end urls.
Also, add log configs in nginx config as given below so that you have some more information when you get some errors.
error_log /var/log/nginx/laravel-app-error.log;
access_log /var/log/nginx/laravel-app-access.log;
Hope this gives you some idea to find the root cause for the 404 error.

nginx error: openat() failed (20: not a directory) for images

I have a PHP project, a REST API. Nginx configuration is working for the API but is not for uploaded images
Images are always returning 404 error
the project starts at /public directory, upload directory is inside public, so the image access url is something like:
DOMAIN.COM/upload/201812/20181204133821.jpg
The actual NGINX configuration is
server {
listen 80;
listen [::]:80;
set $root_path '/usr/share/nginx/html/api/public';
root $root_path;
index index.php index.html index.htm;
server_name api-eduplus.blanco-estudio.com;
#try_files $uri $uri/ #rewrite;
try_files $uri $uri/ /index.php?q=$uri&$args;
location ~* \.(jpg|jpeg|png|gif)$ {
root $root_path;
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
#error_log logs/error.log warn;
}
So the API starts at /public directory and images are uploaded into /public/upload/
Also. The nginx error log on the server says:
2018/12/04 16:35:54 [error] 17338: *1 openat() "/usr/share/nginx/html/api/public/upload/201812/20181204133821.jpg" failed (20: Not a directory), request: "GET /upload/201812/20181204133821.jpg HTTP/1.1"
Please help, I'm actually stuck
I just fixed this issue on my server. The cause of the error was a symlink.
I just encountered this (not very elucidating) error message on my server's nginx config:
"/path/to/index.html" is not found (20: Not a directory)
After some trial-and-error, I determined that the actual cause was that nginx couldn't access the site's document root because part of the path to the root included a symlink, but I had disable_symlinks on; set in my main nginx.conf file.
Commenting-out the disable_symlinks on; line or changing it to disable_symlinks off; fixes the issue.
See also:
https://nginx.org/en/docs/http/ngx_http_core_module.html#disable_symlinks

NGINX + Wordpress gives ERR_TOO_MANY_REDIRECTS

I have been trying to host a simple Wordpress blog using NGINX as the web-server. The blog is hosted as a subdirectory under domain_name.com/blog.
The main blog opens up correctly. But when trying to open the wp-admin under domain_name.com/blog/wp-admin my browser shows ERR_TOO_MANY_REDIRECTS.
I am not sure if this is an issue with my NGINX configuration or wordpress configuration. Following is my NGINX server block:
server {
listen 80;
server_name <domain_name.com>;
root /var/www/html;
index index.php;
location /blog {
try_files $uri $uri/ /blog/index.php?$args;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
Wordpress is installed under the /var/www/html/blog directory. And the values for "siteurl" and "home" wp_options in the database are both pointing to domain_name.com/blog.
What would be a good way to solve this problem?
Additional notes that might be helpful:
When I try to access static files under the wp-content directory, they open without any issues. No redirection errors there.
It is usual for WordPress to redirect an http session to https whenever accessing wp-admin. This may be controlled using the FORCE_SSL_LOGIN and FORCE_SSL_ADMIN settings in wp-config.php.
When a reverse proxy is terminating SSL, the fact that the originating connection is over https must be conveyed to WordPress to avoid a redirection loop.
Your reverse proxy should be setting headers such as X-Forwarded-Proto.
You need to change your nginx configuration so that the HTTPS flag is set correctly for WordPress.
For example:
map $http_x_forwarded_proto $https_flag {
default off;
https on;
}
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php;
location /blog {
try_files $uri $uri/ /blog/index.php?$args;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_param HTTPS $https_flag;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}

Laravel 500 error when using with nginx

I have tried a lot of configuration settings for nginx to work with laravel but none of them do. I keep getting a 500 error on my webpage.
example.com is currently unable to handle this request.
server {
listen _:80;
server_name example.com;
root "C:\Users\Administrator\Google Drive\projects\laravel";
index index.php index.html;
log_not_found off;
charset utf-8;
access_log logs/cdn.example.com-access.log ;
location ~ /\. {deny all;}
location ~ \.css {
add_header Content-Type text/css;
}
location ~ \.js {
add_header Content-Type application/x-javascript;
}
location / {
include mime.types;
}
location = /favicon.ico {
}
location = /robots.txt {
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9054;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I am pretty brand new to laravel and sort of new to nginx, I have all my laravel files in the document root that I just moved from my http://localhost:8000 development server. How can I make it live and make it work with nginx?
I can see wrong configuration in your post. You need to properly setup you web server. You should set public directory as root directory, set write permissions on a storage folder.
So, change this:
root "C:\Users\Administrator\Google Drive\projects\laravel";
To this:
root "C:\Users\Administrator\Google Drive\projects\laravel\public";
And restart your web server.

Categories