简体   繁体   English

动态路线在Caddy中无法在React中使用

[英]Dynamic routes not working in React with Caddy

I have a React Application. 我有一个React应用程序。 I am using some routes that receives parameters in the URL (by GET), for example myapp.com/products/1 that works pretty good in local enviroment but no in my CentOS server. 我正在使用一些通过URL接收参数(通过GET)的路由,例如myapp.com/products/1在本地环境中运行得很好,但在我的CentOS服务器中却没有。 I have to mention that I am using npm run build to generate the static files. 我不得不提到,我正在使用npm run build生成静态文件。

For other side, I have running Caddy as HTTP server in the CentOS server, there I have problem only with the routes that includes parameters, so now am wondering if caddy have something to be with this issue. 另一方面,我在CentOS服务器中将Caddy作为HTTP服务器运行,那里的问题仅在于包含参数的路由,因此现在想知道caddy是否与该问题有关。

Here the React people say: 这里的React人们说:

If you're using Apache, you need to create a .htaccess file in the public folder that looks like this: 如果使用的是Apache,则需要在公用文件夹中创建一个.htaccess文件,如下所示:

 Options -MultiViews
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.html [QSA,L]

So, how can I do that in Caddy. 所以,我该如何在Caddy中做到这一点。 I have currently these lines for the site: 我目前在网站上有以下几行:

mysite.com {
   root PathToProject/build/
   log logs/pagelog.log
}

What is missing in order to can use parameters in the URL? 可以使用URL中的参数缺少什么?

I have this config in my Caddyfile 我的Caddyfile中有此配置

rewrite {
  if    {path} not_match ^\/0.0.0.0
  to    {path} {path}/ /?_url={uri}
}

See if that helps, it took care of my routing issue (similar to what u have mentioned) for React App. 看看是否有帮助,它解决了我的React App路由问题(类似于您提到的问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM