当前位置: 首页 > 知识库问答 >
问题:

React-router和nginx

段干恺
2023-03-14

我正在将react应用程序从webpack-dev-server过渡到nginx。

my-nginx-container | 2017/05/12 21:07:01 [error] 6#6: *11 open() "/wwwroot/login" failed (2: No such file or directory), client: 172.20.0.1, server: , request: "GET /login HTTP/1.1", host: "localhost:8080"
my-nginx-container | 172.20.0.1 - - [12/May/2017:21:07:01 +0000] "GET /login HTTP/1.1" 404 169 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:53.0) Gecko/20100101 Firefox/53.0" "-"
render(

  <Provider store={store}>
    <MuiThemeProvider>
      <BrowserRouter history={history}>
        <div>
          Hello there p
          <Route path="/login" component={Login} />
          <App>

            <Route path="/albums" component={Albums}/>

            <Photos>
              <Route path="/photos" component={SearchPhotos}/>
            </Photos>
            <div></div>
            <Catalogs>
              <Route path="/catalogs/list" component={CatalogList}/>
              <Route path="/catalogs/new" component={NewCatalog}/>
              <Route path="/catalogs/:id/photos/" component={CatalogPhotos}/>
              <Route path="/catalogs/:id/photos/:photoId/card" component={PhotoCard}/>
            </Catalogs>
          </App>
        </div>
      </BrowserRouter>
    </MuiThemeProvider>
  </Provider>, app);
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen 8080;
        root /wwwroot;

        location / {
            root /wwwroot;
            index index.html;

            try_files $uri $uri/ /wwwroot/index.html;
        }


    }
}

编辑:

我知道大多数设置都可以工作,因为当我在没有登录的情况下转到localhost:8080时,我也会得到登录页面。这不是通过重定向到localhost:8080/login,而是一些react代码。

共有1个答案

赵驰
2023-03-14

nginx配置中的位置块应该是:

location / {
  try_files $uri /index.html;
}

问题是,对index.html文件的请求可以工作,但您目前没有告诉nginx将其他请求也转发到index.html文件。

 类似资料:
  • 问题内容: 使用 withRouter() 时如何获取路由上下文,位置,参数等? 您可以使用withRouter获得该信息,还是必须将这些内容显式传递到组件树中? 问题答案: 因此,不再使用。在道具中都可以使用: 因此,假设您想将用户转到新路线,您只需

  • React Router 一个用于 React.js 的路由解决方案(routing solution)。它轻松可以同步你的 app 和 URL,同时给嵌套,转换,和服务端渲染一流的支持。 特性: 嵌套视图映射到嵌套路由 模块化构建路由层级 同步和异步转移挂钩 转换中止/重定向/重试 动态 segments 查询参数 当 .active 类的路由活动时可以自动链接 Multiple root r

  • 本文向大家介绍React-Router的` `标签和` `标签有什么区别?相关面试题,主要包含被问及React-Router的` `标签和` `标签有什么区别?时的应答技巧和注意事项,需要的朋友参考一下 Link 组件最终会渲染为 HTML 标签 ,它的 to、query、hash 属性会被组合在一起并渲染为 href 属性。虽然 Link 被渲染为超链接,但在内部实现上使用脚本拦截了浏览器的默认

  • koa-react-router koa 2 middleware for React server side rendering and routing with react-router 5. Looking for React Router 3 support see v1 docs.Try React Router 5 though it's awesome! Usage To insta

  • useReactRouter useReactRouter is a React Hook that provides pub-sub behavior for react-router.Unlike the withRouter Higher-Order Component, useReactRouter will re-render your component when the locati

  • 现在你想在你的 Redux 应用中使用路由功能,可以搭配使用 React Router 来实现。 Redux 和 React Router 将分别成为你数据和 URL 的事实来源(the source of truth)。 在大多数情况下, 最好 将他们分开,除非你需要时光旅行和回放 action 来触发 URL 改变。 安装 React Router 可以使用 npm 来安装 react-rou