React router and nginx giving Unexpected Token < - reactjs

I have a react website which I am hosting inside a Docker container that is running Nginx, which I have running on Port 3000. I then have an instance of Nginx on my host machine, which I have a reverse proxy pointing to 127.0.0.1:3000.
Everything works fine, except I have a Twitter authentication call, which uses a callback. When the url points back to mysite.com/authenticated, this is asking Nginx for the path which of course falls over.
I have scoured the internet and have found many posts indicating I should have this in my nginx default file:
location / {
proxy_pass 127.0.0.1:3000;
try_files $uri $uri/ /index.html;
}
When I then navigate to my website, even the root, I am getting unexpected token < in the console errors.
What could be causing this issue?

Lets say you are calling external twitter api http://twitter/auth.com/twitter/authentication for authentication.
And in your react action file you have give url as /twitter/authentication to call.For this case following will be the configuration.
server {
location / {
proxy_pass http://127.0.0.1:3000;
}
location /twitter/authentication {
proxy_pass http://twitter/auth.com;
}
}
You have to write every location config inside server in nginx config.
After any modification in this config, you have restart nginx.

For me it was the link to app.js
<script src="/app/bundle.js"></script>
When I tried to reach my App at
http://localhost:8080/serv9090
the browser tried to find /app/bundle.js via
http://localhost:8080/app/bundle.js
Which is not found at localhost:8080 (nginx)
For a first try I changed
<script src="/app/bundle.js"></script>
to
<script src="http://localhost:8080/serv9090/app/bundle.js"></script>
This worked fine.
Hope this helped.

I solved this issue by adding the following into the nginx conf file:
location ~ .(static)/(js|css|media)/(.+)$ {
try_files $uri $uri/ /$1/$2/$3;
}

Related

Serve dynamic routes with nginx in a NextJS application

I use nginx as my static file server for my NextJS application, in order to test it in dev environment. My application follows a pattern in which I have, normaly, the following routes:
someRoute/
index.tsx
register.tsx
[id].tsx
My nginx.conf file has the following content:
worker_processes 4;
events { worker_connections 1024; }
http {
server {
listen 80;
root /usr/share/nginx/html;
include /etc/nginx/mime.types;
try_files $uri $uri/ /index.html /register.html;
}
}
What is happening is that, when I create a docker image of the application using nginx, if i try to access someRoute/register or someRoute/123, I always get the localhost/index page. someRoute/index works fine, but the other two don't.
My guess is that when I try to navigate to someRoute/123 nginx expects to find 123.html inside $uri/, which of course won't happen, since it is a dynamic route. As per register.html, I really don't know what's wrong.
How do I get someRoute/register and someRoute/[id] working?

Create react app served with nginx and oauth gives me refresh page issue

