【发布时间】:2018-08-21 09:56:00
【问题描述】:
我正在使用"react-router-dom": "^4.2.2"。
如果我在 localhost:3000/second 上进行测试,它会完美运行。
当我使用 nginx 在 ubuntu 服务器上上传它并尝试 www.website.com 时,它可以工作。当我尝试使用www.website.com/second 时,它给了我404 not found。我正在使用create-react-app。
app.js
class TestRoutes extends React.Component{
constructor(props){
super(props);
}
render(){
return(<React.Fragment>
<BrowserRouter>
<Switch>
<Route exact path='/' component={MainPage}/>
<Route path='/second' component={SecondPage}/>
<Route path='/third' component={ThirdPage}/>
</Switch>
</BrowserRouter>
</React.Fragment>);
}
}
ReactDOM.render(<TestRoutes/>, document.getElementById("root"));
/etc/nginx/sites-available/default 这是来自服务器的配置文件
server {
listen 443 ssl;
root /var/www/reactcamera/build;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name website.com www.website.com;
ssl_certificate /etc/letsencrypt/live/website.com/fullchain.pem; #
managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/website.com/privkey.pem; #
managed by Certbot
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
【问题讨论】:
-
这里有很多可能出错的地方。你有重定向到 create-react-app 在 yarn build 上创建的 dist 目录吗?你建了吗?等等……
-
是的,我从 git 克隆了它并做了
npm install和npm run build。 -
你能显示你的 nginx 配置文件吗
-
nginx.conf?因此,当您转到 URL 时,它会指向某些东西吗?
-
在原帖中添加了配置文件
标签: reactjs nginx react-router create-react-app