【问题标题】:nginx + uwsgi custom 502 will not worknginx + uwsgi 自定义 502 不起作用
【发布时间】:2012-04-01 14:34:49
【问题描述】:

您好,我正在尝试让自定义 502 页面在网站上运行,但似乎无法正常运行。

基本上我测试它的方式是我只是停止 uwsgi 并访问页面,每次我得到默认的 nginx 502 页面。有人可以向我解释如何让它工作吗?我已经在这里工作了一个多星期,但成功率为 0。我在 public_html 中有一个名为 502.html 的文件,我可以使用http://ask.ploy.io/502.html 直接访问它,但是一旦我停止 uwsgi 并尝试访问主域http://ask.ploy.io,我就会得到默认的 502 页面。这是虚拟主机配置:

### nginx vhost conf for ployio
server {
listen 80;
server_name ask.ploy.io www.ask.ploy.io;

access_log /usr/local/apache/domlogs/ask.ploy.io main;
error_log /home/ployio/access-logs/ask.ploy.io debug;

root /home/ployio/public_html;
index index.html index.htm index.php;

location /502.html {
root   /home/ployio/public_html;
}

location ~ /\.ht {
    deny all;
}

location / {
    error_page 404 403 = @uwsgi;
    log_not_found  off;
    error_page   502  /502.html;
    root   /home/ployio/public_html;

}

location @uwsgi {
    internal;
    uwsgi_pass unix:/home/ployio/.uwsgi/uwsgi.sock;
    include /usr/local/nginx/conf/uwsgi_params;
}

location ~* ^.*\.php$ {
    if (!-f $request_filename) {
        return 404;
    }
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass  http://204.61.223.114:8888;
}

location /cpanel {
    rewrite ^/(.*) https://cpanel.ask.ploy.io:2083/$1 permanent;
}

}

【问题讨论】:

    标签: django nginx


    【解决方案1】:

    如果 502 它是您要使用自定义错误页面处理的唯一错误代码,您只需要在位置中指定:

    location /502.html {
        root   /home/ployio/public_html;
    }
    

    您当前的位置仅与确切的“/50x.html”路径匹配,而您的服务器中确实不存在该路径:http://ask.ploy.io/50x.html

    也可以使用 nginx 变量($uri 或类似的东西)将所有 50x 错误重定向到该根目录,但对于您的需要,这应该足够了。

    【讨论】:

    • 好吧,所以我把它改成了这样...root /home/ployio/public_html; index index.html index.htm index.php; error_page 500 502 503 504 /502.html; location /502.html { root /home/ployio/public_html; }
    • 尝试在 error_page 指令中更加具体。使用error_page 502 /502.hml。如果您将 502.html 文件重命名为 50x.html,您的原始 nginx 配置也可能会起作用。
    • 好的,所以编辑了我的主要评论以显示我所做的更改。我已经尝试了各种变体,但我仍然无法让它工作。到目前为止,感谢您在这件事上的帮助。我真的不明白为什么这不起作用它应该是直截了当的。
    • 在尝试打开ask.ploy.io时尝试粘贴您的nginx的错误日志,也许我们可以在那里找到有用的东西。
    • 好的,所以我重新编辑了我的初始帖子,以完全匹配我对我的虚拟主机所做的更改。所以我注意到的一件奇怪的事情是我的访问日志文件和错误日志文件是相同的,但在虚拟主机中它们被明确指定。所以在我的error_log文件中我看到... 2012/04/01 13:46:43 [error] 20089#0: *4132 connect() to unix:/home/ployio/.uwsgi/uwsgi.sock failed (111:连接被拒绝)同时连接到上游,客户端:24.200.194.125,服务器:ask.ploy.io,请求:“GET / HTTP/1.0”,上游:“uwsgi://unix:/home/ployio/.uwsgi/uwsgi .sock:", host: "ask.ploy.io" 这对我来说很奇怪,看起来更像是一个访问权限
    【解决方案2】:

    主要问题在location @uwsgi 部分。它似乎永远无法正确处理 502 返回。也许是设计使然?

    这是一个有效的配置

    server {
       listen 80;
        server_name ask.ploy.io www.ask.ploy.io;
    
        access_log /usr/local/apache/domlogs/ask.ploy.io main;
        error_log /home/ployio/access-logs/ask.ploy.io debug;
    
        root /home/ployio/public_html;
        index index.html index.htm index.php;
    
        location / {
            uwsgi_pass unix:/home/ployio/.uwsgi/uwsgi.sock;
            include /usr/local/nginx/conf/uwsgi_params;
        }
    
        error_page 502 503 504 @maintenance;
        location @maintenance {
            root /home/ployio/public_html_502;
            rewrite ^(.*)$ /502.html break;
        }
    
    }
    

    确保将 502.html 放在新的根目录中并在那里引用它.. ps.. randall 很烂

    【讨论】:

    • “randall 很烂”是完全没有必要的
    猜你喜欢
    • 2013-09-19
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 2016-02-16
    • 2012-07-10
    • 2018-11-27
    相关资源
    最近更新 更多