0

I am noticing strange behavior with my code. ( PHP 7.4 ). I am using Jquery (jquery-3.6.0.min.js).

I am using JQuery post to retrieve the data from the server, but randomly ( sometimes on every 4th attempt ), I do get for the same request 502 Bad Gateway - with no errors in NGINX.

The jquery code is:

$.post(IBE_URL+"/assets/ajax/tour-addons.php",
               payload,
               function(data, status){
                   $("#addon_canvas_body").html(data);
               });

The current nginx configuration is:



## For a live site, handling more connections, uncomment, then start WinNMP.exe --phpCgiServers=25:
#worker_processes auto;
#worker_rlimit_nofile 100000;




events {   
    ## For a live site, uncomment:
    #worker_connections 8096;
}


http {

        server_tokens       off;
    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;
    ssi         off;
    server_names_hash_bucket_size  64;


    ## Timeouts ##
    ##############

    client_body_timeout 5;
    client_header_timeout   5;
    keepalive_timeout   25 25;
    send_timeout        15s;
    resolver_timeout    3s;

    # Timeout period for connection with FastCGI-server. It should be noted that this value can't exceed 75 seconds. 
    fastcgi_connect_timeout 5s;

    # Amount of time for upstream to wait for a fastcgi process to send data. 
    # Change this directive if you have long running fastcgi processes that do not produce output until they have finished processing. 
    # If you are seeing an upstream timed out error in the error log, then increase this parameter to something more appropriate. 
    fastcgi_read_timeout    40s;

    # Request timeout to the server. The timeout is calculated between two write operations, not for the whole request. 
    # If no data have been written during this period then serve closes the connection.
    fastcgi_send_timeout    15s;


    ## Buffers ##
    #############

    fastcgi_temp_file_write_size 10m;
    #fastcgi_busy_buffers_size 512k;
    fastcgi_buffer_size 512k;
    fastcgi_buffers 16 512k;

    open_file_cache         off;

    # php max upload limit cannot be larger than this       
    client_max_body_size        50m;    

    ####client_body_buffer_size 1K;
    client_header_buffer_size   8k;
    large_client_header_buffers 8 16k;
    types_hash_max_size     2048;




    include nginx.mimetypes.conf;
    default_type text/html;




    ## Logging ##
    #############

    access_log  "c:/winnmp/log/nginx_access.log";
    error_log   "c:/winnmp/log/nginx_error.log" warn;   #debug or warn
    log_not_found   on; #enables or disables messages in error_log about files not found on disk. 
    rewrite_log off;

    fastcgi_intercept_errors off;   # Do Not Change (off) !


    gzip  off;

    index  index.php index.htm index.html;

    server {


        # NEVER ALLOW PUBLIC ACCESS TO THIS SERVER !!!
        # Instead, create projects using WinNMP Manager, and allow public access only to those projects!
        # How to allow access from LAN and Internet to your local project:
        # http://WinNMP.wtriple.com/howtos.php#How-to-allow-access-from-LAN-and-Internet-to-your-local-project

        listen      127.0.0.1:80    default_server;     # Do Not Change ! Security Risk !
        #listen     [::1]:80    ipv6only=on;        # Do Not Change ! Security Risk !
        server_name localhost;              # Do Not Change ! Security Risk !

        # This directive is modified automatically by WinNMP.exe for portability.
        root        "c:/winnmp/www";
        autoindex on;
        
        allow       127.0.0.1;  # Do Not Change ! Security Risk !
        allow       ::1;        # Do Not Change ! Security Risk !
        deny        all;        # Do Not Change ! Security Risk !

        ## deny access to .htaccess files, if Apache's document root concurs with nginx's one
        location ~ /\.ht {
            deny  all;
        }

        location = /favicon.ico {
                log_not_found off; 
        }
        location = /robots.txt {
                log_not_found off; 
        }


        ## Tools are now served from include/tools/
        location ~ ^/tools/.*\.php$ {                   
            root "c:/winnmp/include";
            try_files $uri =404; 
            include     nginx.fastcgi.conf;
            fastcgi_pass    php_farm;
            allow       127.0.0.1;      # Do Not Change ! Security Risk !
            allow       ::1;            # Do Not Change ! Security Risk !
            deny        all;            # Do Not Change ! Security Risk !
        }
        location ~ ^/tools/ {
            root "c:/winnmp/include";
            allow       127.0.0.1;      # Do Not Change ! Security Risk !
            allow       ::1;            # Do Not Change ! Security Risk !
            deny        all;            # Do Not Change ! Security Risk !
        }


        ## How to add phpMyAdmin 
        ## Copy phpMyAdmin files to c:/winnmp/include/phpMyAdmin then uncomment:

        #location ~ ^/phpMyAdmin/.*\.php$ {
        #   root "c:/winnmp/include";
        #   try_files $uri =404; 
        #   include         nginx.fastcgi.conf;
        #   fastcgi_pass    php_farm;
        #   allow           127.0.0.1;  
        #   allow           ::1;
        #   deny            all;
        #}       
        #location ~ ^/phpMyAdmin/ {
        #   root "c:/winnmp/include";
        #}

        ## Notice that the root directive lacks /phpMyAdmin because Nginx adds the URL path /phpMyAdmin to the root path, so the resulting directory is c:/winnmp/include/phpMyAdmin
        

        ## PHP for localhost ##
        #######################

        location ~ \.php$ {
            try_files $uri =404; 
            include     nginx.fastcgi.conf;
            include     nginx.redis.conf;
            fastcgi_pass    php_farm;
            allow       127.0.0.1;      # Do Not Change ! Security Risk !
            allow       ::1;            # Do Not Change ! Security Risk !
            deny        all;            # Do Not Change ! Security Risk !
            }

        # How to allow access from LAN and Internet to your local project:
        # http://WinNMP.wtriple.com/howtos.php#How-to-allow-access-from-LAN-and-Internet-to-your-local-project



    }

        

    include domains.d/*.conf;


    include nginx.phpfarm.conf;
}

Considering I'm on windows machine, I'm using WinNMP to run NGINX.

The things that I believe that are important, that I have checked are:

  • The payload is an array containing 2 short strings ( up to 5 characters ) with no strange characters
  • I have re-worked the tour-addons.php script to only return a string "ok" and 200 HTTP code, to make sure the the amount of data going over a network is not an issue - still the same behavior
  • Following the online responses, I have updated my fastcgi parameters and have restarted NGINX, but still the same result.
  • I have also tried to make the intervals between the requests to be longer ( up to 10-15 seconds ) to make sure that I am not sending too many requests in short amount of time, but still with the same result. Even with 30 seconds in between the requests, for the SAME request, every 4th fails.
  • If I use postman to send the requests, the same behavior is repeated with 502 bad gateway having the error: The server was acting as a gateway or proxy and received an invalid response from the upstream server.

Thank you in advance!

MadB
  • 1
  • 1
  • `php nginx 502` give multiple hints, like: [“502 bad gateway” in Nginx – Top 5 reasons for it, and how to resolve it](https://bobcares.com/blog/502-bad-gateway-nginx/#:~:text=502%20Bad%20Gateway%20in%20Nginx%20commonly%20occurs%20when,for%20this%20error%2C%20and%20how%20to%20fix%20it.), But also on stackoverflow: [nginx 502 bad gateway](https://stackoverflow.com/questions/4252368/nginx-502-bad-gateway). Did you check any of those links? – Luuk Jan 31 '23 at 16:33
  • What is the value of constant IBE_URL ? What is the configuration of php_farm upstream ? – Aleksey Vaganov Jan 31 '23 at 16:34
  • @AlekseyVaganov Thank you. I have tried eveything from the offered links. The constant IBE url, is just a url to my localhost ( working link ). The phpfarm configuration is the following: `upstream php_farm { server 127.0.0.1:9001 weight=1 max_fails=1 fail_timeout=1; server 127.0.0.1:9002 weight=1 max_fails=1 fail_timeout=1; server 127.0.0.1:9003 weight=1 max_fails=1 fail_timeout=1; }` – MadB Jan 31 '23 at 16:52
  • Check the health of php-fpm services that listens the 127.0.0.1:9001, 127.0.0.1:9002, 127.0.0.1:9003 – Aleksey Vaganov Jan 31 '23 at 21:00

0 Answers0