AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题

问题[502-error](server)

Martin Hope
Alain Alemany
Asked: 2020-08-29 20:46:56 +0800 CST

Wordpress 导入过程以 502 Bad Gateway 错误结束

  • 1

我有一个 Wordpress 网站,现在在 LEMP 和 Ubuntu 中完美运行了一段时间。

但是,今天我必须从另一个 Wordpress 导入以前导出的 XML,在此过程中我收到 502 Bad Gateway 错误。

检查error.log,我看到了这个:

2020/08/29 00:16:48 [error] 7#7: *954359 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 189.11.0.19, server: example.com, request: "POST /wp-admin/admin.php?import=wordpress&step=2 HTTP/1.1", upstream: "fastcgi://189.11.0.19:9001", host: "example.com", referrer: "https://example.com/wp-admin/admin.php?import=wordpress&step=1&_wpnonce=ef40a27516"

为什么会发生这种情况?我怎样才能解决这个问题?

提前致谢。

温暖的问候。

nginx wordpress 502-error import
  • 1 个回答
  • 916 Views
Martin Hope
Jean
Asked: 2020-07-07 21:44:37 +0800 CST

Laravel docker cron nginx 502 网关错误问题

  • 0

我在 docker 容器中使用我的 laravel 应用程序。一切正常,直到我向 dockerfile 添加了一个 cron。我需要安排一份工作,所以我需要一个 cron。我的撰写文件看起来像这样

version: '3'

networks:
  laravel:
    driver: bridge

services:
  nginx:
    image: nginx:stable
    container_name: nginx
    ports:
      - "8080:80"
    volumes:
      - ./src:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
      - mysql
    networks:
      - laravel

  php:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: php
    volumes:
      - ./src:/var/www/html
    ports:
      - "9000:9000"
    networks:
      - laravel

nginx 配置文件看起来像这样

