Nginx Multiple "Single Page App" routing - regex

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

Related

Multiple configurations with different locations (React App) at the same domain using Nginx

I'm trying to configure Nginx for 2 different locations.
This is my scenario.
www.example.com
location : /var/www/example.com
example.com/panel
location : /var/www/example.com/panel
Try to use:
http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
location ^~ /root {
root /var/www/example.com;
index index.html index.htm;
}
location ^~ /root/react_app(or panel) {
root /var/www/example.com;
index index.html index.htm;
try_files $uri $uri/ /root/react_app/index.html;
}

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 showing blank page for invalid url

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

nginx config for ember.js/rails/passenger using history api

I have an nginx configuration which is correctly serving up index.html no matter what URL the user goes to. For example if the user goes to: myapp.example.com/foo/bar, nginx correctly serves index.html, so that ember.js can handle the /foo/bar route.
But I also need nginx to hand off urls that start with /api to my rails application. This is my current config:
server {
listen 80;
server_name myapp.example.com;
root /home/app/myapp/current/public;
passenger_enabled on;
passenger_user app;
passenger_ruby /usr/bin/ruby2.2;
location /api/ {
}
location / {
try_files $uri $uri/ /index.html?/$request_uri;
}
}
The server (nginx, not rails) returns 404 when ember.js makes any API requests. What should I be putting into the location /api/ section or what else should I change?
Moving the passenger options to the location /api section seems to have worked, like this:
server {
listen 80;
server_name myapp.example.com;
root /home/app/myapp/current/public;
location /api/ {
passenger_enabled on;
passenger_user app;
passenger_ruby /usr/bin/ruby2.2;
}
location / {
try_files $uri $uri/ /index.html?/$request_uri;
}
}

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