Nginx 代理传递到 docker 容器中的子目录反应应用程序生产
Nginx proxy pass to subdirectory react app production in docker container
我试图 运行 我构建的 React 应用程序作为我域的子目录(例如:mydomain.com/apps/one/client)。它 运行 在 docker 容器中,我提供了 nginx 配置:
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
那个应用程序 运行 和 docker 组合在一起,我将 80 容器端口映射到服务器计算机中的 3041。然后在我的服务器机器上我有这样的 nginx 配置:
server {
listen 443 ssl;
client_max_body_size 0;
server_name mydomain.com;
(cert settings)[...]
location /apps/one/client {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://localhost:3041/apps/one/client;
}
(other locations config)[...]
}
但我在控制台应用程序中遇到错误 Uncaught SyntaxError: Unexpected token '<'
。我尝试了很多不同的变体,但我仍然无法实现我想要的 - 在我的域路径的子目录上运行应用程序。在这种情况下,nginx 配置应该如何显示?非常感谢您的帮助。
我找到了解决方案。问题可能是 url 末尾缺少斜杠。最后我有:
location /apps/one/client/ {
proxy_pass http://localhost:3041/;
}
并按预期工作。
我试图 运行 我构建的 React 应用程序作为我域的子目录(例如:mydomain.com/apps/one/client)。它 运行 在 docker 容器中,我提供了 nginx 配置:
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
那个应用程序 运行 和 docker 组合在一起,我将 80 容器端口映射到服务器计算机中的 3041。然后在我的服务器机器上我有这样的 nginx 配置:
server {
listen 443 ssl;
client_max_body_size 0;
server_name mydomain.com;
(cert settings)[...]
location /apps/one/client {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://localhost:3041/apps/one/client;
}
(other locations config)[...]
}
但我在控制台应用程序中遇到错误 Uncaught SyntaxError: Unexpected token '<'
。我尝试了很多不同的变体,但我仍然无法实现我想要的 - 在我的域路径的子目录上运行应用程序。在这种情况下,nginx 配置应该如何显示?非常感谢您的帮助。
我找到了解决方案。问题可能是 url 末尾缺少斜杠。最后我有:
location /apps/one/client/ {
proxy_pass http://localhost:3041/;
}
并按预期工作。