连接到上游时Nginx:(111:连接被拒绝)

时间:2016-07-15 07:48:02

标签: nginx

我试图使用Nginx进行反向代理。我在端口3333上有1个Node.js应用程序,在端口3334上有Golang应用程序。在调用Golang API时,我在/var/log/nginx/error.log中看到此消息:

2016/07/15 10:18:36 [error] 4835#0: *131 connect() failed (111: Connection refused) while connecting to upstream, 
client: 27.69.66.52, 
server: video1.techmaster.vn, 
request: "GET /stream/dash/5klRyUnPVyDWouxscIT42uWs5JL4x9nHFol9ecg5g0GLf7aTaI/eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwcmljZSI6MCwiZnVsbCI6dHJ1ZSwidmlkZW8iOnsiaWQiOjEwODcsIm5hbWUiOiJtcDNaaW5nU2hvd0RhdGEtMjY0Lm1wNCIsInBhdGgiOiIvbWVkaWEvODIxNyJ9LCJhdHRhY2htZW50IjpbXSwiZXhwIjoxNDY4NTU2NTE2fQ.qc9d_XPhCepHf5iJyf9ORBPOo3pTvF8Th_VMadNSM2o/43f_vid_19.m4s HTTP/1.1", 
upstream: "http://127.0.0.1:3334/stream/dash/5klRyUnPVyDWouxscIT42uWs5JL4x9nHFol9ecg5g0GLf7aTaI/eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwcmljZSI6MCwiZnVsbCI6dHJ1ZSwidmlkZW8iOnsiaWQiOjEwODcsIm5hbWUiOiJtcDNaaW5nU2hvd0RhdGEtMjY0Lm1wNCIsInBhdGgiOiIvbWVkaWEvODIxNyJ9LCJhdHRhY2htZW50IjpbXSwiZXhwIjoxNDY4NTU2NTE2fQ.qc9d_XPhCepHf5iJyf9ORBPOo3pTvF8Th_VMadNSM2o/43f_vid_19.m4s", 
host: "video1.techmaster.vn", 
referrer: "https://techmaster.vn/khoa-hoc-online/8217/lap-trinh-ios-swift/96/Location-Notification"

我不知道问题是什么。也许我在Nginx配置中做了一些错误。这是我的Nginx配置:

server {
listen  80;
server_name video1.techmaster.vn www.video1.techmaster.vn;
return  301 https://$server_name$request_uri;
}
server {
listen 443 spdy ssl;
server_name video1.techmaster.vn www.video1.techmaster.vn;
keepalive_timeout 30;

# Allow upload video up to 100M
client_max_body_size 100M;

# Config SSL
ssl on;
ssl_certificate /etc/ssl/cert_chain.crt;
ssl_certificate_key /etc/ssl/private/sv.video1.techmaster.vn.key;
ssl_session_timeout 5m;
ssl_session_cache  shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers "xxxxxxxxxx";
ssl_prefer_server_ciphers on;
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";
ssl_stapling on;

location ~* /.*\.(xml)$      {
    root /var/www/videos.techmaster.vn/public;
    expires 7d;
}

location /stream/ {
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:3334;

    set $cors '';

    if ($http_origin ~* (localhost|www\.techmaster\.vn|techmaster\.vn)) {
        set $cors 'true';
    }

    if ($cors = 'true') {
        add_header 'Access-Control-Allow-Origin' "$http_origin";
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Range';
    }
}

location / {
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:3333;

    set $cors '';

    if ($http_origin ~* (localhost|www\.techmaster\.vn|techmaster\.vn)) {
        set $cors 'true';
    }

    if ($cors = 'true') {
        add_header 'Access-Control-Allow-Origin' "$http_origin";
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Range';
    }
}
}

2 个答案:

答案 0 :(得分:2)

我已经编辑了Nginx配置,它可以正常工作

curl 'https://www.youtube.com/insight_ajax?action_get_statistics_and_data=1&v=QcIy9NiNbmo' -H 'Origin: https://www.youtube.com' -H 'X-YouTube-Page-Label: youtube_20160714_RC2' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36' -H 'X-YouTube-Variants-Checksum: 634d96c10153993c2fa1ff127bb5f5fa' -H 'Content-Type: application/x-www-form-urlencoded' -H 'X-YouTube-Page-CL: 127470335' -H 'Referer: https://www.youtube.com/watch?v=QcIy9NiNbmo' -H 'X-YouTube-Client-Version: 1.20160714' -H 'X-Youtube-Identity-Token: QUFFLUhqblJscnRybVd5NUdiMk5YcTdoUUE5el9oajNNUXw=' --data 'session_token=QUFFLUhqa2xneXkxMWN1VWRZTndWc1FTbXFpeDhKZjRFd3xBQ3Jtc0tueEQ0dEp3ZThFeFNQT0FnZldKbWp1UmZ0bmxZeGxwS0MxRFBTTVhlaV9Pek5QNFQ3TkV1TTBFck5WR1RJTGpPd3JtdS1TMnBjbTNjYmV0MThNbHdMQmxyZnQzLTRkcWl1dWU4M3lkbWEyQlgzdEUxRmJtNnB2bl9uOFVzSWN6Nm5sXzFRenNIWnc2dXJkMFFZNVZhdktNc3JoRmc%3D' --compressed

答案 1 :(得分:2)

在使用节点平台配置AWS时遇到了同样的问题。

我使用3000端口启动应用程序。 当我将端口更改为8081时,它可以工作。