server {
    listen 80;
    index index.php index.html;
    server_name localhost;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html/public;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

我的 docker 文件看起来像这样

FROM php:7.3-fpm

WORKDIR /var/www/html

# RUN docker-php-ext-install pdo pdo_mysql mysqli
# RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
RUN docker-php-ext-install mysqli pdo pdo_mysql && docker-php-ext-enable mysqli

# Install cron
RUN apt-get update && apt-get install -y cron

# Add crontab file in the cron directory
ADD src/app/schedule/crontab /etc/cron.d/cron

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/cron

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

# Run the command on container startup
CMD printenv > /etc/environment && echo "cron starting..." && (cron) && : > /var/log/cron.log && tail -f /var/log/cron.log

并且 nginx 日志中的错误是

2020/07/06 08:27:06 [error] 5#5: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://172.19.0.2:9000", host: "localhost:8080"

nginx 正在运行

/nginx - 172.19.0.7

这里出了什么问题?

nginx docker 502-error
  • 1 个回答
  • 2409 Views
Martin Hope
user2586441
Asked: 2020-05-07 05:42:49 +0800 CST

带有嵌套代理的 Nginx 自定义 502 页面

  • 0

我知道如何为上游错误创建自定义 502 页面,并且多次没有问题,但是在这种特殊情况下,一个代理响应被传递给另一个代理,我找不到正确的配置。

一般的工作流程是这样的:用户请求图片的缩略图,nginx 将此请求代理到图片存储。如果 storage 返回 404,则 nginx 将此请求代理到应用服务器,应用服务器生成带有请求 URI 的缩略图并返回代码 200,通过 nginx 传递给用户。

但是,如果用户请求完全不存在图片的缩略图,则应用服务器返回 502。在这种情况下,我想显示一个自定义页面,而不是内置的 nginx“502 Bad Gateway”。

正如我在开始时提到的,我知道如何创建自定义 502 页面,但我认为这种具有双重代理的特殊复杂设置需要一些我找不到的额外配置 :( 我尝试了一个带有内部选项的位置,我我尝试了一个带有指向 html 页面的直接 URL 的位置,我尝试了“@”位置,但 nginx 总是显示其内置页面。文件 502.html 和 502x.html 都存在于 /var/www/html 中并且是可读的通过 nginx:

ll /var/www/html
total 20
drwxr-xr-x 2 root root 4096 May  3 11:21 ./
drwxr-xr-x 4 root root 4096 Mar 24 11:50 ../
-rw-r--r-- 1 root root   36 Apr 28 10:49 502.html
-rw-r--r-- 1 root root   36 May  3 11:21 502x.html

Nginx 配置:

server {
listen 443 ssl http2;
server_name cdn.domain.tld;

ssl_certificate /etc/letsencrypt/live/cdn.domain.tld/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/cdn.domain.tld/privkey.pem;

client_max_body_size 10m;

client_body_buffer_size 128k;
proxy_connect_timeout 75;
proxy_send_timeout 300;
proxy_read_timeout 500;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_http_version 1.1;
proxy_buffering on;
proxy_redirect off;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

#locations for redirection of 502 response received from thumbnail generator. Not working :(
#error_page 502  @502;
#error_page 502  https://cdn.domain.tld/502x.html;
error_page 502  /502.html;
location =/502.html {
   root /var/www/html;
   internal;
}
location @502 {
   root /var/www/html;
   try_files /502x.html 502;
}
location = /502x.html {
   root /var/www/html;
   index 502x.html;
}

location  / {
    proxy_pass http://192.168.10.5/thumbs$request_uri;

    #Sets caching time for different response codes. If only caching time is specified  then only 200, 301, and 302 responses are cached.
    proxy_cache_valid 30m;

    add_header X-Proxy-Thumbs-Cache $upstream_cache_status;

    #intercept 404 from backend
    #from nginx docs:
    #If an error response is processed by a proxied server or a FastCGI/uwsgi/SCGI/gRPC server,.
    #and the server may return different response codes (e.g., 200, 302, 401 or 404), it is possible to respond with the code it returns (equal sign does that)
    #So we proxy 404 to app which generates thumbnail and returns it with 200 code
    proxy_intercept_errors on;
    error_page 404 = @not-found;

    access_log /var/log/nginx/cdn.access.log cachelog;
    error_log /var/log/nginx/cdn.error.log;
}

location @not-found {
   proxy_pass http://192.168.10.12/thumbgen?key=$request_uri;

   add_header X-Proxy-Thumbs-Cache2 $upstream_cache_status;

   proxy_intercept_errors on;

   access_log /var/log/nginx/cdn-404.access.log cachelog;
   error_log /var/log/nginx/cdn-404.error.log;
}

}

nginx 502-error custom-errors
  • 1 个回答
  • 560 Views
Martin Hope
user3783243
Asked: 2020-03-03 06:43:36 +0800 CST

Lighttpd/Nginx 无功能

  • 0

我正在使用 AWS 的 Blackboard AMI,但无法让 lighttpd 在其上运行。我不断得到502。

我跑service lighttpd status了,这让我回来了:

● lighttpd.service - Lighttpd Daemon
   Loaded: loaded (/lib/systemd/system/lighttpd.service; disabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Mon 2020-03-02 09:33:49 EST; 4min 31s ago
  Process: 4770 ExecStart=/usr/sbin/lighttpd -D -f /etc/lighttpd/lighttpd.conf (code=exited, status=255)
  Process: 4763 ExecStartPre=/usr/sbin/lighttpd -t -f /etc/lighttpd/lighttpd.conf (code=exited, status=0/SUCCESS)
 Main PID: 4770 (code=exited, status=255)

Mar 02 09:33:49 ip-172-31-27-103 systemd[1]: Stopped Lighttpd Daemon.
Mar 02 09:33:49 ip-172-31-27-103 systemd[1]: Starting Lighttpd Daemon...
Mar 02 09:33:49 ip-172-31-27-103 lighttpd[4763]: Syntax OK
Mar 02 09:33:49 ip-172-31-27-103 systemd[1]: Started Lighttpd Daemon.
Mar 02 09:33:49 ip-172-31-27-103 lighttpd[4770]: 2020-03-02 09:33:49: (network.c.409) can't bind to port:  443 Address already in use
Mar 02 09:33:49 ip-172-31-27-103 systemd[1]: lighttpd.service: Main process exited, code=exited, status=255/n/a
Mar 02 09:33:49 ip-172-31-27-103 systemd[1]: lighttpd.service: Unit entered failed state.
Mar 02 09:33:49 ip-172-31-27-103 systemd[1]: lighttpd.service: Failed with result 'exit-code'.

然后我跑去netstat -tulpn查看正在使用的内容443并返回:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1134/sshd       
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      1269/postgres   
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      1895/nginx.conf 
tcp6       0      0 :::22                   :::*                    LISTEN      1134/sshd       
tcp6       0      0 :::9900                 :::*                    LISTEN      1963/node       
udp        0      0 0.0.0.0:68              0.0.0.0:*                           903/dhclient    
udp        0      0 172.31.27.103:123       0.0.0.0:*                           1230/ntpd       
udp        0      0 127.0.0.1:123           0.0.0.0:*                           1230/ntpd       
udp        0      0 0.0.0.0:123             0.0.0.0:*                           1230/ntpd       
udp6       0      0 fe80::cc5:7dff:fee9:123 :::*                                1230/ntpd       
udp6       0      0 ::1:123                 :::*                                1230/ntpd       
udp6       0      0 :::123                  :::*                                1230/ntpd       

然后我尝试了各种命令来查看它的状态,nginx但它们都返回它没有在我的机器上运行。

Failed to restart nginx.service: Unit nginx.service not found.
sudo: /usr/sbin/nginx: command not found

可以nginx在另一个服务下运行,或者我怎样才能让我的服务器正常运行?没有为 AMI 提供支持。

更新ps -efl|grep 1895 | grep -v "grep"返回:

4 S root      1895  1864  0  80   0 - 14857 sigsus 08:07 ?        00:00:00 nginx: master process /usr/local/openresty/bin/openresty -c /usr/local/etc/openresty/nginx.conf
5 S nobody    1911  1895  0  80   0 - 15113 ep_pol 08:07 ?        00:00:00 nginx: worker process
5 S nobody    1912  1895  0  80   0 - 15113 ep_pol 08:07 ?        00:00:00 nginx: worker process
nginx lighttpd 502-error amazon-ami
  • 1 个回答
  • 270 Views
Martin Hope
Dimitar
Asked: 2017-01-25 01:02:43 +0800 CST

502 坏网关 nginx 和 apache 服务器

  • 1

您好,您能帮我在我的 ubuntu 16.04 服务器上设置 apache 和 nginx 服务器,但是当我尝试访问 localhost/info.php 示例时,我得到了

502 错误网关 nginx/1.9.15 (Ubuntu)

错误 502 bad gateway,这是来自错误日志文件:

2017/01/24 10:50:55 [错误] 14774#14774: *29 connect() 在连接到上游时失败(111:连接被拒绝),客户端:127.0.0.1,服务器:example.com,请求:“GET /info.php HTTP/1.1”,上游:“ http://127.0.0.1:8080/info.php ”,主机:“localhost”

我的 /etc/apache2/ports.conf 是:

Listen 8000

<IfModule ssl_module>
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>

我的 /etc/nginx/sites-available/default 文件是:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

    server_name 127.0.0.1;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        proxy_pass http://127.0.0.1:8000;
            include /etc/nginx/proxy_params;
    }

    location ~* \.(js|css|jpg|jpeg|gif|png|svg|ico|pdf|html|htm)$ {
        expires      30d;
        }

    }

       location @proxy {
                proxy_pass http://127.0.0.1:8000;
                include /etc/nginx/proxy_params;
       }
       location ~* \.php$ {
                proxy_pass http://127.0.0.1:8000;
                include /etc/nginx/proxy_params;
       }
}

