React-router和nginx

前端之家收集整理的这篇文章主要介绍了React-router和nginx前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在将我的反应应用程序从webpack-dev-server转换为Nginx

当我转到根网址“localhost:8080 / login”时,我只得到一个404,在我的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);

我的Nginx文件是这样的:

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 – 它是一些反应代码

Nginx配置中的位置块应为:
location / {
  try_files $uri /index.html;
}

问题是对index.html文件的请求有效,但您目前还没有告诉Nginx将其他请求转发到index.html文件

原文链接:https://www.f2er.com/react/301349.html

猜你在找的React相关文章