Nginx getting file not found on dynamic root - reactjs

I am trying to load two different react app one on root level and one on sub location i.e /in. What I am doing is I am checking if $uri consist /in /t if that condition is true it will change root path after setting root path I am serving index file. Its not working its throwing 404 not found when i am trying to access domain.com/in/something and domain.com is working fine.
I don't know is it right way to do this or not. I tried this
Here is my nginx config file:
server {
server_name domain.com;
rewrite ^/(.*)/$ /$1 permanent;
set $rootpath "/var/www/html/website/public";
location / {
if ($uri ~ \/(in|t)\/){
add_header X-uri "$uri";
set $rootpath "/var/www/html/app/build";
}
root $rootpath;
try_files $uri /index.html;
}
}

Related

Nginx locations

I'm working on a multi-tenant application in React and some clients want to upload .html files to the root of the project to use google console and things like that. I would not like all those files to be mixed with the application code so apart from the following code:
location / {
try_files $ uri /index.html = 404;
}
I would like to add in NGINX a location block that allows me to divert to another folder any other .html file that starts in /, excluding cases like /static/example.html.
Example:
/ -> React default
/static/*.html -> React default
/*.html -> Derive to new folder
I would appreciate any help in this regard.
I tried something like this...
location /*.html {
root /extras_folder;
}
location / {
root /project_folder;
try_files $uri /index.html =404;
}
But does not work
This adheres to the rules you described:
location ^~ /index.html {
root /project_folder;
try_files $uri /index.html =404;
}
location ~ ^/[^/]+\.html {
root /extras_folder;
try_files $uri /index.html =404;
}
location / {
root /project_folder;
try_files $uri /index.html =404;
}
Second block is a regex match only on *.html in the root path
Third block is for all other paths
First block is to cover the edge case of / being /index.html - regex rules otherwise always get priority over path location blocks
What about this?
server {
listen 80;
root /project_folder;
location / {
try_files $uri #extras_folder;
}
location #extras_folder {
root /extras_folder;
try_files $uri #index_html;
}
location #index_html {
try_files /index.html =404;
}
}
This looks in /project_folder first, in /extras_folder second and, if still no file is found finally serves /project_folder/index.html (or 404 if that file doesn't exist).
You'd put uploaded files into /extras_folder in this case.

Moving Cakephp 3.x application into nginx subfolder

System
nginx on Ubuntu
CakePhp 3.4.6
Problem
I'm trying to move a CakePhp application into a subfolder on the webserver.
Let's say the subfolder is called /project
and root webserver directory is under /var/www/html/
I tried something like this in the nginx default (without any success):
location /project {
alias /var/www/html/project/webroot;
try_files $uri $uri/ /project/webroot/index.php;
}
The subfolder url-suffix is always interpreted as a controller.
I am guessing that I need to set specific values in the app.php file, so cakephp knows that the url has a prefix but I haven't found a value combination that works yet.
Anybody has guidance here?
It is a combination of app.php and nginx rewrite rule:
config.php/app.php
'base' => '/project'
/etc/nginx/sites-available/default
`
location /project {
if (-f $request_filename) {
break;
}
# prevent recursion
if ($request_uri ~ /webroot/index.php) {
break;
}
rewrite ^/project$ /project/ permanent;
rewrite ^/project/webroot/(.*) /project/webroot/index.php last;
rewrite ^/project/(.*)$ /project/webroot/$1 last;
}
`

Subfolder installation

I'm trying to add on cakephp on to an existing server, but the location / block is being used. I'm following the pretty url on nginx section on the cakephp cookbook. On my test environment, I have the server block looking like
server {
listen 80;
server_name localhost;
access_log /var/www/html/log/access.log;
error_log /var/www/html/log/error.log;
location / {
root /var/www/html/cakephp/app/webroot/;
index index.php;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
root /var/www/html/cakephp/app/webroot/;
index index.php;
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
From this, I'm able to run my testsController through the url localhost/tests
However, the server that I'm trying to add cakephp to, there is already another application installed at the domain root.
location / {
proxy_pass https://localhost/somepage;
}
I tried setting up a location block like
location /cakephp {
root /var/www/html/cakephp/app/webroot/;
index index.php;
try_files $uri $uri/ /index.php?args;
}
I understand that this wouldn't work because it's looking for cakephp in the url, which it wouldn't be there. Since root is set to be /var/www/html/cakephp/app/webroot, when I access the url localhost/cakephp, is it looking for /var/www/html/cakephp/app/webroot/cakephp?
I'm getting confused about how to set this up. I read about url rewriting and cakephp running in some subdirectory, but I'm not sure if that's what I am looking for. Right now, the application runs with http://localhost/someController. I would like to have the the application run with the url http://localhost/cakephp/someController. How should I setup my nginx config?
Fix static files first
With the config in the question, what you'll find is that nothing really works - not even requests for static files.
Consider:
server {
...
root /wherever/;
error_log /tmp/cakephp.err.log debug; # <- add this
location /cakephp {
root /var/www/html/cakephp/app/webroot/;
index index.php;
try_files $uri $uri/ /index.php?args;
}
}
This will produce:
$ curl -I http://cakephp.dev/cakephp/favicon.ico
HTTP/1.1 404 Not Found
The debug log will help to clarify why this occurs:
-> cat /tmp/cakephp.err.log
...
2015/08/23 10:53:43 [debug] 9754#0: *87 http script var: "/cakephp/favicon.ico"
2015/08/23 10:53:43 [debug] 9754#0: *87 trying to use file: "/cakephp/favicon.ico" "/var/www/html/cakephp/app/webroot/cakephp/favicon.ico"
Nginx is using the whole url as the path to a file, not just the bit after the location prefix. This is where understanding the difference between the root directive and the alias directive is important, and is also a common question (random result, there are many).
So, fixing that first:
server {
...
error_log /tmp/cakephp.err.log debug;
location /cakephp {
alias /var/www/html/cakephp/app/webroot/; # <- alias, not root
index index.php;
try_files $uri $uri/ /index.php?args;
}
}
Will produce:
$ curl -I http://cakephp.dev/cakephp/favicon.ico
HTTP/1.1 200 OK
Then fix php requests
The problem with php requests is more or less the same thing; though the request finds the right php file it's configured such that CakePHP will assume it's installed in the root. There are various solutions to this - here's one:
server {
...
error_log /tmp/cakephp.err.log debug;
location /cakephp {
alias /var/www/html/cakephp/app/webroot/;
index index.php;
try_files $uri $uri/ /cakephp/index.php; # <- redirect to the actual equivalent request
}
location /cakephp/index.php {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
# Explicit script filename
fastcgi_param SCRIPT_FILENAME /var/www/html/cakephp/app/webroot/index.php;
}
}
In this way static files and dynamic requests both work - and the environment variables that CakePHP receives are such that it understands the root of the application to be /cakephp/.

CakePHP 2 in a subdirectory with ngix - 301 redirect

I am having a really hard time getting nginx working with CakePHP in a subdirectory.
I'm using nginx because I am only familiar with Apache and I want to learn something new. My goal is to host CakePHP in a subdirectory.
My /etc/nginx/sites-available/default file looks like this:
server {
listen 0.0.0.0:80;
root /var/www/;
index index.html index.php;
include /etc/nginx/include/php;
error_page 404 = /404.html;
location / {
root index.html;
index index.php index.html index.htm;
autoindex on;
try_files $uri $uri/ /index.php;
}
location /randodb {
if (!-e $request_filename) {
rewrite ^/randodb(.+)$ /randodb/app/webroot/$1 last;
break;
}
}
location /randodb/app/webroot {
if (!-e $request_filename) {
rewrite ^/randodb/app/webroot/(.+)$ /randodb/app/webroot/index.php?url=$1 last;
break;
}
}
}
server {
listen 0.0.0.0:443;
root /var/www/;
index index.html index.php;
fastcgi_param HTTPS on;
include /etc/nginx/include/ssl;
include /etc/nginx/include/php;
error_page 404 = /404.html;
}
No matter what I do, any requests in the /randodb folder get a 301 redirect.
It always redirects me to http://nginx-php-fastcgi/randodb/
If you want to see this behavior in action: http://www.matgilbert.com/randodb
First this is the only block you need
location ~ /randodb(.*) {
root /var/www/randodb/app/webroot;
try_files $1 $1/ /index.php?url=$1;
}
above in the / block, you specified root index.html; what is this supposed to do, cause I believe this is incorrect, root is supposed to be a directory.
About this block
fastcgi_param HTTPS on;
include /etc/nginx/include/ssl;
include /etc/nginx/include/php;
is php working ? never seen it before, are u using something different than fast-cgi or fpm?
then the ssl block, here's my ssl block on my server
server {
listen 443 ssl;
ssl_certificate /etc/ssl/name.crt;
ssl_certificate_key /etc/ssl/name.key;
server_name example.com
location / {
# the rest of my config
}
}

Making pushState for backbone work in subpaths for nginx

I have two backbone applications and one API on the same subdomain but i am having trouble making pushStates work for the applications with correct location regex.
This is the paths im trying to accomplish
subdomain.domain.org/admin
subdomain.domain.org/client
subdomain.domain.org/api
I have tried to configure rewrite rules for client and admin but it always fail with 404 or 500 error. What would be the correct way to do this?
Here is just one example of all the combinations i have tried:
location ~ ^/admin.*$ {
alias /var/www/project/admin/public
rewrite ^(.+)$ /index.html last;
}
I figured it out after reading this https://serverfault.com/questions/361159/nginx-multiple-location-issues#361426
and ended up with this:
location ^~ /admin {
access_log /var/log/nginx/admin.access.log;
error_log /var/log/nginx/admin.error.log notice;
alias /var/www/project/admin/public/;
try_files $uri $uri/ /index.html;
rewrite ^/admin/(.+/)$ /admin/index.html last;
}
location ^~ /client {
access_log /var/log/nginx/client.access.log;
error_log /var/log/nginx/client.error.log notice;
alias /var/www/project/client/public/;
try_files $uri $uri/ /index.html;
rewrite ^/client/(.+/)$ /client/index.html last;
}

Resources