Score:0

Nginx 502 Issue with the API & php-fpm

co flag

I've configured the new dev machine but can't get nginx working with the API as it is supposed to. The current error is that 'curl localhost/api' returns a 502.

Host:

    Nginx
    PHP 8.1.x, php-fpm
    - ext-ctype, ext-fileinfo, ext-iconv, ext-intl, ext-libxml,
    - ext-mbstring, ext-openssl, ext-pdo, ext-simplexml
    - composer 2.x
    FOP Version 1.1 (java for pdf))
    postgresql14, postgresql14-contrib


**ls**
conf.d                  koi-win             nginx.conf.save.2
default.d.original      mime.types          scgi_params
fastcgi.conf            mime.types.default  scgi_params.default
fastcgi.conf.default    nginx.conf          uwsgi_params
fastcgi_params          nginx.conf.default  uwsgi_params.default
fastcgi_params.default  nginx.conf.save     win-utf
koi-utf                 nginx.conf.save.1

Nginx .conf

 user nginx;
    worker_processes auto;
    error_log /var/log/nginx/error.log;
    pid /run/nginx.pid;
    
    # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
    include /usr/share/nginx/modules/*.conf;
    
    events {
        worker_connections 1024;
    }
    
    http {
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
        access_log  /var/log/nginx/access.log  main;
    
        sendfile            on;
        tcp_nopush          on;
        tcp_nodelay         on;
        keepalive_timeout   65;
        types_hash_max_size 4096;
    
        include             /etc/nginx/mime.types;
        default_type        application/octet-stream;
    
        # Load modular configuration files from the /etc/nginx/conf.d directory.
        # See http://nginx.org/en/docs/ngx_core_module.html#include
        # for more information.
        include /etc/nginx/conf.d/*.conf;
    
        server {
            listen       80;
            listen       [::]:80;
            server_name  _;
            root         /usr/share/nginx/html;
    
            # Load configuration files for the default server block.
            include /etc/nginx/default.d/*.conf;
    
            error_page 404 /404.html;
                                       

global.conf

 vim conf.d/global.conf
server {
    listen 8872;
    root /var/www/mysite.com-api/public;
    location / {
        try_files $uri /index.php$is_args$args;
    }
    location ~ ^/(index|captcha)\.php(/|$) {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_buffer_size 256k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 512k;
        fastcgi_read_timeout 240;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $document_root;
    }
    location ~ \.php$ {
        return 404;
    }
    error_log /var/log/nginx/mysite.com-error.log;
    access_log /var/log/nginx/mysite.com-access.log;
}
server {
    listen 80;
    root /var/www/mysite.com-ui;
    location /api {
        proxy_pass http://localhost:8872;
    }
    location /admin {
        rewrite  ^/admin/(.+)$ /$1 break;
        proxy_pass http://localhost:81;
        proxy_redirect off;
    }
    location / {
        try_files $uri /index.html;
    }
    error_log /var/log/nginx/mysite.com-ui-error.log;
    access_log /var/log/nginx/mysite.com-ui-access.log;
}
server {
    listen 81;
    root /var/www/mysite.com-admin-ui;
    location / {
        try_files $uri $uri/ /index.html;
    }
    error_log /var/log/nginx/mysite.com-admin-ui-error.log;
                                                            
    
    

Logs:

[root@ip-10-1-15-19 nginx]# ls -lah /var/log/nginx
total 32K
drwxr-xr-x  2 root  root  283 May 24 06:58 .
drwxr-xr-x 11 root  root 4.0K May 24 03:34 ..
-rwxr-xr-x  1 nginx root    0 May 24 03:34 access.log
-rwxr-xr-x  1 root  root  133 May 23 17:20 access.log-20220524.gz
-rwxr-xr-x  1 nginx root 2.0K May 24 08:14 error.log
-rwxr-xr-x  1 root  root  332 May 23 17:28 error.log-20220524.gz
-rwxr-xr-x  1 nginx root  978 May 23 18:33 mysite.com-access.log
-rwxr-xr-x  1 nginx root    0 May 23 17:23 mysite.com-admin-ui-access.log
-rwxr-xr-x  1 nginx root    0 May 23 17:23 mysite.com-admin-ui-error.log
-rwxr-xr-x  1 nginx root 2.6K May 23 18:33 mysite.com-error.log
-rwxr-xr-x  1 nginx root 2.2K May 24 08:24 mysite.com-ui-access.log
-rwxr-xr-x  1 nginx root 1008 May 23 17:32 mysite.com-ui-error.log
[root@ip-10-1-15-19 nginx]# tail -f /var/log/nginx/access.log

Nginx Logs

      sudo tail -f /var/log/nginx/access.log /var/log/nginx/error.log
    ==> /var/log/nginx/access.log <==
    
    ==> /var/log/nginx/error.log <==
    2022/05/24 08:14:50 [emerg] 10638#10638: bind() to 0.0.0.0:8872 failed (98: Address already in use)
    2022/05/24 08:14:50 [emerg] 10638#10638: bind() to 0.0.0.0:80 failed (98: Address already in use)
    2022/05/24 08:14:50 [emerg] 10638#10638: bind() to 0.0.0.0:81 failed (98: Address already in use)
    2022/05/24 08:14:50 [emerg] 10638#10638: bind() to [::]:80 failed (98: Address already in use)
    2022/05/24 08:14:50 [emerg] 10638#10638: bind() to 0.0.0.0:8872 failed (98: Address already in use)
    2022/05/24 08:14:50 [emerg] 10638#10638: bind() to 0.0.0.0:80 failed (98: Address already in use)
    2022/05/24 08:14:50 [emerg] 10638#10638: bind() to 0.0.0.0:81 failed (98: Address already in use)
    2022/05/24 08:14:50 [emerg] 10638#10638: bind() to [::]:80 failed (98: Address already in use)
    2022/05/24 08:14:50 [emerg] 10638#10638: still could not bind()
    2022/05/24 14:12:14 [notice] 2911#2911: signal process started

sudo netstat -plant | grep 80

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2769/nginx: master
tcp6       0      0 :::80                   :::*                    LISTEN      2769/nginx: master

# for more information.

 include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

did this change in config

listen 80;
listen [::]:80 ipv6only=on default_server;

still same 502 error occur.

sudo ps aux | grep 'php'
root      2854  0.0  0.2 252816  5428 ?        Ss   May24   0:02 php-fpm: master process (/etc/php-fpm.conf)
apache    2855  0.0  0.2 252816  5132 ?        S    May24   0:00 php-fpm: pool www
apache    2856  0.0  0.2 252816  5132 ?        S    May24   0:00 php-fpm: pool www
apache    2857  0.0  0.2 252816  5132 ?        S    May24   0:00 php-fpm: pool www
apache    2858  0.0  0.2 252816  5132 ?        S    May24   0:00 php-fpm: pool www
apache    2859  0.0  0.2 252816  5132 ?        S    May24   0:00 php-fpm: pool www
root      7230  0.0  0.0 119420   988 pts/0    S+   06:53   0:00 grep --color=auto PHP

Is there a possibility that there is some problem with the conf.d/global.conf file, its throw 502?

can anyone suggest how to troubleshoot or fix it?

Roid avatar
cg flag
You should try consume the API directly if you API is accessible via localhost you can try using a SSH tunnel to test your API using "curl localhost:8872/api"
Tim avatar
gp flag
Tim
"The HyperText Transfer Protocol (HTTP) 502 Bad Gateway server error response code indicates that the server, while acting as a gateway or proxy, received an invalid response from the upstream server."
Ivan Shatsky avatar
gr flag
The corresponding entry from the nginx error log could be very helpful.
Santosh Baruah avatar
co flag
I test the API but same problem persist : curl localhost:8872/api <html> <head><title>502 Bad Gateway</title></head> <body> <center><h1>502 Bad Gateway</h1></center> <hr><center>nginx/1.20.0</center> </body> </html>
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.