linux上go web访问失败
linux上go web访问失败
问题1:前端一直访问不到后端接口,我以为是跨域问题,但是我暂时没有找到问题
conf.d/default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
try_files $uri $uri/ /index.html;
root /usr/share/nginx/html;
index index.html index.htm;
}
# 将/api下的请求转发到后端
location /api/ {
proxy_pass http://127.0.0.1:8080/api/;
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 X-Forwarded-Proto $scheme;
# rewrite ^/api/(.*)$ /$1 break;
}
location /uploads/ {
alias /opt/gvb/server/uploads/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
问题2:访问不到后端接口
使用curl内网访问 127.0.0.1能够访问成功
[root@iZf8zaj27gxmvpvdjp2thtZ /]# curl 127.0.0.1:8080/api/article
{"code":0,"data":{"count":0,"list":[]},"msg":"成功"}
直接访问ip,未响应
[root@iZf8zaj27gxmvpvdjp2thtZ /]# curl xx.xx.xx.xx:8080/api/article
^C
[root@iZf8zaj27gxmvpvdjp2thtZ gvb_ser]# ./main
2024/07/08 18:42:38 config yamlFile load Init success.
[gvb] [2024-07-08 18:42:38] [info] es.go:20 gvb_server/core.ConnectES 连接es成功
[gvb] [2024-07-08 18:42:38] [info] redis.go:31 gvb_server/core.ConnectRedisDB redis连接成功
[gvb] [2024-07-08 18:42:38] [info] main.go:34 main.main gvb_server运行在:0.0.0.0:8080
[GIN] 2024/07/08 - 19:42:21 | 200 | 321.557899ms | 127.0.0.1 | GET "/api/article"
再尝试前端接口,访问也没有问题
[root@iZf8zaj27gxmvpvdjp2thtZ /]# curl xx.xx.xx.xx:81
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="icon" href="/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vite App</title>
<script type="module" crossorigin src="/assets/index-DQcT9Huh.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-DNEc7cif.css">
</head>
<body>
<div id="app"></div>
</body>
我通过 枫枫 的指点,先后解决了2,1
解决1:
结果并不是跨域问题,发现访问404,而且后端没有收到请求,说明前端的网络出现了问题,后面发现nginx的容器的网络是bridge模式,这个模式会导致nginx容器将127这个ip作为自己容器的ip,而不是宿主机的ip,但是我们的后端服务是在宿主机中的,所以出现了这个问题,我们需要更改为host网络模式
# 查看网络模式
docker inspect -f '{{ .HostConfig.NetworkMode }}' nginx_container_id_or_name
bridge
我们需要重新运行一个新的容器,并且-v指定nginx配置,conf.d/default.conf在我的文章开头,dist文件是你的vue build之后的文件
nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
# host模式通常不指定-p
docker run -d \
--name nginx \
-v /home/nginx/conf.d:/etc/nginx/conf.d \
-v /home/nginx/nginx.conf:/home/nginx/nginx.conf \
-v /home/xrc/nginx/dist:/usr/share/nginx/html \
--network host \
--restart=always \
nginx
解决2:
这个问题是因为我的内部防火墙没有关闭, 没有对外开放,所以不仅仅需要开安全组。不过实际上开发是不需要对外开放的,我们通过nginx的80端口访问前端,前端的nginx代理到后端请求即可。这个问题是多余了,但是也让我多了解了一些原理
# 开启防火墙
systemctl start firewalld.service
# 关闭防火墙
systemctl stop firewalld.service
更新:
问题3:部署的图片资源访问404
default.conf
location /uploads/ {
alias /opt/gvb/server/uploads/;
}
解决3:
我一直以为是这个配置文件出错,我改了很多网络上的版本,仍然不对,最后我尝试了重启nginx,终于成功访问
我在运行容器的时候没有指定--restart=always退出容器立即重启服务,所以需要手动重启
问题4:nginx下的localhost/uploads/file/default.png访问失败
因为同源策略的影响,前端url的地址是ip,但是请求的缺省localhost,所以这样是不行的

浙公网安备 33010602011771号