nginx上部署vue项目:访问时报错: Connection refused) while connecting to upstream, client: 172.16.54.1, server: localhost, request: “POST /api/accountApi/accoun t/loginAccount HTTP/1.1”, upstream: “http://10.2.0.126:8888/api/accountApi/account/loginAccount”, host: "172.16.5
一般在nginx.conf中配置不对,跨域访问就会出现404,找不到页面的情况: 如下配置:
主要是配置代理那块,这样写就没有问题了:真的是解决好久
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
root html; #配置自己的静态文件的路径,就是刚刚将打包的vue项目上传的路径,我这里是nginx根路径下的html文件中
location / {
# root html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
#需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404,两种写法,这里需要在下边配置router
# try_files $uri $uri/ @router;
}
# location @router{
# rewrite ^.*$ /index.html last;
#}
# 6、这里是解决跨域问题,将你后端的地址写在proxy_pass 后面就可以了
#重点
location /api/ {
#rewrite ^.+api/?(.*)$ /$1 break; #这样写有问题,在网上看有人这样写,发现有问题
rewrite ^/api/(.*)$ /$1 break; #必须的写这个,使用nginx的rewrite对uri进行重写 下面这行也要改为api
proxy_pass http://10.1.0.111:8888/; #跨域转发路由地址
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}