我正在尝试使用基本的 nginx 反向代理来访问三个不同的应用程序,每个应用程序都在 docker 容器内运行。nginx 反向代理已启用http://127.0.0.1:8080
,我希望实现以下行为:
http://127.0.0.1:8080/api
路由到 Django 后端 apihttp://127.0.0.1:8000/api
http://127.0.0.1:8080/
路由到正在运行的主 React 应用程序http://127.0.0.1:5200
http://127.0.0.1:8080/app2
路由到正在运行的第二个应用程序http://127.0.0.1:5300
这是nginx.conf
events {}
http {
server {
listen 8080;
server_name 127.0.0.1;
rewrite_log on;
error_log /var/log/nginx/error.log notice;
# debug with cache off
add_header Cache-Control no-cache;
location / {
proxy_pass http://127.0.0.1:5200;
proxy_set_header Host 127.0.0.1:5200;
}
location /api {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host 127.0.0.1:8000;
}
location = /app2 {
return 302 /app2/;
}
location /app2/ {
proxy_pass http://127.0.0.1:5300/;
proxy_set_header Host 127.0.0.1:5300;
}
if ($http_referer ~ https?://[^/]+/app2/(.*))
{
# rewrite request URI only if it isn't already started with '/app2' prefix
rewrite ^((?!/app2).*) /app2$1;
}
}
}
如果我在第二个应用程序中使用这些路由,则http://127.0.0.1:8080/app2
不会加载页面,并且我会从 nginx 反向代理获取以下日志
<BrowserRouter>
<Routes>
<Route path="/" element={<Comp />} />
<Route path="/test" element={<Comp2 />} />
</Routes>
</BrowserRouter>
127.0.0.1 - - [16/Sep/2023:22:47:25 +0000] "GET /app2 HTTP/1.1" 302 145 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
127.0.0.1 - - [16/Sep/2023:22:47:25 +0000] "GET /app2/ HTTP/1.1" 200 1052 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
2023/09/16 22:47:25 [warn] 29#29: *1 an upstream response is buffered to a temporary file /var/cache/nginx/proxy_temp/1/00/0000000001 while reading upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET /app2/main.js HTTP/1.1", upstream: "http://127.0.0.1:5300/main.js", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
127.0.0.1 - - [16/Sep/2023:22:47:25 +0000] "GET /app2/main.js HTTP/1.1" 200 9778028 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
我为第二个应用添加了一条捕获所有路由以进行测试
<BrowserRouter>
<Routes>
<Route path="/" element={<Comp2 />} />
<Route path="/aaa" element={<Comp2 />} />
<Route path="/bbb" element={<Comp />} />
<Route path="*" element={<Comp />} />
</Routes>
</BrowserRouter>
这次,页面加载,但显示 app2 的所有路径Comp
,捕获所有规则优先。这是当我尝试通过反向代理访问时http://127.0.0.1:8080/app2
,但当直接在其服务器上访问第二个应用程序时http://127.0.0.1:5300
,路径加载了正确的组件
这是来自 1/ 反向代理和 2/ app2 nginx 服务器的日志,当访问http://127.0.0.1:8080/app2
1/ nginx 反向代理时
2023/09/22 06:19:32 [notice] 29#29: *12 "https?://[^/]+/app2/(.*)" does not match "", client: 127.0.0.1, server: 127.0.0.1, request: "GET /app2/ HTTP/1.1", host: "127.0.0.1:8080"
127.0.0.1 - - [22/Sep/2023:06:19:32 +0000] "GET /app2/ HTTP/1.1" 200 1051 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
2023/09/22 06:19:32 [notice] 29#29: *12 "https?://[^/]+/app2/(.*)" matches "http://127.0.0.1:8080/app2/", client: 127.0.0.1, server: 127.0.0.1, request: "GET /app2/main.js HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:32 [notice] 29#29: *12 "^((?!/app2).*)" does not match "/app2/main.js", client: 127.0.0.1, server: 127.0.0.1, request: "GET /app2/main.js HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:32 [warn] 29#29: *12 an upstream response is buffered to a temporary file /var/cache/nginx/proxy_temp/2/00/0000000002 while reading upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET /app2/main.js HTTP/1.1", upstream: "http://127.0.0.1:5300/main.js", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
127.0.0.1 - - [22/Sep/2023:06:19:32 +0000] "GET /app2/main.js HTTP/1.1" 200 9875574 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
2023/09/22 06:19:33 [notice] 29#29: *12 "https?://[^/]+/app2/(.*)" matches "http://127.0.0.1:8080/app2/", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/faces/face_1.jpg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *12 "^((?!/app2).*)" matches "/assets/img/faces/face_1.jpg", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/faces/face_1.jpg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *12 rewritten data: "/app2/assets/img/faces/face_1.jpg", args: "", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/faces/face_1.jpg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *11 "https?://[^/]+/app2/(.*)" matches "http://127.0.0.1:8080/app2/", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/faces/face_4.jpg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *11 "^((?!/app2).*)" matches "/assets/img/faces/face_4.jpg", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/faces/face_4.jpg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *11 rewritten data: "/app2/assets/img/faces/face_4.jpg", args: "", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/faces/face_4.jpg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
127.0.0.1 - - [22/Sep/2023:06:19:33 +0000] "GET /assets/img/faces/face_1.jpg HTTP/1.1" 200 64369 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
2023/09/22 06:19:33 [notice] 29#29: *1 "https?://[^/]+/app2/(.*)" matches "http://127.0.0.1:8080/app2/", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/faces/face_3.jpg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *1 "^((?!/app2).*)" matches "/assets/img/faces/face_3.jpg", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/faces/face_3.jpg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *1 rewritten data: "/app2/assets/img/faces/face_3.jpg", args: "", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/faces/face_3.jpg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
127.0.0.1 - - [22/Sep/2023:06:19:33 +0000] "GET /assets/img/faces/face_4.jpg HTTP/1.1" 200 56227 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
2023/09/22 06:19:33 [notice] 29#29: *12 "https?://[^/]+/app2/(.*)" matches "http://127.0.0.1:8080/app2/", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/faces/face_5.jpg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *12 "^((?!/app2).*)" matches "/assets/img/faces/face_5.jpg", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/faces/face_5.jpg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *12 rewritten data: "/app2/assets/img/faces/face_5.jpg", args: "", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/faces/face_5.jpg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
127.0.0.1 - - [22/Sep/2023:06:19:33 +0000] "GET /assets/img/faces/face_3.jpg HTTP/1.1" 200 59833 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
2023/09/22 06:19:33 [notice] 29#29: *10 "https?://[^/]+/app2/(.*)" matches "http://127.0.0.1:8080/app2/", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/faces/face_6.jpg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *10 "^((?!/app2).*)" matches "/assets/img/faces/face_6.jpg", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/faces/face_6.jpg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *10 rewritten data: "/app2/assets/img/faces/face_6.jpg", args: "", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/faces/face_6.jpg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *1 "https?://[^/]+/app2/(.*)" matches "http://127.0.0.1:8080/app2/", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/faces/face_2.jpg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *1 "^((?!/app2).*)" matches "/assets/img/faces/face_2.jpg", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/faces/face_2.jpg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *1 rewritten data: "/app2/assets/img/faces/face_2.jpg", args: "", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/faces/face_2.jpg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
127.0.0.1 - - [22/Sep/2023:06:19:33 +0000] "GET /assets/img/faces/face_5.jpg HTTP/1.1" 200 66106 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
127.0.0.1 - - [22/Sep/2023:06:19:33 +0000] "GET /assets/img/faces/face_6.jpg HTTP/1.1" 200 60971 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
127.0.0.1 - - [22/Sep/2023:06:19:33 +0000] "GET /assets/img/faces/face_2.jpg HTTP/1.1" 200 80120 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
2023/09/22 06:19:33 [notice] 29#29: *10 "https?://[^/]+/app2/(.*)" matches "http://127.0.0.1:8080/app2/", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/css/fonts/Font-Awesome/fontawesome-webfont.woff2?v=4.7.0 HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *10 "^((?!/app2).*)" matches "/assets/css/fonts/Font-Awesome/fontawesome-webfont.woff2", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/css/fonts/Font-Awesome/fontawesome-webfont.woff2?v=4.7.0 HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *10 rewritten data: "/app2/assets/css/fonts/Font-Awesome/fontawesome-webfont.woff2", args: "v=4.7.0", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/css/fonts/Font-Awesome/fontawesome-webfont.woff2?v=4.7.0 HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
127.0.0.1 - - [22/Sep/2023:06:19:33 +0000] "GET /assets/css/fonts/Font-Awesome/fontawesome-webfont.woff2?v=4.7.0 HTTP/1.1" 200 77160 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
2023/09/22 06:19:33 [notice] 29#29: *12 "https?://[^/]+/app2/(.*)" matches "http://127.0.0.1:8080/app2/", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/css/fonts/Pe-Icon-Stroke/Pe-icon-7-stroke.woff?d7yf1v HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *12 "^((?!/app2).*)" matches "/assets/css/fonts/Pe-Icon-Stroke/Pe-icon-7-stroke.woff", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/css/fonts/Pe-Icon-Stroke/Pe-icon-7-stroke.woff?d7yf1v HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *12 rewritten data: "/app2/assets/css/fonts/Pe-Icon-Stroke/Pe-icon-7-stroke.woff", args: "d7yf1v", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/css/fonts/Pe-Icon-Stroke/Pe-icon-7-stroke.woff?d7yf1v HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
127.0.0.1 - - [22/Sep/2023:06:19:33 +0000] "GET /assets/css/fonts/Pe-Icon-Stroke/Pe-icon-7-stroke.woff?d7yf1v HTTP/1.1" 200 58556 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
2023/09/22 06:19:33 [notice] 29#29: *1 "https?://[^/]+/app2/(.*)" matches "http://127.0.0.1:8080/app2/", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/header-1.jpeg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *1 "^((?!/app2).*)" matches "/assets/img/header-1.jpeg", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/header-1.jpeg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *1 rewritten data: "/app2/assets/img/header-1.jpeg", args: "", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/header-1.jpeg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
127.0.0.1 - - [22/Sep/2023:06:19:33 +0000] "GET /assets/img/header-1.jpeg HTTP/1.1" 200 430070 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
2023/09/22 06:19:33 [notice] 29#29: *11 "https?://[^/]+/app2/(.*)" matches "http://127.0.0.1:8080/app2/", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/header-2.jpeg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *11 "^((?!/app2).*)" matches "/assets/img/header-2.jpeg", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/header-2.jpeg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *11 rewritten data: "/app2/assets/img/header-2.jpeg", args: "", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/header-2.jpeg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
127.0.0.1 - - [22/Sep/2023:06:19:33 +0000] "GET /assets/img/header-2.jpeg HTTP/1.1" 200 238167 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
2023/09/22 06:19:33 [notice] 29#29: *10 "https?://[^/]+/app2/(.*)" matches "http://127.0.0.1:8080/app2/", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/office-1.jpeg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *10 "^((?!/app2).*)" matches "/assets/img/office-1.jpeg", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/office-1.jpeg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:33 [notice] 29#29: *10 rewritten data: "/app2/assets/img/office-1.jpeg", args: "", client: 127.0.0.1, server: 127.0.0.1, request: "GET /assets/img/office-1.jpeg HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
127.0.0.1 - - [22/Sep/2023:06:19:33 +0000] "GET /assets/img/office-1.jpeg HTTP/1.1" 200 383149 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
2023/09/22 06:19:34 [notice] 29#29: *10 "https?://[^/]+/app2/(.*)" matches "http://127.0.0.1:8080/app2/", client: 127.0.0.1, server: 127.0.0.1, request: "GET /app2/favicon.png HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:34 [notice] 29#29: *10 "^((?!/app2).*)" does not match "/app2/favicon.png", client: 127.0.0.1, server: 127.0.0.1, request: "GET /app2/favicon.png HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
127.0.0.1 - - [22/Sep/2023:06:19:34 +0000] "GET /app2/favicon.png HTTP/1.1" 200 143748 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
2023/09/22 06:19:34 [notice] 29#29: *10 "https?://[^/]+/app2/(.*)" matches "http://127.0.0.1:8080/app2/", client: 127.0.0.1, server: 127.0.0.1, request: "GET /static/assets/img/favicon.png HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:34 [notice] 29#29: *10 "^((?!/app2).*)" matches "/static/assets/img/favicon.png", client: 127.0.0.1, server: 127.0.0.1, request: "GET /static/assets/img/favicon.png HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
2023/09/22 06:19:34 [notice] 29#29: *10 rewritten data: "/app2/static/assets/img/favicon.png", args: "", client: 127.0.0.1, server: 127.0.0.1, request: "GET /static/assets/img/favicon.png HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/app2/"
127.0.0.1 - - [22/Sep/2023:06:19:34 +0000] "GET /static/assets/img/favicon.png HTTP/1.1" 200 1051 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
2/ app2 的 nginx 服务器
172.17.0.1 - - [22/Sep/2023:06:19:32 +0000] "GET / HTTP/1.0" 200 1051 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"
172.17.0.1 - - [22/Sep/2023:06:19:32 +0000] "GET /main.js HTTP/1.0" 200 9875574 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"
172.17.0.1 - - [22/Sep/2023:06:19:33 +0000] "GET /assets/img/faces/face_1.jpg HTTP/1.0" 200 64369 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"
172.17.0.1 - - [22/Sep/2023:06:19:33 +0000] "GET /assets/img/faces/face_4.jpg HTTP/1.0" 200 56227 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"
172.17.0.1 - - [22/Sep/2023:06:19:33 +0000] "GET /assets/img/faces/face_3.jpg HTTP/1.0" 200 59833 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"
172.17.0.1 - - [22/Sep/2023:06:19:33 +0000] "GET /assets/img/faces/face_5.jpg HTTP/1.0" 200 66106 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"
172.17.0.1 - - [22/Sep/2023:06:19:33 +0000] "GET /assets/img/faces/face_6.jpg HTTP/1.0" 200 60971 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"
172.17.0.1 - - [22/Sep/2023:06:19:33 +0000] "GET /assets/img/faces/face_2.jpg HTTP/1.0" 200 80120 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"
172.17.0.1 - - [22/Sep/2023:06:19:33 +0000] "GET /assets/css/fonts/Font-Awesome/fontawesome-webfont.woff2?v=4.7.0 HTTP/1.0" 200 77160 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"
172.17.0.1 - - [22/Sep/2023:06:19:33 +0000] "GET /assets/css/fonts/Pe-Icon-Stroke/Pe-icon-7-stroke.woff?d7yf1v HTTP/1.0" 200 58556 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"
172.17.0.1 - - [22/Sep/2023:06:19:33 +0000] "GET /assets/img/header-1.jpeg HTTP/1.0" 200 430070 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"
172.17.0.1 - - [22/Sep/2023:06:19:33 +0000] "GET /assets/img/header-2.jpeg HTTP/1.0" 200 238167 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"
172.17.0.1 - - [22/Sep/2023:06:19:33 +0000] "GET /assets/img/office-1.jpeg HTTP/1.0" 200 383149 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"
172.17.0.1 - - [22/Sep/2023:06:19:34 +0000] "GET /favicon.png HTTP/1.0" 200 143748 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"
172.17.0.1 - - [22/Sep/2023:06:19:34 +0000] "GET /static/assets/img/favicon.png HTTP/1.0" 200 1051 "http://127.0.0.1:8080/app2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"
我也尝试过使用 nginx 的 rewrite(以及许多不同的正则表达式),但得到了类似的错误
location /app2/ {
proxy_pass http://127.0.0.1:5300/;
proxy_set_header Host 127.0.0.1:5300;
rewrite ^/app2/(.*)$ /$1 break;
}
如何配置 nginx 反向代理以/app2
从路径中删除并从目标应用程序返回正确的页面。
答案1
我想我已经知道你想解决什么了。
当您从 请求页面时,/app2/
它会尝试加载其他资源(例如)/main.js
,这会导致 404 错误,因为它应该是/app2/main.js
。
如果这是您的问题,您正在尝试一种极其复杂且不必要的方法来解决它。
我进一步假设第二个应用程序也是一个 React 应用程序,就像第一个一样。如果是这种情况,你只需要正确配置你的应用的基本 URL。
在您的
package.json
文件中,将homepage
条目更改为相对路径,不包括任何子域/嵌套目录。
对于你来说,情况是:
{
...,
"homepage": "/app2/",
...
}
然后,你的 nginx 配置中唯一需要的是:
location /app2/ {
proxy_pass http://127.0.0.1:5300/;
### I don't think the Host header is even necessary
# proxy_set_header Host 127.0.0.1:5300;
}
您的 React 应用程序现在应该自动为反向代理生成正确的 URL。