节点获取 nginx 代理 400 错误请求
node fetch nginx proxy 400 Bad Request
我使用 request 和 nginx 代理将请求发送到 api。
这是我发送请求的方式
const request = require('request');
request({
url: 'https://google.com',
proxy: 'https://my-proxy.com',
strictSSL: false,
tunnel: false,
}, (err, response, body) => {
console.log(body)
});
一切正常
现在,由于 request
已弃用,我搬到了 node-fetch and https-proxy-agent
const fetch = require('node-fetch');
const HttpsProxyAgent = require('https-proxy-agent');
fetch('https://google.com', {
agent: new HttpsProxyAgent('https://my-proxy.com'),
method: 'GET'
})
.then(response => {
console.log(response)
return response.text()
})
.then(text => {
console.log(text)
})
在这种情况下,结果是
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.14.0 (Ubuntu)</center>
</body>
</html>
在 nginx 日志端我有
client sent invalid request while reading client request line, client: xx.xx.xx.xx, server: my-proxy.com, request: "CONNECT google.com:443 HTTP/1.1"
万一我可以解决它更新我的 nginx 配置
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS;
ssl_prefer_server_ciphers on;
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload";
ssl_dhparam dhparams.pem;
server {
listen 80;
server_name my-proxy.com;
location / {
resolver 8.8.8.8;
proxy_pass http://$http_host$uri$is_args$args;
}
}
server {
listen 443 ssl;
server_name my-proxy.com;
ssl_protocols TLSv1.1 TLSv1.2;
# for ssl https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04
location / {
resolver 8.8.8.8;
proxy_pass https://$http_host$uri$is_args$args;
}
}
server {
listen 80 default;
server_name _;
location / {
return 403;
}
}
}
我正在为 ssl 使用 certbot
很确定问题出在 strictSSL
但我找不到如何使用 node-fetch
我安装了squid代理。
对于这类工作确实比 nginx 好
我使用 request 和 nginx 代理将请求发送到 api。
这是我发送请求的方式
const request = require('request');
request({
url: 'https://google.com',
proxy: 'https://my-proxy.com',
strictSSL: false,
tunnel: false,
}, (err, response, body) => {
console.log(body)
});
一切正常
现在,由于 request
已弃用,我搬到了 node-fetch and https-proxy-agent
const fetch = require('node-fetch');
const HttpsProxyAgent = require('https-proxy-agent');
fetch('https://google.com', {
agent: new HttpsProxyAgent('https://my-proxy.com'),
method: 'GET'
})
.then(response => {
console.log(response)
return response.text()
})
.then(text => {
console.log(text)
})
在这种情况下,结果是
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.14.0 (Ubuntu)</center>
</body>
</html>
在 nginx 日志端我有
client sent invalid request while reading client request line, client: xx.xx.xx.xx, server: my-proxy.com, request: "CONNECT google.com:443 HTTP/1.1"
万一我可以解决它更新我的 nginx 配置
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS;
ssl_prefer_server_ciphers on;
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload";
ssl_dhparam dhparams.pem;
server {
listen 80;
server_name my-proxy.com;
location / {
resolver 8.8.8.8;
proxy_pass http://$http_host$uri$is_args$args;
}
}
server {
listen 443 ssl;
server_name my-proxy.com;
ssl_protocols TLSv1.1 TLSv1.2;
# for ssl https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04
location / {
resolver 8.8.8.8;
proxy_pass https://$http_host$uri$is_args$args;
}
}
server {
listen 80 default;
server_name _;
location / {
return 403;
}
}
}
我正在为 ssl 使用 certbot
很确定问题出在 strictSSL
但我找不到如何使用 node-fetch
我安装了squid代理。
对于这类工作确实比 nginx 好