nginx代理常见问题

1、http200,但是返回We’re sorry but XXXX doesn’t work properly without JavaScript enabled. Please enable it to continue.(项目1)

可能原因

server {
    listen       80;
    server_name  localhost;
    client_max_body_size 20m;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
        add_header Access-Control-Allow-Origin *;
    }

    // 改动
    location /dev-api/ {
        proxy_pass http:www.baidu.com/dev-api;
        add_header Access-Control-Allow-Origin *;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

改成

location /dev-api/ {
        proxy_pass http:www.baidu.com/dev-api/;
        add_header Access-Control-Allow-Origin *;
    }
// 或者
    location /dev-api {
        proxy_pass http:www.baidu.com/dev-api;
        add_header Access-Control-Allow-Origin *;
    }

项目本地启动代理配置 vue.config.js

devServer:{
		proxy: {
			'/dev-api': {  
				target: 'http://www.baidu.com/dev-api,
				ws: true,      
				secure: false, 
				changeOrigin: true,
				pathRewrite:{
					'^/dev-api':''
				}
			}
		}
	}

2、返回content-type是text/html (项目2)
原因:
nginx代理路径配置错误
… location /dev-api/ …

/dev-api/配置错误,查看我本地的代理,发现这个项目的请求路径是api,后改成/api/即可正常使用

项目本地启动代理配置

VUE_APP_PROXY = [["/proxy/api","http://www.baidu.com/dev-api"]
VUE_APP_GLOB_API_URL=/proxy

线上配置代理打包后,proxy应该被去掉

相当于

VUE_APP_PROXY = [["/api","http://www.baidu.com/dev-api"]
VUE_APP_GLOB_API_URL=
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值