【发布时间】:2021-07-02 05:09:24
【问题描述】:
我使用下面源代码中的 GitHub 存储库来设置 Dockerfiles 和 docker-compose 并在其上进行构建。
它的工作原理是有一个 Nginx 反向代理根据 URL 将请求发送到 client(react) 或 backend(node js)。
这适用于单页 React 页面。我通过react-routes-dom 在一个反应中添加了多个页面。我像下面这样设置它,当我 npm start 反应代码并在 localhost:3000/path 访问时它可以工作。
function Main() {
return (
<Switch>
<Route path='/' exact component={ComponentA} />
<Route path='/path' exact component={ComponentB} />
</Switch>
);
}
当我尝试通过反向代理访问它时,就会出现问题。配置几乎与 repo default.conf 中的配置相同
当我尝试访问其他路由时会出现问题。
如果我尝试访问基本路径localhost。有用。
如果我尝试访问路径localhost/path,它不起作用。
访问基地/的日志
client | 172.18.0.5 - - [06/Apr/2021:11:51:15 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36" "-"
nginx | 172.18.0.1 - - [06/Apr/2021:11:51:15 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36" "-"
访问自定义/path的日志
nginx | 172.18.0.1 - - [06/Apr/2021:11:52:43 +0000] "GET /path HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36" "-"
client | 2021/04/06 11:52:43 [error] 31#31: *7 open() "/usr/share/nginx/html/path " failed (2: No such file or directory), client: 172.18.0.5, server: , request: "GET /path HTTP/1.0", host: "client"
client | 172.18.0.5 - - [06/Apr/2021:11:52:43 +0000] "GET /path HTTP/1.0" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36" "-"
我尝试修改React-router and nginx、https://gkedge.gitbooks.io/react-router-in-the-real/content/nginx.html、Nginx proxy_pass then try_file之后的conf文件。它们都不起作用。
我尝试将所有流量重定向到 / 以可能对路径有所帮助,但我得到一个空页面。
location / {
rewrite /(.*) / break;
proxy_pass http://client;
}
来源:https://github.com/LukeMwila/multi-container-nginx-react-node-mongo
【问题讨论】:
标签: nginx docker-compose react-router nginx-reverse-proxy