Nginx 502 API 和 php-fpm 问题

Nginx 502 API 和 php-fpm 问题

我已经配置了新的开发机器,但无法让 nginx 按照预期使用 API。当前的错误是'curl localhost/api' returns a 502.

主持人:

    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 的配置文件

 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;
                                       

全局配置文件

 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;
                                                            
    
    

日志:

[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 日志

      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

# 了解更多信息。

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

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

配置中做了这个改变

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

仍然出现相同的 502 错误。

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

conf.d/global.conf 文件是否有可能存在问题,从而引发 502?

有人可以建议如何排除故障或修复它吗?

相关内容