Facebook 错误解析输入 URL 错误 502 Bad Gateway

Facebook 错误解析输入 URL 错误 502 Bad Gateway

每当有人在 Facebook 或 Twitter 上分享我的网站链接时,他们都会收到 502 Bad Gate way。我检查了 nginx 错误日志以查看发生了什么,结果出现了这个错误。

error1615#0: *2375 connect() failed (111: Connection refused) while connecting to upstream, client: 69.171.230.119, server: example.com, request: "GET /328/example/tracks/2023 HTTP/1.1", upstream: "http://127.0.0.1:9000/snapshots?_escaped_fragment_=/328/example/tracks/2023", host: " example.com"

我尝试了很多方法来解决此问题,但没有成功。

#configs
server {
    listen                          80;
    server_name                     www.example.com;
    rewrite                         ^/(.*)      http://example.com/$1           permanent;
}

server {
    listen                          80;
    server_name                     www.api.example.com;
    rewrite                         ^/(.*)      http://api.example.com/$1       permanent;
}

server {
    set $maintenance 0;

    listen                          80;
    server_name                     example.com;

    access_log                      /srv/example-backend/logs/nginx.access.log;
    error_log                       /srv/example-backend/logs/nginx.error.log;

    root                            /srv/example-frontend/www/built/prod;
    index                           index.html;

    gzip                            on;
    gzip_types                      text/plain text/xml text/css application/json application/javascript application/x-javascript;

    location = /favicon.ico {
         log_not_found              off;
    }

    location /api {
        proxy_pass                  http://api.example.com/api/v1;
        proxy_redirect              off;
    }

    location ~* \.(eot|ttf|woff|js|css)$ {
    expires                     max;
        add_header                  Access-Control-Allow-Origin     *;
        add_header                  Access-Control-Allow-Headers    "X-Requested-With";
        add_header                  Access-Control-Allow-Methods    "GET, OPTIONS";
    }

    location / {
        set $prerender 0;

        if ($http_user_agent ~* "baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest") {
            set $prerender 1;
            set $prerender_uri $request_uri;
        }

        if ($args ~ "_escaped_fragment_=(.*)") {
            set $prerender 1;
            set $prerender_uri $1;
        }

        if ($uri ~ "\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent)") {
            set $prerender 0;
            set $maintenance 0;
        }

        if ($prerender = 1) {
            set $args _escaped_fragment_=$prerender_uri;
            rewrite                 ^       /snapshots;
        }

        expires                     -1;
        add_header                  Pragma                  "no-cache";
        add_header                  Cache-Control           "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";

        if ($args ~* disable_maintenance) {
            set $maintenance 0;
        }

        if ($maintenance = 1) {
            rewrite            ^       /maintenance.html break;
        }

        try_files                   $uri                    /index.html =404;
    }

    location /snapshots {
        proxy_set_header            Host                    $http_host;

        proxy_pass                  http://127.0.0.1:9000;
        proxy_connect_timeout       30s;
        proxy_redirect              off;
    }
}

server {
    listen                          80;
    server_name                     api.example.com;

    gzip                            on;
    gzip_types                      text/plain text/xml text/css application/json application/javascript;
    client_max_body_size            2G;

    access_log                      /srv/example-backend/logs/nginx.api.access.log;
    error_log                       /srv/example-backend/logs/nginx.api.error.log;

    root                            /srv/example-backend/www/app/;
    index                           index.php;

    more_set_headers                'Access-Control-Allow-Origin: *';
    more_set_headers                'Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE';
    more_set_headers                'Access-Control-Allow-Headers: Authorization,Cache-Control,Origin,Content-Type,Accept';

    location = /favicon.ico {
        log_not_found               off;
        access_log                  off;
    }

    location = /robots.txt {
         alias /srv/example-frontend/www/built/prod/robots.txt;
    }

   location ~ /\. {
        deny                        all;
        access_log                  off;
        log_not_found               off;
   }

    location / {
        if ($request_method = OPTIONS) {
            more_set_headers       'Content-Length: 0';
            more_set_headers       'Access-Control-Max-Age: 1728000';
            more_set_headers       'Content-Type: text/plain';
            return                 204;
        }

        try_files                   $uri                    /index.php?$args;
    }

    location ~ /uploads/ {
        if ($request_method = OPTIONS) {
            more_set_headers       'Content-Length: 0';
            more_set_headers       'Access-Control-Max-Age: 1728000';
            more_set_headers       'Content-Type: text/plain';
            return                 204;
        }

        #count downloads
        if ($args ~* dl=1&entityTypeId=(\d+)&entityId=(\d+)&userId=(\d+)&filename=(.+)$) {
             set $entityTypeId      $1;
             set $entityId          $2;
             set $userId            $3;

             more_clear_headers     Content-Type;
             add_header             Content-Type            application/octet-stream;
             add_header             Content-Disposition     'attachment; filename=$4';

             post_action /count_downloads;
        }

    expires                     max;
        try_files                   $uri        =404;
    }

    location /count_downloads {
        proxy_pass                  http://api.example.com/api/v1/downloads/$entityTypeId/$entityId/$userId;
        internal;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires                     24h;
        log_not_found               off;
    }

    #yii specific
    location ~ /(protected|framework) {
        deny                        all;
        access_log                  off;
        log_not_found               off;
    }

    #m4a streaming
    location ~* \.m4a {
        types {
            audio/mp4               mp4     m4a;
        }

        mp4;
        mp4_buffer_size             1m;
        mp4_max_buffer_size         5m;
    }

    location ~ \.php$ {
        include                     fastcgi_params;
        try_files                   $uri                =404;
        fastcgi_split_path_info     ^(.+\.php)(/.+)$;
        fastcgi_param               PATH_INFO           $fastcgi_script_name;
        fastcgi_index               index.php;
        fastcgi_param               SCRIPT_FILENAME     $document_root$fastcgi_script_name;
        fastcgi_pass                unix:/var/run/php5-fpm.sock;
    }
}

答案1

这意味着 nginx 无法连接到 127.0.0.1 端口 9000 来转发请求。

您需要通过监控相关指标来调查为什么没有服务绑定到该套接字的原因。

相关内容