我的 /etc/nginx/nginx.conf 文件:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

/var/www/html 文件夹的权利和所有权:

➜  html sudo ls -lah /var/www/html          
общо 40K
drwx--s--x  6 dimitar www-data 4,0K яну 24 10:44 .
drwxr-sr-x  3 dimitar www-data 4,0K дек 28 12:26 ..
-rw-r-Sr--  1 dimitar www-data  12K яну 24 00:52 index.html
-rw-r--r--  1 dimitar www-data   18 яну 24 10:44 info.php
drwxrwsr-x  6 dimitar www-data 4,0K яну 10 14:02 Laracast
drwxrwsr-x  2 dimitar www-data 4,0K яну 23 20:17 phpmyadmin
drwxrwxrwx  5 dimitar www-data 4,0K дек 16 11:28 softuni
drwxrwsr-x 12 dimitar www-data 4,0K яну  3 20:04 users

sudo netstat -lntp 的输出:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:7845            0.0.0.0:*               LISTEN      5054/skype      
tcp        0      0 127.0.0.1:30666         0.0.0.0:*               LISTEN      31454/Viber     
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      2521/mysqld     
tcp        0      0 0.0.0.0:139             0.0.0.0:*               LISTEN      2523/smbd       
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      14328/nginx -g daem
tcp        0      0 127.0.0.1:4371          0.0.0.0:*               LISTEN      28075/spotify   
tcp        0      0 127.0.0.1:5940          0.0.0.0:*               LISTEN      2301/teamviewerd
tcp        0      0 0.0.0.0:57621           0.0.0.0:*               LISTEN      28075/spotify   
tcp        0      0 127.0.0.1:45112         0.0.0.0:*               LISTEN      31454/Viber     
tcp        0      0 0.0.0.0:220             0.0.0.0:*               LISTEN      1233/sshd       
tcp        0      0 127.0.0.1:4381          0.0.0.0:*               LISTEN      28075/spotify   
tcp        0      0 0.0.0.0:445             0.0.0.0:*               LISTEN      2523/smbd       
tcp6       0      0 :::139                  :::*                    LISTEN      2523/smbd       
tcp6       0      0 :::220                  :::*                    LISTEN      1233/sshd       
tcp6       0      0 :::445                  :::*                    LISTEN      2523/smbd       
tcp6       0      0 :::8000                 :::*                    LISTEN      15002/apache2  

