Laravel 404 on production server NGINX - nginx

I have Laravel with NGINX on my production server, I have added new routes and they work fine on the localhost. But in production, it returns 404 by Laravel.
1. I have restart NGINX but still no result.
2. I look a the route list and they are present.
What is the problem and how can I fix it?
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/laravel/public;
index index.php index.html index.htm;
server_name www.somesite.jp;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}

With me, I have found that localhost often doesn't mind the case on routes, but in production, getting the case correct is important. Same true on Controller file names etc.

Ok, so it is my bad. Problem was that on the server there were different conditions and I was trying to get User::findOrFail($userId) where ID didn't exist and it returns Laravel 404 error!

Related

How to configure Nginx for URL mapping of CNAME requests?

I'm trying to configure my Nginx server block to accept requests without changing the requested domain, so that my users can use their custom domain with my application via a DNS CNAME-type entry.
The expected behaviour is as follows:
User creates a dns cname entry pointing to my server/domain e.g. www.userdomain.com to www.mydomain.com
My server then directs the request to my application www.mydomain.com/app while maintaining the URL www.userdomain.com
I've been able to make this behaviour work as above by setting my www.mydomain.com server block as default_server, but i'm trying to find a different way because i want to replicate this behaviour with a different server block/domain in the same server.
My current Server Block looks like the example below:
server {
listen 80 default_server;
root /var/www/mydomain.com/app;
index index.php;
server_name www.mydomain.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
location ~ /(.*)$ {
try_files $uri $uri/ /;
}
}
Is it possible to reproduce this behaviour without setting the default_server flag?

Reverse proxy nginx to itself

I am currently hosting a single-page react app that is hosted in the URL root like so:
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
try_files $uri /index.html;
}
}
I need to put the site behind an AWS elastic load balancer and at the same time change the path so everything is within a /support directory e.g. http://example.com/index.html -> http://example.com/support/index.html.
AWS ALBs do not support URL rewriting so I have to do this within the nginx config on the server. First of all I tried changing the config to:
server {
listen 80;
server_name localhost;
location /support {
alias /var/www/html;
try_files $uri /index.html;
}
}
This sort-of works but the URLs within the javascript content don't contain the /support path (e.g. they contain http://example.com/script.js instead of http://example.com/support/script.js).
I then tried creating a reverse-proxy config to proxy /support to /, which sadly put nginx in an infinite loop until it ran out of worker threads:
server {
listen 80;
server_name localhost;
location /support {
proxy_pass http://localhost:80;
}
location / {
root /var/www/html;
try_files $uri /index.html;
}
}
I'm confused why requests are going into a reverse-proxy loop? Shouldn't proxy_pass remove the /support prefix before proxying the request, and therefore it shouldn't be "caught" again by the /support location?
Just a guess.
Do you want to serve something on /?
If not - it is easy:
server
{
listen 80;
server_name localhost;
location /support/
{
alias /var/www/html/;
try_files $uri $uri/ /index.html;
}
location /
{
return 303 http://localhost/support$request_uri;
}
}
Fiddle around with the ending slashes if it does not work (using them - or not - makes often a difference).
Use alias instead of root so that /support is not added to the /var/www/html folder.
Everything gets redirected to /support.
If you want to serve something on / which is different from /support:
Use sub_filter or subs_filter in /support to rewrite your source code links on-the-fly so that they will never use /.
If you have redirects inside your source code (or proxy_pass backend) - you need proxy_redirect and/or Lua to catch and change them on-the-fly.

Dokku redirects to another domain when requested site is down

I have Dokku installed on a server, with multiple sites/domains deployed to it. When one of my sites goes down, all HTTP requests to it get redirected (for some reason) to another site. This is confusing. I'm expecting Dokku to show some error page in this case. Is it the default behavior or I did something wrong?
PS. This is the problem: https://github.com/dokku/dokku/issues/2602
How about adding a custom error page based on the error code by editing vhost file:
server{
server_name www.foo.com;
root /srv/www/foo/public_html;
expires 1M;
access_log /srv/www/foo/logs/access.log;
error_log /srv/www/foo/logs/error.log;
error_page 404 /404.html;
location / {
index index.html;
rewrite ^/(.*)/$ /$1 permanent;
try_files "${uri}.html" $uri $uri/ =404;
}
location = /404.html {
internal;
}
}
Your server error might be caught from codes 404 or 500

nginx to always serve the root index.html in every path

I have currently the code below. I am wondering if it's possible to still service this root even though I go to other pages like http://localhost/dog. The problem with my command below is it will return 404
server {
listen 80;
server_name localhost;
location / {
root /usr/src/app/angularjs/dist;
}
}
It is possible. Add the try_files directive to your location block, this will tell nginx to load all requests that cannot be matched to a filesystem path with your index.html:
try_files $uri /index.html;

nginx configure default error pages

First off, first time nginx user. So, I'm still learning the differences from Apache and nginx.
I have a stock install of nginx. (apt-get install nginx-full) I modified the default configuration found at '/etc/nginx/sites-enabled/default' for my setup. However, error pages just don't work. My docroot is /server/www, and my error pages are located within the directory /server/errors/. Below is my config.
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /server/www;
index index.html;
server_name website.com;
location / {
try_files $uri $uri/ /index.html;
}
error_page 403 404 405 /40x.html;
location /40x.html {
root /server/errors;
internal;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /server/errors;
internal;
}
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;
}
}
All pages that should generate a 403 or 404 page, just load the index.html file. I believe the something is probably happening with 500 errors, however, that's a bit harder to generate.
Also, if it's relevant, I moved the error pages above docroot, but they weren't working before that. I made that move as the 'internal' flag didn't appear to make the pages internal as the documentation claimed, as I could still access them directly.
Any advise/help you could give would be great!
The error issue was related to the location / having an end value of /index.html. Essentially, nginx was first trying the uri as a file, then as a directory, and then reverting to index.html. Instead I wanted it to return a 404. So I changed it to the below and now it works.
location / {
try_files $uri $uri/ =404;
}

Resources