PHP-FPM 和 Nginx:502 错误网关

2024-06-19

配置

  • Ubuntu 服务器 11.10 64 位
  • 亚马逊AWS,Ec2,托管在云上
  • t1.micro实例

在我写任何其他内容之前,我想声明我已经检查了两者nginx 502 错误网关 https://stackoverflow.com/questions/4252368/nginx-502-bad-gateway and Nginx + PHP-FPM 502 网关错误 https://stackoverflow.com/questions/3616191/nginx-php-fpm-502-bad-gateway线程,不幸的是这在这方面没有帮助我。

这个问题似乎相当常见:nginx 或 php-fpm 的错误配置可能会导致502 Bad Gateway错误,这是我无法摆脱的。注意出现这个即使当我转到我的域根目录时,不指定任何特定目录。

我正在运行 Amazon EC2 Web 服务器,启用了端口 9000,打开了端口 80 等。

特别的问题是,我怎样才能摆脱这个讨厌的错误?或者,更好的是,我怎样才能得到php5-fpm to 实际上工作.

到目前为止我尝试过的

配置文件的编辑基本一致,特别是php-fpm.conf and nginx.conf.

i. php-fpm.conf

我添加了以下内容,但没有多大帮助:

;;;;;;;;;;;;;
; Fpm Start ;
;;;;;;;;;;;;;

;pm.start_servers = 20
;pm.min_spare_servers = 5
;pm.max_spare_servers = 35

现在,之后我尝试包含我的配置文件:

include=/etc/php5/fpm/*.conf

这只会让我更加困惑。

完整配置

;;;;;;;;;;;;;;;;;;;;;
; FPM Configuration ;
;;;;;;;;;;;;;;;;;;;;;

; All relative paths in this configuration file are relative to PHP's install
; prefix (/usr). This prefix can be dynamicaly changed by using the
; '-p' argument from the command line.

; Include one or more files. If glob(3) exists, it is used to include a bunch of
; files from a glob(3) pattern. This directive can be used everywhere in the
; file.
; Relative path can also be used. They will be prefixed by:
;  - the global prefix if it's been set (-p arguement)
;  - /usr otherwise
;include=/etc/php5/fpm/*.conf

;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;

[global]
; Pid file
; Note: the default prefix is /var
; Default Value: none
pid = /var/run/php5-fpm.pid

; Error log file
; Note: the default prefix is /var
; Default Value: log/php-fpm.log
error_log = /var/log/php5-fpm.log

; Log level
; Possible Values: alert, error, warning, notice, debug
; Default Value: notice
log_level = notice

; If this number of child processes exit with SIGSEGV or SIGBUS within the time
; interval set by emergency_restart_interval then FPM will restart. A value
; of '0' means 'Off'.
; Default Value: 0
;emergency_restart_threshold = 0

; Interval of time used by emergency_restart_interval to determine when 
; a graceful restart will be initiated.  This can be useful to work around
; accidental corruptions in an accelerator's shared memory.
; Available Units: s(econds), m(inutes), h(ours), or d(ays)
; Default Unit: seconds
; Default Value: 0
emergency_restart_interval = 0

; Time limit for child processes to wait for a reaction on signals from master.
; Available units: s(econds), m(inutes), h(ours), or d(ays)
; Default Unit: seconds
; Default Value: 0
;process_control_timeout = 0

; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging.
; Default Value: yes
daemonize = no

;;;;;;;;;;;;;
; Fpm Start ;
;;;;;;;;;;;;;

;pm.start_servers = 20
;pm.min_spare_servers = 5
;pm.max_spare_servers = 35

;;;;;;;;;;;;;;;;;;;;
; Pool Definitions ; 
;;;;;;;;;;;;;;;;;;;;

; Multiple pools of child processes may be started with different listening
; ports and different management options.  The name of the pool will be
; used in logs and stats. There is no limitation on the number of pools which
; FPM can handle. Your system will tell you anyway :)

; To configure the pools it is recommended to have one .conf file per
; pool in the following directory:
include=/etc/php5/fpm/pool.d/*.conf

ii. nginx.conf

老实说,这个配置只是我访问过的几个网站的一小部分,但我可以告诉你,在出现 502 Bad Gateway 业务之前,服务器运行良好(没有 PHP 工作。期间)。

The issue primarily lies in the fact that something is terribly, terribly wrong. And now, when I try to do a service php5-fpm restart, it hangs in what I'm guessing is an infinite loop or something, which I can't even CTRL-C out of.

完整配置

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

events {
    worker_connections 64;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush off;
    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;

    ##
    # 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/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

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

    server {
        listen 80;
        server_name ec2-xx-xx-xx-xx.compute-x.amazonaws.com;

        location ~ ^(.+\.php)(.*)$ {
            root   /home/wayvac/public;
            fastcgi_pass   unix:/var/run/php5-fpm.pid;  
            #fastcgi_pass   127.0.0.1:9000; #Un-comment this and comment "fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;" if you are not using php-fpm.
            fastcgi_index  index.php;
            set $document_root2 $document_root;
            if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") { set $document_root2 $1$2; }
            if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") { set $document_root2 $1$2; }
            if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") { set $document_root2 $1$2; }
            if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") { set $document_root2 $1$2; }
            if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") { set $document_root2 $1$2; }
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            fastcgi_param   SCRIPT_FILENAME $document_root2$fastcgi_script_name;
            fastcgi_param   PATH_INFO   $fastcgi_path_info;
            fastcgi_param   PATH_TRANSLATED $document_root2$fastcgi_path_info;
            include fastcgi_params;
            fastcgi_param  DOCUMENT_ROOT      $document_root2;
        }       

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

        location / {
            root /home/wayvac/public;   
            index index.html index.htm index.php;
        }

        location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
            # Some basic cache-control for static files to be sent to the browser
            expires max;
            add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }

        #include drop.conf;
        #include php.conf;
    }
}

如果有人遇到与我相同的问题找到此页面,我找到了答案here http://www.howtoforge.com/installing-nginx-with-php5-and-php-fpm-and-mysql-support-lemp-on-ubuntu-12.04-lts.

对于那些懒得点击并自行解决的人...;)

条件:

使用 NGINX 和 PHP 5.3 的 Ubuntu 或 Debian 服务器工作正常,但将 PHP 升级到 5.4 会出现 502 Bad Gateway 错误。寻找在端口 9000 上运行的服务(通常运行netstat -lp或类似)不返回任何内容。

The fix:

Open /etc/php5/fpm/pool.d/www.conf并记下“listen”参数(在我的例子中/var/run/php5-fpm.sock):

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses on a
;                            specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = /var/run/php5-fpm.sock

并将虚拟主机中的 fastcgi_pass 变量替换为您刚刚记下的位置。

所以这个示例 symfony2 配置(取自here http://wiki.nginx.org/Symfony):

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  location ~ ^/(app|app_dev)\.php(/|$) {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param  HTTPS              off;
  }

变成这样:

  # pass the PHP scripts to FastCGI server at /var/run/php5-fpm.sock
  location ~ ^/(app|app_dev)\.php(/|$) {
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param  HTTPS              off;
  }

然后重启nginx:

sudo /etc/init.d/nginx restart

Note:代替~ ^/(app|app_dev)\.php(/|$) { with ~ ^/index\.php(/|$) {如果你不在 SF2**

希望这可以节省一些人的时间:)

Edit

当然,你可以改变listen = /var/run/php5-fpm.sock to listen = 127.0.0.1:9000 in /etc/php5/fpm/pool.d/www.conf然后重新启动 php5-fpm (这将使您不必更改虚拟主机),但您必须假设他们更改了 php5-fpm 以通过套接字运行而不是侦听端口 9000,这是有原因的。

Edit2

如果您仍然遇到 502 错误,请参阅此内容answer https://stackoverflow.com/questions/23443398/nginx-error-connect-to-php5-fpm-sock-failed-13-permission-denied/23487409#23487409.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PHP-FPM 和 Nginx:502 错误网关 的相关文章

  • PHP 中的文本版本控制,具有差异突出显示

    如果您曾经在 StackOverflow 上编辑过问题 您可能已经注意到它会跟踪问题所应用的确切更改 它以一种形式显示 突出显示文本中已删除的红色部分和自特定编辑以来添加的绿色部分 我的问题是如何自己实现这样一个系统 我正在尝试使用 PHP
  • JavaScript 可以直接调用 PHP 函数吗?还是需要单独的 php 文件来调用该函数?

    我正在做一些基本的 Ajax 东西 不是 jquery 只是学习基础知识 并且我设置了一个通用结构 其中 html 调用一个 javascript 函数 该函数将数据发送到并运行特定的 php 页面 但是如果我只需要运行一个已经在funct
  • mysql utf8_general_ci 区分大小写

    我有一个 mysql 数据库 我使用 utf8 general ci 不区分大小写 在我的表中 我有一些列 例如 ID 和区分大小写的数据 例如 iSZ6fX 或 AscSc2 为了区分大写和小写 最好只在这些列上设置 utf8 bin 如
  • CakePHP 控制器的实际测试?

    我正在使用 SimpleTest 1 0 1 用 CakePHP 刚刚发布的 1 2 4 编写一个新应用程序 我已经阅读了相关章节Cookbook http book cakephp org view 366 Testing control
  • ios-使用 firebase 发送通知

    我正在尝试创建带有通知的应用程序 通知必须从 php 文件发送到 firebase 然后发送到设备 起初 我尝试使用 firebase 控制台发送 效果完美 但是当我尝试使用 php 发送通知时 我遇到了问题 显示已成功发送 但我没有收到任
  • CodeIgniter“找不到您请求的页面。”错误?

    我在使用 CodeIgniter 时遇到问题 我已经检查了互联网上所有可能的解决方案 似乎对我的情况没有任何帮助 我不是一个大专业人士 这是我第一次使用 CodeIgniter 所以不要对我严厉 路线 php route default c
  • 用于提取 FDF 数据的 PHP 正则表达式代码

    我正在尝试使用 PHP 和正则表达式解析 FDF 文件 但我就是无法理解正则表达式 我一直在解析文件以生成数组 FDF 1 2 1 0 obj lt lt FDF lt lt Fields lt lt V email protected c
  • 目录中每个文件的循环代码[重复]

    这个问题在这里已经有答案了 我有一个图片目录 我想循环遍历并对其进行一些文件计算 这可能只是睡眠不足 但我如何使用 PHP 来查找给定的目录 并使用某种 for 循环遍历每个文件 Thanks scandir http php net sc
  • 是否可以动态添加代码/扩展类?

    我想为我的代码编写一种 插件 模块 系统 如果我可以在定义类后将内容 添加 到类中 那么事情会变得更加容易 例如 这样的事情 class foo public function a return b 上课了 现在我想在定义后向其中添加另一个
  • php洗一副牌

    我想使用 php 创建随机桥手的集合 我认为我可以将有序的卡片包编码为字符串 deal下面 我喜欢它有 52 个字母 同时考虑大小写 我发现了 php 函数str shuffle 所以我想我可以做以下事情 pack abcdefghijkl
  • 在 Laravel 5 中合并 'with' 和 'whereHas'

    我在 Laravel 5 中使用 Eloquent 编写了这段代码 它工作得很好 filterTask function query use id query gt where taskid id User whereHas submiss
  • PHP:将“’”字符从 ISO-8859-1 转换为 UTF-8 时出现问题

    我在使用 PHP 将 ISO 8859 1 数据库内容转换为 UTF 8 时遇到一些问题 我正在运行以下代码来测试 Connect to a latin1 charset database and retrieve Georgia O Ke
  • PHP 和 MySQLi 中没有选择数据库错误[重复]

    这个问题在这里已经有答案了 我必须从 MySQL 数据库中选择数据 我一直在寻找答案 但仍然没有找到 我正在学习W3School http www w3schools com php php mysql select asp 我的 MySQ
  • 使用 MongoDB PHP 驱动程序时的安全问题

    我有在 MYSQL 上保护 sql 注入的经验 但是在使用 php 驱动程序的 MongoDB 上我应该注意什么 在大多数页面中 我通过 GET POST 和搜索 插入系统获取数据 我通过 UDID 其他字段进行搜索 并且可以插入任何字符串
  • PHP中如何找出特定进程仍在运行

    我正在编写一个脚本 该脚本构建其他脚本的队列 并应该管理它们的启动 管理器脚本应该知道哪个子进程已经完成 因此它可以启动在队列中等待的其他脚本 我添加了一个 echo 获取每个子进程的进程 ID 所以我有我的子进程进程 ID 现在正在使用系
  • 如何在 PHP 中获取有用的错误消息?

    我经常尝试运行 PHP 脚本 但只是得到一个空白屏幕 无错误信息 只是一个空屏幕 原因可能是一个简单的语法错误 错误的括号 缺少分号 或者函数调用失败 或者完全是其他原因 很难找出哪里出了问题 我最终注释掉了代码 在各处输入 echo 语句
  • 提交ajax表单并停留在同一页面不起作用

    我想将用户的评论存储在我的数据库中 当用户提交时 我不想将他们重定向到新页面 我有以下代码 但它不起作用 我的 HTML 代码
  • symfony2 选择单选框的默认值

    我的项目是使用 Silex 和 Symfony 组件 即表单组件 编写的 我尝试创建一组从类中构建的单选按钮 并且我想预先选择其中一个单选按钮 我创建这样的表格 form app form factory gt createBuilder
  • 当错误较多时,Laravel 不会显示错误。这是 Bug?

    重复的https github com laravel framework issues 31123 https github com laravel framework issues 31123 Laravel 版本 6 8 0 6 10
  • 用于分页的php示例脚本[关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 任何人都可以建议一个好的分页 php 脚本 其中人们想要分页显示数据库中的大量项目 以下链接可以帮助您

随机推荐

  • HttpClient SSLException

    我尝试向 Web 服务发送 https 请求 经过几次成功的尝试后 我开始不断收到此错误 这个错误是什么意思 为什么它第一次发生 javax net ssl SSLException java lang RuntimeException C
  • 什么可能会在一台服务器上导致此错误,而在另一台服务器上则不会?

    我们有一个连接到外部 Web 服务的 ASP Net 网站 几天前它突然停止工作 基本代码是这样的 Try request New ExternalWebService ProcessRequestService Error occurs
  • 如何解决这一 Mercurial 冲突?

    我对 Mercurial 和 Python 感到沮丧 因为它们让简单的事情变得困难 我有一个微不足道的冲突 由于 Mercurial 没有给出任何建议 我什至不知道如何解决这个微不足道的文件冲突 冲突是微不足道的 但如果我不能解决这个问题
  • Mac OS X Yosemite 中的 Node.js dtrace 错误

    我在 Mac OS X 10 10 Yosemite 上尝试使用 DTrace Node js 应用程序 sudo dtrace n profile 97 execname node arg1 jstack 150 8000 count t
  • 从 .resx 文件组获取所有可用区域性

    我需要以编程方式列出 resx 文件组中的可用区域性 但 ResourceManager 类似乎没有帮助 我可能有 Labels resx Labels fr FR resx Labels ro RO resx 等等 但是 我如何在运行时找
  • 如何在实体框架中完全锁定一行

    我正在处理的情况是我们正在处理金钱交易 例如 我有一个用户钱包表 其余额位于该行 UserId Wallet Id Balance 现在 在我们的网站和网络服务中 每次发生特定交易时 我们都需要 检查是否有足够的资金可用于执行该交易 从余额
  • 在 Flurry 中记录比错误 ID 更多信息的方法?

    我目前使用 iOS 版 Flurry 5 4 0 我担心在方法方面是否能够记录更多信息 而不仅仅是错误 ID void logError NSString errorID message NSString message error NSE
  • SFINAE:检测类是否具有自由函数[重复]

    这个问题在这里已经有答案了 有没有办法使用 SFINAE 来检测给定类的自由函数是否重载 基本上 我有以下解决方案 struct has no f struct has f void f has f const x template
  • NUnit 与 xUnit

    两者有什么区别NUnit http www nunit org and xUnit net https xunit net 开发其中两个而不是仅一个有什么意义 我读到 xUnit 是由 NUnit 的发明者开发的 xUnit net 是 N
  • 编译器如何如此好地优化这个阶乘函数?

    所以我一直在研究一些神奇的东西O3在 GCC 中 实际上我正在使用 Clang 进行编译 但这与 GCC 相同 我猜优化器的很大一部分是从 GCC 转移到 Clang 的 考虑这个 C 程序 int foo int n if n 0 ret
  • 调整下拉框中列表的高度[重复]

    这个问题在这里已经有答案了 可能的重复 下拉框显示的高度 https stackoverflow com questions 5600982 height of the dropdown box display hi 如何调整下拉列表中列表
  • 自动执行异步函数

    下面的代码可以完美运行 const Course mongoose model Course courseSchema async function foo const nodeCourse new Course name Node JS
  • 通过 SSL 发布 UTF-8 时,HEROKU 上出现“EOFError:已到达文件结尾”

    我在 heroku 上遇到了奇怪的错误 为了重现它 我必须使用请求正文中的任何 UTF 8 字符制作大的 超过几 KB HTTPS POST 这是一个例子 require net https require uri Accutally I
  • 如何在滚动时保持这些 tagHover 的位置靠近标签并且 tagHover 具有固定位置?

    https plnkr co edit PuP3f71kCjkqgjjMkgNS p preview https plnkr co edit PuP3f71kCjkqgjjMkgNS p preview 下面还有一个 stackoverfl
  • 是否可以在 Kivy 应用程序中读取多个 .kv 文件?

    我意识到简短的答案可能是 不 但也许这值得再问一次 如果我正在使用一个有几千行的 Kivy 应用程序 那么是否可以在另一个 kv 文件中编写一些类 这将使编辑 更正错误 进行更改等变得更加容易 只是为了澄清 KV 文件将是彼此的延续 不指向
  • 检查是否存在多列

    是否有更复杂的方法来检查数据框df包含 2 个名为Column 1 and Column 2 if numpy all map lambda c c in df columns Column 1 Columns 2 do something
  • W3WP.EXE 使用 100% CPU - 从哪里开始?

    在 IIS6 上运行的 ASP NET Web 应用程序会定期使 CPU 达到 100 在这些事件中 几乎所有的 CPU 使用情况都是由 W3WP 负责的 CPU 在几分钟到一个多小时的任何时间都保持在 100 这是在临时服务器上 此时该站
  • 如何使用现有列表创建二维数组?

    例如 我有一个名为 mazeline 的 txt 数据 如下所示 abcd cdae korp 所以我首先列出了 3 个清单 mazeline readmaze split mline0 list mazeline 0 mline1 lis
  • Akka Streams / HTTP:从响应中获取原始请求

    我有一个 Akka Streams 源 它会遍历流程并发布 HTTP 请求 source map toRequest via Http outgoingConnection host map toMessage 假设toRequest方法将
  • PHP-FPM 和 Nginx:502 错误网关

    配置 Ubuntu 服务器 11 10 64 位 亚马逊AWS Ec2 托管在云上 t1 micro实例 在我写任何其他内容之前 我想声明我已经检查了两者nginx 502 错误网关 https stackoverflow com ques