/etc/nginx/proxy_params 的内容:

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

client_max_body_size 100M;
client_body_buffer_size 1m;
proxy_intercept_errors on;
proxy_buffering on;
proxy_buffer_size 128k;
proxy_buffers 256 16k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_max_temp_file_size 0;
proxy_read_timeout 300;

有人可以帮我一把吗?

nginx apache-2.4 502-error 502
  • 1 个回答
  • 13148 Views
Martin Hope
olhur
Asked: 2016-08-28 13:08:49 +0800 CST

使用 https 在 nginx 后面设置 GitLab 并在不同的端口上响应

  • 0

亲爱的服务器故障社区,

我最近在运行 Ubuntu14.04 的 VPS 上安装了捆绑的 GitLab。为了让它运行,我使用了 nginx,它通过 http 将一个特定的子域路由到 GitLab。到目前为止一切顺利,它工作正常,不会与安装的其他服务发生冲突,即 Taiga.io。

由于不使用 https 是一个非常糟糕的主意,我决定改变它 - 我使用 Let's Encrypt 来获取我的域和子域的证书,然后进行相应的设置。Taiga.io 顺利进行,没有任何问题,然后我继续使用 GitLab。

不幸的是,当尝试在 GitLab 上设置 https 时,我遇到了502 Bad Gateway nginx/1.4.6 (Ubuntu)错误。

这是我的设置:

/etc/gitlab/gitlab.rb:
external_url 'https://gitlab.example.com:8090'
nginx['enable'] = false

/opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml:
host: gitlab.example.com
port: 8090
https: true

/etc/ngingx/sites-enabled/gitlab:
server {
    listen 80;
    server_name gitlab.example.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    server_name gitlab.example.com;

    large_client_header_buffers 4 32k;
    client_max_body_size 50M;
    charset utf-8;

    access_log /opt/gitlab/logs/nginx.access.log;
    error_log /opt/gitlab/logs/nginx.error.log;

    location / {
        proxy_pass http://127.0.0.1:8090;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto  $scheme;
    }
    add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
    # This line was taken from another config file, not sure if those pins shouldn't be changed or something?
    add_header Public-Key-Pins 'pin-sha256="klO23nT2ehFDXCfx3eHTDRESMz3asj1muO+4aIdjiuY="; pin-sha256="633lt352PKRXbOwf4xSEa1M517scpD3l5f79xMD9r9Q="; max-age=2592000; includeSubDomains';

    ssl on;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!3DES:!MD5:!PSK';
    ssl_session_cache shared:SSL:10m;
    ssl_dhparam /etc/ssl/dhparam.pem;
    ssl_stapling on;
    ssl_stapling_verify on;
}

我在想,关于这方面的每个教程都建议将端口设置为 443(显然),但我认为这里不应该是这种情况,因为无论如何我都在 nginx 中从 443 重定向到:8090。我错过了什么吗?

此致!

菲利普

==================

编辑:

我看到sudo netstat -plutn在 8090 上没有显示任何监听。看起来我把配置文件搞砸了?我会期待的。

ssl nginx https gitlab 502-error
  • 2 个回答
  • 1783 Views
Martin Hope
davidhq
Asked: 2016-04-22 02:02:51 +0800 CST

nginx默认错误页面由于某种原因停止工作

  • 1

这曾经有效:

server {
  server_name example.com;

  location / {
    proxy_pass http://localhost:8888;
    error_page 502 /502.html;
  }

  location = /502.html {
    root /var/www/example;
  }
}

现在突然它不再...我不知道发生了什么变化?

