Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

nginx代理Vue项目端口出现占用,并且接口出现404.

vue的端口是8080,nginx按理来说是监听8080的但是请求的接口是404。
下面是nginx的配置:

    server {
        listen       8080;
        server_name  localhost;
        location / {
            add_header Access-Control-Allow-Origin *;
            root   html;
            index  index.html index.htm;
        }
         location /api {
             proxy_pass   https://www.sxqcwx.com;
         }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 
    }

我的请求

      this.postAxios("/api/findRegion2", {}, res => {
        console.log(res);
      });

在Vue中这样是可以请求成功的

    proxyTable: {
      // '/api/**': {
      //   target: 'https://www.sxqcwx.com',
      //   secure: false,
      //   changeOrigin: true,
      // }
    }

image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
server {
    listen 8080;
    root /www;
    index index.html;
    sendfile on;
    sendfile_max_chunk 1M;
    tcp_nopush on;
    gzip_static on;

    location /api/ {
      proxy_pass https://www.sxqcwx.com;
    }

    location / {
        try_files $uri $uri/ /index.html;
    }
}

image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...