Ngnix反向代理react-router配置问题解决方法
2016-11-15 17:11
337 查看
项目以react router实现,用ngnix做反向代理的时候出现404找不到页面,有两种解决方法。
第一种 将<Route path="*" component={NotFound} />对应的component改为<IndexRoute>对应的component,如下:
const Routes = ({ history }) =>
<Router history={history}>
<Route path="/" component={Index}>
<IndexRoute component={DefaultPage}/>
<Route path="/lc" component={DefaultPage} />
<Route path="/Icon" component={Icon} />
<Route path="/ECharts" component={ECharts} />
<Route path="/page1" component={Page1} />
<Route path="/page3" component={Index} />
<Route path="/ruleJson" component={ruleJson} />
<Route path="/rules" component={rules} />
<Route path="/dataSource" component={DataSource}/>
<Route path="/viewTable" component={ViewTable}/>
<Route path="/sourceMatch" component={SourceMatch}/>
<Route path="/addMatchRules" component={AddMatchRules}/>
<Route path="seeMatchRules" component={SeeMatchRules}/>
<Route path="projectSourceUse" component={ProjectSourceUse}/>
<Route path="kafkaConfig" component={KafkaConfig}/>
<Route path="dataLocate" component={DataLocate}/>
<Route path="configurationEnvironment" component={ConfigurationEnvironment}/>
<Route path="mainframeInfo" component={MainframeInfo}/>
<Route path="resInfo" component={ResInfo}/>
<Route path="askForRes" component={AskForRes}/>
<Route path="releaseTask" component={ReleaseTask}/>
<Route path="taskList" component={TaskList}/>
<Route path="taskList" component={TaskList}/>
<Route path="taskList" component={TaskList}/>
<Route path="*" component={DefaultPage} />
</Route>
</Router>
此时 Ngnix服务端配置如下
server {
listen 80;
server_name localhost;
proxy_redirect http://localhost:3083/ /;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://10.248.26.202; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /pt/ {
proxy_pass http://10.248.26.202:3083; rewrite "^/pt(.*)$" $1 break;
}
location /help/ {
proxy_pass http://10.248.26.202/help; }
location ~^/lc/ {
proxy_pass http://localhost:8080; rewrite "^/lc(.*)$" $1 break;
}
}
此时代理成功 但是字体加载不到 根据404请求的地址所以添加一个字体的规则
location ^~/fonts/{
rewrite "^(.*)/fonts(.*)$" $1/lc/fonts$2;
}
第二种方法
在路由中添加一条与uri对应的路径指向首页 如下
const Routes = ({ history }) =>
<Router history={history}>
<Route path="/" component={Index}>
<IndexRoute component={DefaultPage}/>
<Route path="/lc" component={DefaultPage} />
<Route path="/Icon" component={Icon} />
<Route path="/ECharts" component={ECharts} />
<Route path="/page1" component={Page1} />
<Route path="/page3" component={Index} />
<Route path="/ruleJson" component={ruleJson} />
<Route path="/rules" component={rules} />
<Route path="/dataSource" component={DataSource}/>
<Route path="/viewTable" component={ViewTable}/>
<Route path="/sourceMatch" component={SourceMatch}/>
<Route path="/addMatchRules" component={AddMatchRules}/>
<Route path="seeMatchRules" component={SeeMatchRules}/>
<Route path="projectSourceUse" component={ProjectSourceUse}/>
<Route path="kafkaConfig" component={KafkaConfig}/>
<Route path="dataLocate" component={DataLocate}/>
<Route path="configurationEnvironment" component={ConfigurationEnvironment}/>
<Route path="mainframeInfo" component={MainframeInfo}/>
<Route path="resInfo" component={ResInfo}/>
<Route path="askForRes" component={AskForRes}/>
<Route path="releaseTask" component={ReleaseTask}/>
<Route path="taskList" component={TaskList}/>
<Route path="taskList" component={TaskList}/>
<Route path="taskList" component={TaskList}/>
<Route path="*" component={DefaultPage} />
</Route>
</Router>
此时ngnix服务器端的配置如下
server {
listen 80;
server_name localhost;
proxy_redirect http://localhost:3083/ /;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://10.248.26.202; }
location /pt/ {
proxy_pass http://10.248.26.202:3083; rewrite "^/pt(.*)$" $1 break;
}
location /help/ {
proxy_pass http://10.248.26.202/help; }
location ~^/lc/ {
proxy_pass http://localhost:8080; rewrite "^/lc(.*)$" $1 break;
}
第一种 将<Route path="*" component={NotFound} />对应的component改为<IndexRoute>对应的component,如下:
const Routes = ({ history }) =>
<Router history={history}>
<Route path="/" component={Index}>
<IndexRoute component={DefaultPage}/>
<Route path="/lc" component={DefaultPage} />
<Route path="/Icon" component={Icon} />
<Route path="/ECharts" component={ECharts} />
<Route path="/page1" component={Page1} />
<Route path="/page3" component={Index} />
<Route path="/ruleJson" component={ruleJson} />
<Route path="/rules" component={rules} />
<Route path="/dataSource" component={DataSource}/>
<Route path="/viewTable" component={ViewTable}/>
<Route path="/sourceMatch" component={SourceMatch}/>
<Route path="/addMatchRules" component={AddMatchRules}/>
<Route path="seeMatchRules" component={SeeMatchRules}/>
<Route path="projectSourceUse" component={ProjectSourceUse}/>
<Route path="kafkaConfig" component={KafkaConfig}/>
<Route path="dataLocate" component={DataLocate}/>
<Route path="configurationEnvironment" component={ConfigurationEnvironment}/>
<Route path="mainframeInfo" component={MainframeInfo}/>
<Route path="resInfo" component={ResInfo}/>
<Route path="askForRes" component={AskForRes}/>
<Route path="releaseTask" component={ReleaseTask}/>
<Route path="taskList" component={TaskList}/>
<Route path="taskList" component={TaskList}/>
<Route path="taskList" component={TaskList}/>
<Route path="*" component={DefaultPage} />
</Route>
</Router>
此时 Ngnix服务端配置如下
server {
listen 80;
server_name localhost;
proxy_redirect http://localhost:3083/ /;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://10.248.26.202; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /pt/ {
proxy_pass http://10.248.26.202:3083; rewrite "^/pt(.*)$" $1 break;
}
location /help/ {
proxy_pass http://10.248.26.202/help; }
location ~^/lc/ {
proxy_pass http://localhost:8080; rewrite "^/lc(.*)$" $1 break;
}
}
此时代理成功 但是字体加载不到 根据404请求的地址所以添加一个字体的规则
location ^~/fonts/{
rewrite "^(.*)/fonts(.*)$" $1/lc/fonts$2;
}
第二种方法
在路由中添加一条与uri对应的路径指向首页 如下
const Routes = ({ history }) =>
<Router history={history}>
<Route path="/" component={Index}>
<IndexRoute component={DefaultPage}/>
<Route path="/lc" component={DefaultPage} />
<Route path="/Icon" component={Icon} />
<Route path="/ECharts" component={ECharts} />
<Route path="/page1" component={Page1} />
<Route path="/page3" component={Index} />
<Route path="/ruleJson" component={ruleJson} />
<Route path="/rules" component={rules} />
<Route path="/dataSource" component={DataSource}/>
<Route path="/viewTable" component={ViewTable}/>
<Route path="/sourceMatch" component={SourceMatch}/>
<Route path="/addMatchRules" component={AddMatchRules}/>
<Route path="seeMatchRules" component={SeeMatchRules}/>
<Route path="projectSourceUse" component={ProjectSourceUse}/>
<Route path="kafkaConfig" component={KafkaConfig}/>
<Route path="dataLocate" component={DataLocate}/>
<Route path="configurationEnvironment" component={ConfigurationEnvironment}/>
<Route path="mainframeInfo" component={MainframeInfo}/>
<Route path="resInfo" component={ResInfo}/>
<Route path="askForRes" component={AskForRes}/>
<Route path="releaseTask" component={ReleaseTask}/>
<Route path="taskList" component={TaskList}/>
<Route path="taskList" component={TaskList}/>
<Route path="taskList" component={TaskList}/>
<Route path="*" component={DefaultPage} />
</Route>
</Router>
此时ngnix服务器端的配置如下
server {
listen 80;
server_name localhost;
proxy_redirect http://localhost:3083/ /;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://10.248.26.202; }
location /pt/ {
proxy_pass http://10.248.26.202:3083; rewrite "^/pt(.*)$" $1 break;
}
location /help/ {
proxy_pass http://10.248.26.202/help; }
location ~^/lc/ {
proxy_pass http://localhost:8080; rewrite "^/lc(.*)$" $1 break;
}
相关文章推荐
- nginx配置反向代理解决前后端分离跨域问题
- nginx反向代理多个tomcat 配置.解决因404问题js css无法加载问题
- 配置nginx反向代理服务器,解决浏览器跨域调用接口的限制问题
- ReactJS & dva 项目常见报错问题与解决方法 (react-router 4)
- react-router browserHistory刷新页面404问题解决方法
- nginx配置反向代理解决前后端分离跨域问题
- react-router与react-reduct配合使用时页面不刷新问题解决方法
- nginx配置反向代理解决前后端分离跨域问题
- Nginx 的使用配置并利用nginx 反向代理解决跨域问题
- Windows10下启动nginx反向代理服务器失败的解决方法(重在解决:80端口被PID为4的System占用问题)
- 解决Linux下ArcGIS Server的Tomcat不稳定问题,nginx配置反向代理时的一个小问题
- [原创]SQL Server 2008 安装过程中遇到“性能计数器注册表配置单元一致性”检查失败 问题的解决方法
- phpMyAdmin安装配置方法及问题解决
- 在Resin3下配置SpringSide的HelloWorld示例程序碰到的问题和解决方法(说白了还是resin3.0解析xml问题)
- phpMyAdmin 安装配置方法和问题解决
- SQL Server 2008 安装过程中遇到“性能计数器注册表配置单元一致性”检查失败 问题的解决方法
- Weblogic8.1配置log4j问题及解决方法。
- Apache配置--同一台机器上两个应用需要同时占用80端口的问题解决方法
- 配置wine使迅雷5稳定运行的详细方法,解决下载到99.9%终止的问题