/var/www/example/index.html如果端口 8888 没有响应,我会得到 404 而不是显示...

如何再次使这项工作?谢谢你

nginx 502-error
  • 2 个回答
  • 314 Views
Martin Hope
Bartosz Kowalczyk
Asked: 2012-05-17 05:27:16 +0800 CST

Nginx:502 错误网关

  • 2

我尝试安装 nginx。我以前用 lighttpd 但现在必须安装 nginx。你能帮我配置nginx吗?

我使用 debian 6。我的nginx.conf:

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

我的网站启用/默认看起来像

server {
    listen   80; ## listen for ipv4; this line is default and implied
    #listen   [::]:80 default ipv6only=on; ## listen for ipv6

    root /var/www; #!
    index index.php index.html index.htm; #1
    access_log      /var/log/nginx/access.log info;
    error_log       /var/log/nginx/error.log info;

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to index.html
            autoindex on;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }

    location /doc/ {
            alias /usr/share/doc/;
            autoindex on;
            allow 127.0.0.1;
            deny all;
    }

    location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one

    location ~ /\.ht {
            deny all;
    }
}

我添加到php.ini

cgi.fix_pathinfo = 0;     

    

php5-cgi 运行为

spawn-fcgi 127.0.0.1 -p 9000 -u www-data -f /usr/bin/php5-cgi

但是我收到502 错误(错误的网关)
我/var/log/说

2012/05/16 15:35:25 [error] 13427#0: *12 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /ndex.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost"

我没有运行sqpwn-fcgi=>php-cgi没有在监听端口9000。

configuration nginx 502-error
  • 2 个回答
  • 13623 Views
Martin Hope
user39547
Asked: 2010-04-03 05:45:46 +0800 CST

Nginx + PHP-FPM = "随机" 502 错误网关

  • 7

我正在运行 Nginx 并通过 FastCGI 将 php 请求代理到 PHP-FPM 进行处理。我将随机收到 502 Bad Gateway 错误页面 - 我可以通过非常快速地单击我的 PHP 网站/刷新页面一两分钟来重现此问题。当我得到 502 错误页面时,我所要做的就是刷新浏览器并正确刷新页面。

这是我的设置:

nginx/0.7.64 PHP 5.3.2 (fpm-fcgi) (内置: Apr 1 2010 06:42:04) Ubuntu 9.10 (Latest 2.6 Paravirt)

我使用这个 ./configure 指令编译了 PHP-FPM

./configure --enable-fpm --sysconfdir=/etc/php5/conf.d --with-config-file-path=/etc/php5/conf.d/php.ini --with-zlib --with -openssl --enable-zip --enable-exif --enable-ftp --enable-mbstring --enable-mbregex --enable-soap --enable-sockets --disable-cgi --with-curl --with -curlwrappers --with-gd --with-mcrypt --enable-memcache --with-mhash --with-jpeg-dir=/usr/local/lib --with-mysql=/usr/bin/mysql -- with-mysqli=/usr/bin/mysql_config --enable-pdo --with-pdo-mysql=/usr/bin/mysql --with-pdo-sqlite --with-pspell --with-snmp --with- sqlite --with-tidy --with-xmlrpc --with-xsl

我的 php-fpm.conf 看起来像这样(相关部分):

 ...   
<value name="pm">
     <value name="max_children">3</value>
     ...
     <value name="request_terminate_timeout">60s</value>
       <value name="request_slowlog_timeout">30s</value>
       <value name="slowlog">/var/log/php-fpm.log.slow</value>
       <value name="rlimit_files">1024</value>
       <value name="rlimit_core">0</value>
       <value name="chroot"></value>
       <value name="chdir"></value>
       <value name="catch_workers_output">yes</value>
       <value name="max_requests">500</value>
...

我已经尝试将 max_children 增加到 10 并且没有区别。我还尝试将其设置为“动态”并将 max_children 设置为 50,并将 start_server 设置为“5”,没有任何区别。

我尝试过同时使用 1 个和 5 个 nginx 工作进程。

我的 fastcgi_params conf 看起来像:

fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
fastcgi_param  REDIRECT_STATUS    200;

Nginx 将错误记录为:

[错误] 3947#0:* 10530 connect() 失败(111:连接被拒绝)同时连接到上游,客户端:68.40.xxx.xxx,服务器:www.domain.com,请求:“GET /favicon.ico HTTP/ 1.1”,上游:“fastcgi://127.0.0.1:9000”,主机:“www.domain.com”

