【问题标题】:502 Bad Gateway NGinx502 错误网关 Nginx
【发布时间】:2015-08-10 22:29:18
【问题描述】:

我正在使用 Nginx 服务器,以前从未使用过,所以如果我错过了一些愚蠢的事情,我深表歉意,但我对这一切都很陌生。

在我的 error.log 中我有:

[error] 2105#0: *87 connect() failed (111: Connection refused) while connecting to upstream, client: 86.58.251.66, server: premium.bookboon.com, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "premium.bookboon.com"

而且我不确定为什么会发生此错误。这是我的 nginx.conf:

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

events {
    worker_connections 1024;
    # 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;

    ##
    # 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;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

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


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

我已经看到通过修复一些 php-fpm 选项解决了很多这些问题,但是我正在使用的服务器似乎没有使用它,快速的 find / -name "php-fpm" 没有返回任何东西,所以我在这里有点失落。

/启用站点/默认值:

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

       server_name    premium.bookboon.com;
       return         301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;

    root /var/www/premium/web;
    server_name premium.bookboon.com;

    ssl_certificate /etc/ssl/certs/premium.bookboon.com_bundle.crt;
    ssl_certificate_key /etc/ssl/private/premium.bookboon.com.key;

    location / {
        # try to serve file directly, fallback to rewrite
        try_files $uri @rewriteapp;

    } 
    location @rewriteapp {
        # rewrite all to app.php
        rewrite ^(.*)$ /app.php/$1 last;
    }

    location ~ ^/(app|config|test)\.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;
    fastcgi_param ENVIRONMENT "prod";
    }

    error_log /var/log/nginx/premium_error.log;
    access_log /var/log/nginx/premium_access.log;

}

任何帮助将不胜感激。

【问题讨论】:

  • 这个问题似乎是off topic
  • 指定FastCGI的Nginx配置文件不是nginx.conf。尝试发布您的 /etc/nginx/sites-enabled/default 文件
  • 给你,希望对你有帮助,干杯!
  • 根据fastcgi_pass选项,nginx正在将请求转发到你主机的9000端口,但是那里没有程序监听。您在部署中没有错过任何一步吗?
  • 例如,php-fpm(debian 上的php5-fpm)或替代安装并启动在您的服务器上? nginx 只能充当前端/代理或静态文件服务器。它不能自己处理 PHP,这就是为什么你需要一些其他的服务。

标签: linux nginx server


【解决方案1】:

Nginx 本身并不包含任何执行 PHP 代码的组件。它需要 PHP-FPM。

您的 nginx 已配置为使用 PHP-FPM,但是(如您所说)未安装 PHP-FPM。

如果你想运行 PHP 脚本,你需要安装和配置 PHP-FPM。在基于 Debian 的系统上使用 apt-get install php5-fpm 完成安装。您通常还必须安装几个php5-* 库包。 (取决于您要运行的应用程序,因此请阅读其文档或尝试执行它并查找错误消息。)

配置PHP-FPM并不复杂,默认就可以了。请注意,您的 nginx 配置为通过网络套接字与 PHP-FPM 通信!确保在文件/etc/php5/fpm/pool.d/www.conf 中,listen 参数配置了正确的 IP 地址和端口组合,例如listen = 127.0.0.1:9000.

您通常还需要在 php.ini 中进行一些编辑,但这非常适合您的用例,超出了本主题。

【讨论】:

    猜你喜欢
    • 2011-05-14
    • 2019-06-05
    • 2021-11-19
    • 2012-09-25
    • 2014-12-07
    • 2020-09-29
    • 2012-07-16
    • 2015-04-30
    • 2020-10-10
    相关资源
    最近更新 更多