先上解决方案:
-
确保 Nginx 有权限连接到后端端口(28080)。详见解决方案:端口绑定权限被拒绝
-
如果使用了 SELinux,需要允许 httpd 网络连接:
bash
setsebool -P httpd_can_network_connect 1
问题过程描述:
http://10.10.10.173:28011/prod-api/code 访问报502 Bad Gateway,
在本地命令访问curl http://127.0.0.1:28080/code能正常请求到数据。
nginx实际转发配置如下:
server {
listen 28011;
server_name esp.icp.com;
index index.php index.html index.htm default.php default.htm default.html;
root /home/esp/web/esp-cloud-vue3/dist;
try_files uri uri/ /index.html; # 将所有请求导 index.html
#PROXY-START/
location ~ ^/(prod-)?api(.*)$ {
#rewrite ^/api(.*) 1 break;
rewrite ^/(?:prod-)?api(/.*) 1 break;
proxy_pass http://127.0.0.1:28080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_http_version 1.1;
proxy_hide_header Upgrade;
add_header X-Cache $upstream_cache_status;
#Set Nginx Cache
set $static_filedaYAOiSb 0;
if ( uri \~\* "\\.(gif\|png\|jpg\|css\|js\|woff\|woff2)" )
{
set $static_filedaYAOiSb 1;
expires 1m;
}
if ( $static_filedaYAOiSb = 0 )
{
add_header Cache-Control no-cache;
}
}
#PROXY-END/
}
root@localhost \~\]# curl http://127.0.0.1:28080/code {"msg":"操作成功","img":"/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAA8AKADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBg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