PHP-FPM 在发生错误时记录以下内容:

[NOTICE] pid 17161, fpm_unix_init_main(), line 255: getrlimit(nofile): max:1024, cur:1024 
[NOTICE] pid 17161, fpm_event_init_main(), line 93: libevent: using epoll 
[NOTICE] pid 17161, fpm_init(), line 50: fpm is    running, pid 17161 
[DEBUG] pid 17161, fpm_children_make(), line 403: [pool default] child 17162 started 
[DEBUG] pid 17161, fpm_children_make(), line   403: [pool default] child 17163    started 
[DEBUG] pid 17161,  fpm_children_make(), line 403: [pool default] child 17164 started 
[NOTICE] pid 17161, fpm_event_loop(), line 111: ready to handle connections

当我重新创建问题时,我的 CPU 使用率最高可达 10-15%。我的免费内存 (free -m) > 130MB

当我使用 php5-cgi 来服务我的 php 请求时,我遇到了这个间歇性的 502 Bad Gateway 问题。有谁知道如何解决这一问题?

编辑/更新:我正在使用主管启动 php-fpm (因此没有被取消)。

fastcgi nginx php-fpm 502-error
  • 2 个回答
  • 8818 Views
Martin Hope
Sean
Asked: 2010-03-13 01:21:33 +0800 CST

nginx 502 坏网关 - fastcgi 不听?(Debian 5)

  • 4

我有使用 nginx 的经验,但它总是为我预先安装(通过 VPS.net 预配置图像)。我真的很喜欢它为我做的事情,现在我正在尝试使用 apt-get 将它安装在我自己的服务器上。这是一个相当新鲜的 Debian 5 安装。我安装了一些额外的软件包,但它们都是 .deb 的,没有手动编译或任何疯狂的事情。

Apache 已经安装,但我禁用了它。我做了 apt-get install nginx 并且效果很好。根据我的需要更改了一些配置,尽管我将要描述的相同问题即使使用默认配置也会发生。

我花了一段时间才弄清楚 nginx 的默认 debian 包不会自动生成 fastcgi 进程。这很蹩脚,但我想出了如何使用这个脚本来做到这一点,我发现它发布在许多不同的网站上:

#!/bin/bash

## ABSOLUTE path to the PHP binary
PHPFCGI="/usr/bin/php5-cgi"

## tcp-port to bind on
FCGIPORT="9000"

## IP to bind on
FCGIADDR="127.0.0.1"

## number of PHP children to spawn
PHP_FCGI_CHILDREN=10

## number of request before php-process will be restarted
PHP_FCGI_MAX_REQUESTS=1000

# allowed environment variables sperated by spaces
ALLOWED_ENV="ORACLE_HOME PATH USER"

## if this script is run as root switch to the following user
USERID=www-data

################## no config below this line

if test x$PHP_FCGI_CHILDREN = x; then
  PHP_FCGI_CHILDREN=5
fi

ALLOWED_ENV="$ALLOWED_ENV PHP_FCGI_CHILDREN"
ALLOWED_ENV="$ALLOWED_ENV PHP_FCGI_MAX_REQUESTS"
ALLOWED_ENV="$ALLOWED_ENV FCGI_WEB_SERVER_ADDRS"

if test x$UID = x0; then
  EX="/bin/su -m -c \"$PHPFCGI -q -b $FCGIADDR:$FCGIPORT\" $USERID"
else
  EX="$PHPFCGI -b $FCGIADDR:$FCGIPORT"
fi

echo $EX

# copy the allowed environment variables
E=

for i in $ALLOWED_ENV; do
  E="$E $i=${!i}"
done

# clean environment and set up a new one
nohup env - $E sh -c "$EX" &> /dev/null &

当我执行“ps -A | grep php5-cgi”时,我看到 10 个进程正在运行,应该准备好收听了。

但是当我尝试通过 nginx 查看网页时,我只收到 502 bad gateway 错误。

经过一番折腾,我尝试远程登录到 127.0.0.1 9000(fastcgi 正在侦听端口 9000,并且 nginx 被配置为与该端口通信),但它只是立即关闭了连接。

这让我觉得问题出在 fastcgi 上,但我不确定我能做些什么来测试它。它可能只是关闭连接,因为它没有得到任何要处理的数据,但它会立即关闭,这让我不这么认为。

所以……有什么建议吗?我想不通。凌晨 1 点没用,但我要疯了!

php debian fastcgi nginx 502-error
  • 3 个回答
  • 12238 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve