nginx showing blank page for invalid url - ember.js

I have an emberjs app and using nginx to serve it. I want to redirect to index page if the file not found. I tried using the following configuration but its showing blank page instead of index page.
server
{
listen 80 ;
server_name x.x.x.x
xyz.example.com;
root /home/ubuntu/website/dist/;
index index.html index.htm;
error_page 404 /home/ubuntu/website/dist/;
location / {
try_files $uri $uri.html $uri/ /fallback/index.html;
}
location /fallback {
root /home/ubuntu/website/dist;
}
}
I added the following block after the reference
location /{
error_page 404 = #foobar;
}
location #foobar {
rewrite .* / permanent;
}
Finally, the conf file look like this
server
{
listen 80 ;
server_name X.X.X.X
xyz.example.com;
root /home/ubuntu/website/dist/;
index index.html index.htm;
error_page 404 = #foobar;
location /{
try_files $uri $uri.html $uri/ =404;
}
location #foobar {
rewrite .* / permanent;
}
}

After update of OP
location /{
error_page 404 = #foobar;
}
Should be
location /{
try_files $uri $uri.html $uri/ #foobar;
}

Related

nginx cannot find files in /home/ubuntu/

I don't understand why nginx cannot find files when the root is under home directory, e.g. /home/ubuntu/tabs. It will give 404 error.
It works fine when the root is /var/www/tabs.
server{
listen 80;
#root /var/www/tabs;
root /home/ubuntu/tabs;
server_name 18.191.229.199;
index main.html ;
location / {
try_files $uri $uri/ =404;
}
location /folder {
try_files /folder/main1.html /folder/main2.html =404;
}
}

POST request return 405 on auth (nginx angular)

i'm using django and angular with nginx. I get 405 not allowed when i try to post to /auth/.
my nginx conf:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /home/dev/projet-name/frontend/my-app/dist/tryangular;
include /etc/nginx/default.d/*.conf;
location / {
try_files $uri $uri/ /index.html;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location ~ ^/(api|admin)/ {
include uwsgi_params;
uwsgi_pass SERV_IP:3031;
}
}
Djnago is on the same server.
I don't even know where i messed up

How to have Nginx to proxy pass only on "/" location and serve index.html on the rest

I have a web app that uses Django for the backend and some frontend and ReactJS for stricly the frontend. I am setting up my Nginx configuration and I am trying to get Nginx to proxy_pass only on the "/" location and then on the rest of the locations I want it to serve the index.html file from React.
Here is my current Nginx configuration under sites-available. So the only url that does not have the prefix "/home/" or "/app/" is the homepage. I want Django to serve the homepage and then ReactJS to serve the rest.
server {
listen 80;
root /home/route/to/my/ReactJS/build;
server_name www.mydomain.com;
location = /favicon.ico { log_not_found off; }
location / {
include proxy_params;
proxy_pass http://0.0.0.0:8000;
}
location /home/ {
try_files $uri $uri/ /index.html;
}
location /app/ {
try_files $uri $uri/ /index.html;
}
}
Let me know if you need any more details or if I could clear things up.
Thanks!
Just change it to the following: (replacing the last three location blocks)
location = / {
include proxy_params;
proxy_pass http://0.0.0.0:8000;
}
location / {
try_files $uri $uri/ /index.html;
}
The location = / only matches the exact domain, everything else will be matched by location /.

Nginx Multiple "Single Page App" routing

How to redirect to begins with "/admin" url redirect to /admin/index.html (single page app) and another pages redirect to /index.html (other single page app).
my nginx.conf is here
location / {
if ( $allowed = "deny" ) { return 403; }
try_files $uri /index.html;
}
location /admin {
alias /usr/share/nginx/html/admin;
}
this is working but multi level url redirect to 503 error.
sample: get /admin/questions/1 ->fail 503
sample: get /admin/questions -> fail 503
sample get /admin -> ok
I solved it.
default.conf:
root /usr/share/nginx/html;
index index.html;
location ~ ^/admin {
try_files $uri /admin/index.html;
}
location / {
try_files $uri /index.html;
}

nginx try_files with $host

In my configuration i use the host-name($host) as directory so i don't need to make a entry for each domain in the config (only for development purpose)
one issue is that try_files failed i tried different ways but all of them without success.
try_files html\$host$uri =404;
try_files $document_root\$host$uri =404;
try_files $uri =404;
try_files $document_root\$host$fastcgi_script_name =404;
server {
listen 80;
server_name default;
charset UTF-8;
#access_log logs/host.access.log main;
# redirect server error pages to the static pages
error_page 500 502 503 504 /error/50x.html;
error_page 404 = /error/404.html;
error_page 403 = /error/403.html;
location ^~ /error/ {
internal;
root html/default;
}
location / {
root html/$host;
index index.php index.html index.htm;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
#try_files html\$host$uri =404;
#try_files $document_root\$host$uri =404;
#try_files $uri =404;
#try_files $document_root\$host$fastcgi_script_name =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root\$host$fastcgi_script_name;
include fastcgi_params;
}
The concept it self is correct, but you are applying it in the wrong place, you should place this as a root for the server, not in the try_files
Also I believe you should use $http_host not $host, refer to this answer to know the difference
I don't know why your paths are not absolute, they should start with /
I'll add changes to your current posted config
server {
#since you will match multiple domains I removed the server_name
listen 80 default_server;
charset UTF-8; # don't really need this but it won't hurt.
root /usr/share/nginx/html/$http_host; #assuming this path
index index.php index.html index.htm;
# redirect server error pages to the static pages
error_page 500 502 503 504 /error/50x.html;
error_page 404 = /error/404.html;
error_page 403 = /error/403.html;
location ^~ /error/ {
internal;
root /usr/share/nginx/html/default;
}
location / {
try_files $uri $uri/ /index.php$is_args$query_string;
}
location ~ \.php$ {
# these are the minimal required parameters.
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
}