React-router和nginx

mar*_*tin 76 nginx react-router

我正在将我的反应应用程序从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" "-"
Run Code Online (Sandbox Code Playgroud)

我应该在哪里寻找解决方案?

我的路由器位反应看起来像这样:

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);
Run Code Online (Sandbox Code Playgroud)

我的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;
        }


    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

我知道大多数设置都有效,因为当我在没有登录的情况下访问localhost:8080时,我也会获得登录页面.这不是通过重定向到localhost:8080/login - 它是一些反应代码.

Tob*_*oby 182

nginx配置中的位置块应为:

location / {
  try_files $uri /index.html;
}
Run Code Online (Sandbox Code Playgroud)

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

  • 检查这一点以获得更高级的配置(缓存,404缺失.js,.css文件等)https://gkedge.gitbooks.io/react-router-in-the-real/content/nginx.html (6认同)
  • 如果 nginx 不将请求路由到 index.html 文件,那么 React Router 内的 JS 永远不会知道该请求。此答案将所有请求路由到 React,从而允许 React Router 处理所有请求。 (2认同)
  • 使用 package.json 中的默认主页设置也很重要,该设置使用服务器根目录。我已将其设置为“.”(使用相对路径),但随后 nginx 尝试提供 /custompath/static/main.js 服务,这显然会失败,给出一个无法工作的站点。 (2认同)
  • 请注意,确保您的配置实际上正在加载,我的配置与默认值冲突并且只是被跳过。叹 (2认同)
  • 确保在“/index.html”中包含前导“/”。我认为这是可选的,但实际上不是。 (2认同)

fuw*_*hin 13

这是我的解决方案

简单尝试:

location / {
    root /var/www/mysite;
    try_files $uri /index.html;
}
Run Code Online (Sandbox Code Playgroud)

不再尝试使用非html类型:

location / {
    root /var/www/mysite;
    set $fallback_file /index.html;
    if ($http_accept !~ text/html) {
        set $fallback_file /null;
    }
    try_files $uri $fallback_file;
}
Run Code Online (Sandbox Code Playgroud)

不再尝试使用非html类型和目录(使其try_filesindex和/或兼容autoindex):

location / {
    root /var/www/mysite;
    index index.html;
    autoindex on;
    set $fallback_file /index.html;
    if ($http_accept !~ text/html) {
        set $fallback_file /null;
    }
    if ($uri ~ /$) {
        set $fallback_file /null;
    }
    try_files $uri $fallback_file;
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*lig 12

也许这不是这里的确切情况,但对于使用anddocker为他们的react项目运行容器的任何人来说,在我的情况下,我在我的 中有我需要的一切:react-routerwebpack-dev-servernginx.conf

server {
    listen 80;
    location / {
      root   /usr/share/nginx/html;
      index  index.html index.htm;
      try_files $uri $uri/ /index.html;
    } 
    error_page 404 /index.html;
    location = / {
      root /usr/share/nginx/html;
      internal;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
      root   /usr/share/nginx/html;
    }
  }
Run Code Online (Sandbox Code Playgroud)

但是 nginx 仍然不会向 root 发送 404 错误/。在查看容器后,我注意到有一些配置/etc/nginx/conf.d/default.conf会以某种方式覆盖我的配置,因此在我的配置中删除该文件dockerfile解决了问题:

...
FROM nginx:alpine
COPY --from=build /app/build /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf  # <= This line solved my issue
COPY nginx.conf /etc/nginx/conf.d
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Run Code Online (Sandbox Code Playgroud)

  • 你还可以做的是将你的 Nginx.conf 复制到 default.conf `COPY nginx.conf /etc/nginx/conf.d/default.conf` 从而替换它。无需 RUN rm 命令 (2认同)

Nit*_*hav 7

如果您从子目录提供服务(例如http://localhost:8080/jobs/):

使用您的 / 块如下:

server {
    listen              8080;
    server_name         localhost;
    absolute_redirect   off;
    root                /usr/share/nginx/html;
    index               index.html;

    location / {
      expires         5m;
      add_header      Cache-Control "public";
      try_files $uri $uri/ /jobs/index.html;
    }

}
Run Code Online (Sandbox Code Playgroud)


Fat*_*ora 5

这是在实时服务器中对我有用的解决方案:

我刚刚遵循了本教程,并且使用基本的 Nginx 配置只是简单地配置了位置块。

location / {
    try_files $uri $uri/ /index.html;
}
Run Code Online (Sandbox Code Playgroud)


Jay*_* Lu 5

nginx 配置中的位置块应该是这样的

location / {
       root   /path/to/root/dir;
       index  index.html index.htm;
       try_files $uri $uri/ /index.html;
Run Code Online (Sandbox Code Playgroud)

将所有请求转发到index.html(您的React SPA APP)。

package.json您的 React 应用程序项目中,添加"homepage":"http://your.website.go"以使嵌套路径转发到正确的静态文件。

{
  "name": "React App",
  "version": "0.0.0",
  "homepage": "http://your.website.go",
  "dependencies": {
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-router-dom": "^6.1.1",
    "react-scripts": "5.0.0",
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  }
}
Run Code Online (Sandbox Code Playgroud)