我有一个reactjs应用程序,我想使用nginx为它提供服务。当我使用npm run start
运行它时,一切都很棒。但是:
使用npm run build
构建并使用live-server
进行服务后,首页按预期运行,但其他任何路由均返回cannot GET /the/route
。
使用nginx服务后,第一页与上面相同,但其他任何路由都显示空白页。
我没有使用webpack。
这是我的router.js
。
import React, { Component } from 'react';
import { BrowserRouter, Route } from 'react-router-dom';
import AdminLogin from './components/admin/Login';
import AdminVerify from './components/admin/Verify';
import Complete from './components/admin/Complete';
import Phonesignup from './components/user/Signup';
import Codesignup from './components/user/Verify';
import Completeinfo from './components/user/Completeinfo';
import Side from './components/layout/sidebar/sidebar'
class Router extends Component {
render() {
return (
<BrowserRouter>
<Route path="/" component={Side} exact />
<Route path="/admin/login" component={AdminLogin} exact />
<Route path="/admin/verify" component={AdminVerify} exact />
<Route path="/admin/login/complete" component={Complete} exact />
<Route path="/user/signup" component={Phonesignup} exact />
<Route path="/user/complete" component={Completeinfo} exact />
<Route path="/user/verify" component={Codesignup} exact />
</BrowserRouter>
)
}
}
export default Router;
这是我的App.js。
import React, { Component } from 'react';
import ReduxThunk from 'redux-thunk';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware, compose } from 'redux';
import reducers from './redux/reducers';
import Router from './Router';
import './assets/css/global/index.css';
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
const middleware = [ReduxThunk]
class App extends Component {
render() {
const store = createStore(reducers, composeEnhancers(applyMiddleware(...middleware)));
return (
<Provider store={store}>
<Router />
</Provider>
)
}
}
export default App;
这是我的Nginx配置。
server {
listen 80;
server_name my.site.com;
location / {
root /home/app/frontend;
try_files $uri /index.html;
index index.html index.htm;
}
}
感谢您提供任何有用的答案。