I have been stuck on this for days now and I need the internet's help.
I have created a static HTML landing page project for my react application (These two projects are separate), and I wish to serve both of these using Nginx. My landing page project has its own directory structure and assets that look like so:
/homepage
--index.html
--contact.html
--about-us.html
/static
--css files, imgs, ect
My React application is a Create React Application and from what I understand, after you run npm build that gives you a folder called build
with all your project files properly bundled so that you can deploy it. Now I have two directories with all I need to serve the websites but I'm having a hard time doing so using Nginx.
I would like to have my landing page on / so www.example.com and www.example.com/contact , ect would lead to my landing page project and then www.example.com/app, with www.example.com/app/login, ect would lead to my react project.
My Nginx config looks like this:
# redirect to https
server {
listen 80;
server_name _;
return 301 https://$host$request_uri;
}
server{
listen 443 ssl;
ssl_certificate /etc/nginx/certs/localhost.pem;
ssl_certificate_key /etc/nginx/certs/localhost-key.pem;
proxy_cookie_path / "/; HTTPOnly; Secure";
location / {
root /usr/share/nginx/html/homepage/;
try_files $uri.html $uri $uri/ =404;
}
location /app/ {
alias /usr/share/nginx/html/app/;
try_files $uri $uri/ /index.html;
}
location /api/ {
proxy_pass http://backend:8080/;
}
}
I can properly see my react apps home page, but when I navigate to any other page such as https://localhost/app/test I get sent to a broken landing page's index.html (where all the links / styling are broken). So, the link https://localhost/app directs me to my react app's index.html but when I go to https://localhost/app/test is broken and it gives me the landing pages's broken index.html
How can I properly configure nginx to serve my react app's other routes alongside my landing page project?
EDIT:
For more information I added the fact that I am using React router for my react application and my index.tsx looks like so:
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import * as serviceWorker from "./serviceWorker";
import { Route, BrowserRouter as Router, Switch } from "react-router-dom";
import Home from "./components/Home";
import Login from "./components/Login/Login";
import Dashboard from "./components/Dashboard";
import SignupPage from "./components/Signup/SignupPage";
import DashNavbar from "./components/DashNavbar";
import PersonCard from "./components/PersonCard/PersonCard";
import AnalyticsSmallCard from "./components/AnalyticsSmallCard";
import Test2 from "./components/Test2";
import Test from "./components/Test";
import PrivateRoute from "./components/PrivateRoute";
import NotesSideBar from "./components/NotesSideBar";
import NotesPage from "./components/NotesPage";
import WarningCard from "./components/WarningCard";
const App = (
<Router basename="/app">
<Switch>
<Route path="/" component={Home} />
<Route path="/login" component={Login} />
<Route path="/signup" component={SignupPage} />
<PrivateRoute exact path="/dashboard" component={Dashboard} />
<Route path="/test" component={WarningCard} />
</Switch>
</Router>
);
ReactDOM.render(App, document.getElementById("root"));
serviceWorker.unregister();