【问题标题】:Nginx reverse proxy for Apache - error 500 sometimesApache 的 Nginx 反向代理 - 有时会出现错误 500
【发布时间】:2022-01-07 22:08:44
【问题描述】:

我是 nginx 的新手,我对服务器配置很感兴趣。 我将 Nginx 配置为 Apache 的反向代理,看起来一切正常,但 有时 出现错误 500。

这里奇怪的是,看到错误 500 消息后,我刷新页面并且它工作了!错误 500 出现的非常快,只有半秒,所以我认为不是超时问题。

当我在服务器上测试浏览网站和页面时,随机出现此问题。

到处搜索,尝试更改配置,但没有找到,希望大家帮忙。

以下是配置: 主要的 nginx 配置 - nginx.conf:

####################
# nginx.conf
####################

user              nginx;
worker_processes  2;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

worker_rlimit_nofile 30000;

events {
    worker_connections  8192;
    use epoll;
}

http {
    server_tokens off;
    include       /etc/nginx/mime.types;

    # proxy settings
    proxy_redirect          off;
    proxy_set_header        Host            $host;
    proxy_set_header        X-Real-IP       $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    client_max_body_size    20m;
    client_body_buffer_size 128k;
    proxy_connect_timeout   90;
    proxy_send_timeout      90;
    proxy_read_timeout      90;
    proxy_buffering     off;

    index    index.html index.htm index.php;
    default_type  application/octet-stream;

    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;
    server_names_hash_bucket_size 128;
    tcp_nodelay     off;

    #keepalive_timeout  0;
    keepalive_timeout  5;

    ## Gzip Compression
    gzip on;
    gzip_http_version 1.0;
    gzip_comp_level 5;
    gzip_proxied any;
    gzip_min_length  256;
    #gzip_buffers 32 4k;
    gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml application/xml+rss application/javascript application/json;
    # Some version of IE 6 don't handle compression well on some mime-types, 
    # so just disable for them
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    # Set a vary header so downstream proxies don't send cached gzipped 
    # content to IE6
    gzip_vary on;

    # Load config files from the /etc/nginx/conf.d directory
    # The default server is in conf.d/default.conf
    include /etc/nginx/conf.d/*.conf;
}

默认服务器配置:

#
# The default server conf.d/default.conf
#
server {
    listen       80;
    server_name  _;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
        proxy_pass   http://127.0.0.1:8989;
    }
}

有上述问题的服务器 - /conf.d/myvhost.conf:

server {
    listen       IP:80; ssl off;
    server_name  abc.xyz.com;

    access_log  /home/myvhost/logs/nginx_access.log;
    error_log  /home/myvhost/logs/nginx_error.log;

    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $host;
      proxy_buffering off;

      proxy_pass   http://IP:8989;
    }

    location ~* ^.+\.(htm|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|bz2|pdf|odt|txt|tar|bmp|rtf|js|swf|avi|mp4|mp3|ogg|flv)$ {
      expires 14d;
      add_header Pragma public;
      add_header Cache-Control "public, must-revalidate, proxy-revalidate";
      root /home/myvhost/public_html;
    }

    location ~ /\.ht {
        deny  all;
    }

}

apache配置:

# httpd.conf
# general
...

# virtual host
<VirtualHost IP:8989>
SuexecUserGroup "#503" "#503"
ServerName abc.xyz.com
DocumentRoot /home/myvhost/public_html
ErrorLog /var/log/.../abc.xyz.com_error_log
CustomLog /var/log/.../abc.xyz.com_access_log combined
ScriptAlias /cgi-bin/ /home/myvhost/cgi-bin/
ScriptAlias /awstats/ /home/myvhost/cgi-bin/
DirectoryIndex index.html index.htm index.php index.php4 index.php5
<Directory /home/myvhost/public_html>
Options -Indexes +IncludesNOEXEC +SymLinksIfOwnerMatch +ExecCGI
allow from all
AllowOverride All Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch
AddHandler fcgid-script .php
AddHandler fcgid-script .php5
FCGIWrapper /home/myvhost/fcgi-bin/php5.fcgi .php
FCGIWrapper /home/myvhost/fcgi-bin/php5.fcgi .php5
</Directory>
<Directory /home/myvhost/cgi-bin>
allow from all
AllowOverride All Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch
</Directory>
RewriteEngine on
RewriteCond %{HTTP_HOST} =webmail.abc.xyz.com
RewriteRule ^(.*) https://abc.xyz.com:20000/ [R]
RewriteCond %{HTTP_HOST} =admin.abc.xyz.com
RemoveHandler .php
RemoveHandler .php5
<Files awstats.pl>
AuthName "abc.xyz.com statistics"
AuthType Basic
AuthUserFile /home/myvhost/.awstats-htpasswd
require valid-user
</Files>
IPCCommTimeout 121
</VirtualHost>

在我粘贴在这里的所有配置中,IP 是我的虚拟主机的 ip。

感谢大家的帮助!

【问题讨论】:

  • 500错误是nginx引起的,还是apache引起的,通过nginx传递的?
  • 最后一天,当我仅通过浏览 site:8989 测试 apache 端时,有时仍会出现该错误。所以我认为它是由 Apache 引起的。
  • 我刚刚也发现,这个问题似乎出现在我在 fcgid 模式下运行时。当我更改为 cgi 模式时,该问题似乎消失了。 但是当以 cgi 模式运行时,CPU 负载会增加,这会使服务器很快拒绝请求 :-(。所以我必须改回 fcgid 模式,但这个问题仍然没有解决 - 有时是内部服务器错误 500。
  • 同样的事情发生在我身上,我不知道为什么。 :(

标签: apache nginx


【解决方案1】:

可能是以下两种情况之一:

  1. 两个 www 处理程序 (nginx/apache2) 使用相同的端口
  2. 页面未准备好 - 主要是在最近重新启动/重新加载配置之后

在第二种情况下,您会在日志中发现一些错误消息:

/var/log/nginx/error.log`

【讨论】:

    【解决方案2】:

    相信您会在 /var/log/.../abc.xyz.com_error_log 文件中找到您的问题

    【讨论】:

    • 谢谢!我尝试检查所有日志文件,但仍然没有找到。 :-s
    猜你喜欢
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 2021-07-06
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 2015-01-13
    相关资源
    最近更新 更多