我正在制作一个多容器应用程序,与nginx在前端和弹簧引导在后端反应。
在添加nginx之前,我使用localhost:3000作为我的react应用程序,使用localhost:8080作为我的spring。在localhost:8080 3000/themes中,我可以看到react页面,使用/themes,我可以访问,因为package.json中的代理行是。
根据下面的代码修改这个接口--编写上下文,但是在localhost:80/themes中,我只能访问API,这是预料中的。如果我将行"proxy_pass http://backend;"放在"location /api {}"中,当access http://backend;时,我将得到我的个性化404页面,因为/api不是一条反应路由,不应该是。我希望有一个无反应的路径来服务我的api,比如代理行中的"http:backend/api",在"localhost/themes"中有我的主题页面,在子组件中访问"/themes",并从localhost:8080/themes获得这个API,就像我可以从邮递员那里访问一样。
我现在的nginx.conf是:
upstream backend {
server app-server:8080;
}
server {
listen 80;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
proxy_pass http://backend;
}
error_page 401 403 404 index.html;
location /public {
root /usr/local/var/www;
}
}
我的package.json代理行是:
"proxy": "http://backend",
发布于 2021-10-13 07:57:15
您可以尝试为proxy_pass
添加一个命名位置,并将其作为try_files
的最后一个参数追加。
upstream backend {
server app-server:8080;
}
server {
listen 80;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ @proxy-backend;
}
location @proxy-backend {
proxy_pass http://backend;
}
error_page 401 403 404 index.html;
location /public {
root /usr/local/var/www;
}
}
https://stackoverflow.com/questions/69558280
复制相似问题