Local Community

Large $wpdb query causes 502 Bad Gateway (too big header) -- Increasing Fast-CGI buffer partially fixes

Issue Summary

This WordPress site uses a large front end form to create/update posts and many custom fields. Initially, the forms overwhelmed fast-cgi causing a 502 Bad Gateway. Increasing buffer size resolved the issue. See here. And here. Now, a front end form is encountering the buffer limit (despite having increased its size) causing the 502 once again. Attempting to increase buffer size further or replacing proxy_buffer settings to proxy_buffering off; results in a 405 Not Allowed. Am concerned that already the buffer size is too large.

nginx error:

2022/10/26 18:09:15 [error] 70997#0: *47 upstream sent too big header while reading response header from upstream, client: 127.0.0.1, server: , request: "POST /?post_type=custom_scan&p=12450 HTTP/1.0", upstream: "fastcgi://unix:/Users/roa/Library/Application Support/Local/run/zJJZ9KiTe/php/php-fpm.socket:", host: "oneoption.local", referrer: "http://oneoption.local/?post_type=custom_scan&p=12450"

Current Buffer Settings:

fastcgi_pass php;
		fastcgi_buffer_size      256k;
		fastcgi_buffers          8 512k;
		fastcgi_read_timeout	 1200s;

		proxy_buffer_size        256k;
		proxy_buffers            8 512k;
		proxy_busy_buffers_size  512k;
```

Query causing issue (note the foreach commented out - the form currently submits successfully with these commented out; uncommenting these [as desired] results in the 502)

```
$fields = (object) [
  'checkbox_groups' => ['trendline_20','trendline_20_breach','trendline_60','trendline_60_breach','trade_signal','trade_signal_new','divergence','strength_vs_spy','strength_vs_sector','compression_out','range_90_10','climax','adx','ha_rev_i-i','ha_rev_iiii','ha_ii','ha_cont_ii','stochastic','algo_lines','algo_lines_new'],
  'checkbox_groups_neutral' => ['compression_in','heavy_volume'],
  'bools' => ['pre_earnings_today','post_earnings_today','post_earnings_bull','post_earnings_bear','weekly_options','only_etfs','no_etfs','bullish_climax_long_term','bearish_climax_long_term','greater_prior_high','less_prior_low','greater_20d_high','less_20d_low','greater_52w_high','less_52w_low','greater_sma20','greater_sma50','greater_sma100','greater_sma200','less_sma20','less_sma50','less_sma100','less_sma200','short_squeeze'],
  'ranges' => ['option_liquidity', 'option_iv_atm','stock_price','heavy_volume_today','heavy_volume_yesterday','twenty_day_vol','stock_volatility','percent_60_day_range'],
  'ranges_date' => ['earnings_date_projected','earnings_date_previous','pre_earn_bull','pre_earn_bear'],
];

$post_id = get_the_ID();

 if ( isset( $_POST['post_id'] ) && current_user_can( 'manage_options', $post_id ) ) {

    if(!empty($_POST['title']))
    {
      $new_slug = $new_title = sanitize_title($_POST['title']);
        wp_update_post(
            array (
                'ID'        => $_POST['post_id'],
                'post_title' => $new_title,
                'post_content'  =>  $_POST['description'],
                'post_name' => $new_slug,
                'post_status'   => 'draft',
            )
        );
    }


    $items = [];

    foreach ($fields->checkbox_groups as &$value) {
      $items[$value] = serialize([$_POST[$value.'-m5bull'], $_POST[$value.'-m5bear'], $_POST[$value.'-m15bull'], $_POST[$value.'-m15bear'], $_POST[$value.'-m30bull'], $_POST[$value.'-m30bear'], $_POST[$value.'-h1bull'], $_POST[$value.'-h1bear'], $_POST[$value.'-h2bull'], $_POST[$value.'-h2bear'], $_POST[$value.'-h4bull'], $_POST[$value.'-h4bear'], $_POST[$value.'-d1bull'], $_POST[$value.'-d1bear']]);
      $items['_'.$value] = acf_get_field_key($value, $rndmID);
    }

    foreach ($fields->checkbox_groups_neutral as &$value) {
      $items[$value] = serialize([$_POST[$value.'-m5'], $_POST[$value.'-m15'], $_POST[$value.'-m30'], $_POST[$value.'-h1'], $_POST['-h2'], $_POST[$value.'-h4'], $_POST[$value.'-d1']]);
      $items['_'.$value] = acf_get_field_key($value, $rndmID);
    }

    // foreach ($fields->ranges as &$value) {
    //   $items[$value.'_bool'] = $_POST[$value.'_bool'];
    //   $items['_'.$value.'_bool'] = acf_get_field_key($value.'_bool', $rndmID);
    //   $items[$value.'_min'] = $_POST[$value.'_min'];
    //   $items['_'.$value.'_min'] = acf_get_field_key($value.'_min', $rndmID);
    //   $items[$value.'_max'] = $_POST[$value.'_max'];
    //   $items['_'.$value.'_max'] = acf_get_field_key($value.'_max', $rndmID);
    // }

    // foreach ($fields->ranges_date as &$value) {
    //   $items[$value.'_bool'] = $_POST[$value.'_bool'];
    //   $items['_'.$value.'_bool'] = acf_get_field_key($value.'_bool', $rndmID);
    //   $items[$value.'_min'] = $_POST[$value.'_min'];
    //   $items['_'.$value.'_min'] = acf_get_field_key($value.'_min', $rndmID);
    //   $items[$value.'_max'] = $_POST[$value.'_max'];
    //   $items['_'.$value.'_max'] = acf_get_field_key($value.'_max', $rndmID);
    // }

    // foreach ($fields->bools as &$value) {
    //   $items[$value] = $_POST[$value.'_bool'];
    //   $items['_'.$value] = acf_get_field_key($value, $rndmID);
    // }

    $values = [];
    foreach ( $items as $key => $value ) {
        $values[] = $wpdb->prepare( "(%d,%s,%s)", $post_id, $key, $value );
    }
    $query = "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES ";
    $query .= implode( ",\n", $values );
    global $wpdb;
    // delete all meta first https://support.advancedcustomfields.com/forums/topic/better-way-to-update-multiple-fields-update_field/
    $wpdb->query(
      $wpdb->prepare(
        "
          DELETE FROM wp_postmeta
          WHERE post_id = %d
        ",
        $post_id
      )
    );
    //update fields
    $wpdb->query( $query );

    //redirect
    wp_redirect(get_permalink($post_id)); exit;
  } 
```


#### Troubleshooting Questions

- Are you able to create a new, plain WordPress site in Local and access it in a Browser?
  -Yes

#### System Details

- Which version of Local is being used? 
  -Local 6.4.3

- What Operating System (OS) and OS version is being used? 
  -macOS Catalina


- Attach the Local Log. See this Help Doc for instructions on how to do so:
  - [local-logs3.zip|attachment](upload://uIQo99c2bAABdazHpwmOoA2cmb0.zip) (14.3 KB)

### Security Reminder

Local does a pretty good job of scrubbing private info from the logs and the errors it produces, however there's always the possibility that something private can come through. Because these are public forums, always review the screenshots you are sharing to make sure there isn't private info like passwords being displayed.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.