如何使用 Nginx 从子路径为 React 提供服务
我的设置是一个由 Gunicorn 和 Nginx 提供服务的 Django 应用程序。我一次添加 React 一页,因此我只需要从 React 提供特定页面。为了更容易使用 Nginx 进行路由,我决定让每个 React 路由都以 /v2/
开头。
因此,Nginx conf 文件现在看起来像这样:
upstream backend_server {
server backend:8000;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com *.example.com;
deny 143.198.76.27;
location /v2/ {
alias /usr/share/nginx/html;
try_files $uri /index.html;
}
location / {
proxy_pass http://backend_server$request_uri;
....
}
}
在 package.json
中,我添加了主页:
{
"name": "frontend",
"homepage": "/v2",
...
}
我还尝试调整我的 BrowserRouter
的 basename:
ReactDOM.render(
<Provider store={store}>
<BrowserRouter basename="/v2">
<App />
</BrowserRouter>
</Provider>,
document.getElementById("root")
);
出于测试目的,我制定了 3 条路线:
function App() {
return (
<Routes>
<Route path="/" element={<CounterPage/>}/>
<Route path="/v2" element={<CounterPage/>}/>
<Route path="/v2/test" element={<CounterPage/>}/>
</Routes>
);
}
我的 Docker 映像的 nginx 构建过程如下所示(并且文件已验证存在):
FROM ghcr.io/digibrainllc/example/frontend:latest as build
# Create production build
RUN npm run build
# Production Nginx image with frontend build files
# to be served by Nginx
FROM nginx:1.21-alpine
# Copy the frontend build files over to the directory
# which Nginx serves from
COPY --from=build /frontend/build /usr/share/nginx/html
# Remove the default Nginx settings
RUN rm /etc/nginx/conf.d/default.conf
COPY /nginx/conf/nginx.prod.conf /etc/nginx/conf.d
example.com/v2
:
404 未找到
我需要为 React 提供服务的所有 example.com/v2
路由,并将其余路由路由到 Django。
我怎样才能做到这一点?
My setup is a Django application served with gunicorn and Nginx. I am adding React one page at a time so therefore I need to only serve specific pages from React. To make it easier to route using Nginx I decided to make every React route start with /v2/
.
Therefore the Nginx conf file looks like this right now:
upstream backend_server {
server backend:8000;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com *.example.com;
deny 143.198.76.27;
location /v2/ {
alias /usr/share/nginx/html;
try_files $uri /index.html;
}
location / {
proxy_pass http://backend_server$request_uri;
....
}
}
In the package.json
I added the homepage:
{
"name": "frontend",
"homepage": "/v2",
...
}
I have also tried adjusting my BrowserRouter
's basename
:
ReactDOM.render(
<Provider store={store}>
<BrowserRouter basename="/v2">
<App />
</BrowserRouter>
</Provider>,
document.getElementById("root")
);
And for testing purposes I have made 3 routes:
function App() {
return (
<Routes>
<Route path="/" element={<CounterPage/>}/>
<Route path="/v2" element={<CounterPage/>}/>
<Route path="/v2/test" element={<CounterPage/>}/>
</Routes>
);
}
My nginx build process for the Docker image looks like this (and the files are verified to be there):
FROM ghcr.io/digibrainllc/example/frontend:latest as build
# Create production build
RUN npm run build
# Production Nginx image with frontend build files
# to be served by Nginx
FROM nginx:1.21-alpine
# Copy the frontend build files over to the directory
# which Nginx serves from
COPY --from=build /frontend/build /usr/share/nginx/html
# Remove the default Nginx settings
RUN rm /etc/nginx/conf.d/default.conf
COPY /nginx/conf/nginx.prod.conf /etc/nginx/conf.d
example.com/v2
:
404 Not Found
I need all example.com/v2
routes serving React and the rest routed to Django.
How can I accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我成功了。为了更仔细地检查,我查看了 Nginx 日志:
docker logs [nginx_id]
。在调试过程中,我删除了
别名
并向主服务器块添加了root
。然后 nginx 日志显示路径为
/usr/share/nginx/html/v2/
,但未找到该文件。因此,我登录到 nginx 容器docker exec -it [nginx_id] sh
并将构建文件从/usr/share/nginx/html
复制到/usr/共享/nginx/html/v2
。这使得文件被发现......所以更改是:
frontend/build
文件复制到usr/share/nginx/html /v2 代替。
Nginx.conf:
Nginx Dockerfile:
我知道这会很简单 XD
Ok I got it working. To examine more closely I looked at the Nginx logs:
docker logs [nginx_id]
.During debugging I removed the
alias
and added aroot
to the main server block.Then the nginx logs said the path was
/usr/share/nginx/html/v2/
where the file was not found. So I logged into the nginx containerdocker exec -it [nginx_id] sh
and copied the build files from/usr/share/nginx/html
to/usr/share/nginx/html/v2
. This allowed the files to be discovered...So the changes were to:
frontend/build
files tousr/share/nginx/html/v2
instead.Nginx.conf:
Nginx Dockerfile:
I knew it would be something simple XD