vue测试 Nginx代理 调用三方接口

文章讲述了如何在Vue项目中利用Nginx配置实现对第三方API的代理调用,包括基本的Nginx配置和Vue代码示例,以及如何处理WebSocket请求的转发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vue测试 Nginx代理 调用三方接口

nginx.conf配置

增加/qtapi

worker_processes  1;
events {
    worker_connections  1024;
}
http {
	client_max_body_size 20m;
    include       mime.types;
    default_type  application/octet-stream;   
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;
    server {
		#网站端口
        listen       9080;
        server_name  localhost;
        location / {
			#网站目录
            root   html/www;
            index  index.html index.htm;
        }
		#websocket转发
		location /ws {
			proxy_pass http://localhost:9000;
			proxy_http_version 1.1;
			proxy_set_header Upgrade $http_upgrade;
			proxy_set_header Connection "upgrade";
		}
		location ~ /oa/ {
			#反向代理oa路径的转到
			#pss服务9000端口
            proxy_pass  http://127.0.0.1:9000;
		}      
		location /qtapi {
			proxy_pass https://api.j4u.ink/v1/store/other/proxy/remote/moyu.json;
		}
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

vue代码

import request from '@/utils/request';

postProxyNginx(){
             //Nginx代理测试
            /*nginx.conf配置
            server {
                location /qtapi {
                    proxy_pass https://api.j4u.ink/v1/store/other/proxy/remote/moyu.json;
                }
            }
            */
            request ({
                url: 'http://127.0.0.1:9080/qtapi',
                method: "get"
            }).then(res => {
				console.log('请求成功',res);
                that.ret='请求成功\n'+JSON.stringify(res)
			}).catch(err => {
                console.log('请求失败',err);     
                that.ret='请求失败\n'+JSON.stringify(err)         
			});
        },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值