I have an app that is handled on server by https and it uses Oauth2 with tokens.
In react I have PathRouter which redirects to different pages
<Switch>
{/*the default is devices*/}
<Route exact path="/">
<Redirect to="/devices"/>
</Route>
and I have an nginx config
server {
listen 443 http2 ssl;
listen [::]:443 http2 ssl;
root /home/path-to-static;
index index.html;
server_name *****.com;
error_log /var/log/nginx/debug.log debug;
ssl_certificate /etc/nginx/certs/*****.crt;
ssl_certificate_key /etc/nginx/certs/*****.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
location / {
try_files $uri $uri/ #backend;
}
location #backend {
proxy_pass http://localhost:8080;
}
}
And it works!
The trouble starts when I want to refresh the page. Locally (on localhost) it works just fine, but when I refresh page on the server it won't let me and tells me that I am unauthorized.
I have a login screen where I enter my credentials and receive tokens and every request to the server I make with those tokens BUT my routing (I assume that) is the cornerstone that leads to the error- the inner routing happens without tokens.
Once again: it works fine until I refresh the page. Locally it works fine always.
That is the error that is provided on server
PS Maybe this is important: when I logined via login page and then I proceed to devices there is no devices request - as when I refresh the page via F5.
PS 2 I found a config when I am on some tab in my app and I clear all the uri but root and hit enter and it leads me to https://myapp.com/devices ! And it does it correct.
Ok, after reading tons of answers on SO I finally got it to work.
Firstly, a great answer from user #Panther here react.js application showing 404 not found in nginx server
When your react.js app loads, the routes are handled on the frontend
by the react-router. Say for example you are at http://a.com. Then on
the page you navigate to http://a.com/b. This route change is handled
in the browser itself. Now when you refresh or open the url
http://a.com/b in the a new tab, the request goes to your nginx where
the particular route does not exist and hence you get 404.
To avoid this, you need to load the root file(usually index.html) for
all non matching routes so that nginx sends the file and the route is
then handled by your react app on the browser.
So, as he recommended I've changed my config like
location / {
try_files $uri $uri/ /index.html #backend;
}
location #backend {
proxy_pass http://localhost:8080;
}
But this gave me some weird errors.
So I have to write location for every path I have, like this:
location / {
try_files $uri $uri/ #backend;
}
location /devices { // here my path is similar as in react
// router
try_files $uri $uri/ /index.html;
}

serve react-flask app with nginx tutorial doesn't work

I am following this tutorial in order to learn how to serve react and flask apps with one single nginx server.
I already reached the point in the tutorial where nginx should be serving the React app, but I still can only see default nginx page.
This is my nginx app configuration file /etc/nginx/sites-available/agroware:
server {
listen 80;
root /home/ubuntu/agroware/frontend/build;
index index.html;
location / {
try_files $uri $uri/ =404;
}
location /api {
include proxy_params;
proxy_pass http://localhost:5000;
}
}
I believe I am following the tutorial, but I cannot see the react app to this point.

Invalid path for js bundles in nginx served react app

So I have a SPA that I built using Create-React-App. It works completely fine for local dev, and I wanted to deploy the built app using nginx.
I set up my Dockerfile and nginx.conf, and at first all seemed to run fine. But then, I noticed weirdest issue.
Most of the app is working fine, but when I try to access a nested URL directly (i.e. not by navigating to it through the app -> This works fine. Problem happens only when I access the URL directly, or refresh the page), there are errors in the console/network while fetching the built files. Say I'm accessing localhost:3000/app/potato, I see my browser is trying to access files from localhost:3000/app/static/js/2.ebff4827.chunk.js (instead of localhost:3000/static/js/2 .ebff4827.chunk.js)
Accessing localhost:3000/app works fine, but localhost:3000/app/ (with trailing /) has the same weird behaviour.
As a result, the browser fails to load the page...
Here's my nginx.conf (although I don't believe this to cause my error, as I set it up with a blank CRA and it worked fine https://github.com/piaverous/cra-nginx-demo):
server {
listen 3000;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Does anyone have any clue on what could be causing the issue ? I'm totally at a loss here
So it turns out this problem came from my webpack build.
In my package.json, i had homepage: "./", which caused the path resolving issues.
Just removing this field seemed to fix this issue ! Nginx config was fine :)

CakePHP 3 on nginx not loading css and js files

I am trying to configure CakePHP 3 on OSX running nginx. The default page reports that everything is working except URL rewriting. However, I have URL rewriting working for page URLs (just followed the instructions for nginx setup). The only thing that's not working is loading static assets from webroot. I've been digging through a ton of similar stackoverflow questions and none of the responses seem to work.
vhost file:
server {
listen 80;
server_name albums.dev;
root /Users/username/Sites/albums;
access_log /Library/Logs/default.access.log main;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include /usr/local/etc/nginx/conf.d/php-fpm;
}
}
Again: The entire CakePHP application seems to be working correctly, except the static assets in /webroot/ are coming up as 404.
change the root .. you must point to webroot of your project
if you want i can give my conf to you

Resources