Nginx 502何时提供错误页面内容? [英] Nginx 502 when serving error page content?

查看:92
本文介绍了Nginx 502何时提供错误页面内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在将Nginx设置为服务器上应用程序的反向代理.其中的一部分包括一个具有外部内容(如图像)的维护页面.我能够找到一种方法来设置一个错误页面,其中图像返回200,但是看起来反向代理会改变整个环境.这是来自具有内容问题的nginx维护页面的原始解决方案

I've been setting up Nginx as a reverse proxy for an app on the server. Part of this includes a maintenance page that has external content (like images). I was able to find a method for setting up an error page with images returning 200, but it looks like a reverse proxy will change the whole environment. Here's the original solution from nginx maintenance page with content issue

error_page 503 @maintenance;

location @maintenance {
    root /path_to_static_root;
    if (!-f $request_filename) {
        rewrite ^(.*)$ /rest_of_path/maintenance.html break;
    }
    return 200;
}

反向代理配置为:

location / {
            proxy_pass         http://127.0.0.1:9007/;
            proxy_redirect     off;
}

问题是,当发现维护"根目录中存在文件时,出现问题,服务器返回502.有人知道是什么原因吗?

The Problem is that when a file is found to exist in the "maintenance" root, something goes wrong and the server returns a 502. Anyone know what the cause could be?

一些猜测我想知道服务器是否在端口80上侦听,它将以某种方式将任何好的文件请求传递回代理.如果那是真的,那将如何避免呢?

Some speculation I'm wondering if server listens on port 80, it somehow passes any good file request back into the proxy. If that were true, how would that be avoided?

这是nginx日志中的错误.我直接尝试访问50x.html.不确定为什么会发生这种情况?

Here's the error in the nginx log. I am directly trying to access 50x.html. Not sure why this would occur?

2012/02/17 19:39:15 [error] 21394#0: *13 connect() failed (111: Connection refused) while connecting to upstream, client: (my ip address), server: _, request: "GET /50x.html HTTP/1.1", upstream: "http://127.0.0.1:9007/50x.html", host: "domain.com"

似乎确实是试图从应用而非根目录中获取.我该如何绕过呢?

It looks like it is indeed trying to GET from the app and not the root. How can I bypass this?

我本来以为我找到了一个答案,其中对nginx v1.0.12进行了更改,但是并不能解决问题.它也遇到类似的情况,但我猜想此修复程序太具体了.

I originally thought I had found an answer where a change was made for nginx v1.0.12 but it did not solve the problem. It involved a similar situation but my guess is the fix was too specific.

推荐答案

由于维护页面应该是Nginx可以提供的静态html文件,因此您不需要涉及后端(即,IE应该不使用代理传递).直接.

You shouldn't need to involve the backend (I.E., shouldn't use proxy pass) since your maintenance page should be a static html file that Nginx can serve directly.

假设您已将设置配置为...

Assuming you have a setup configured as ...

server {
    listen 80;
    server_name example.com;
    root /path/to/webroot;


    # Regular locations etc
    ...
}

创建一个名为"503_status"的文件夹,并将维护页面作为"503.html"放在其中.

Create a folder called "503_status" and put your maintenance page in there as "503.html".

在适当的位置上,在Nginx目录下创建一个名为"maintenance.default"的文件,其内容如下:

With that in place, create a file called "maintenance.default" under the Nginx directory with the following content ...

error_page 503 /503_status/503.html;

# Candidate for redirection if not ending with one of these extensions.
if ( $request_uri !~ \.(jpg|gif|png|css|js)$ ) {
     set $maint  "Tr";
}
# Candidate for redirection if not a request for the maintenance page 
if ( $request_uri !~ ^/maintenance/$ ) {
    set $maint  "${maint}ue";
}
# Redirect to a location where the status code will be issued 
if ( $maint = True ) {
    rewrite ^ /maintenance/ redirect;
}
# Due to Nginx quirk, issue the status code in a location context.
# Make "internal" to prevent direct browsing.
location /maintenance {
    internal;
    return 503;
}
# 503_status folder as "internal" so no direct browsing 
location 503_status {
    internal;
    alias /server/path/to/503_status/folder;
}

每当您要对站点进行维护时,只需包含以下文件即可...

Whenever you put to put the site into maintenance, just include the file as follows ...

server {
    listen 80;
    server_name example.com;
    root /path/to/webroot;

    include /server/path/to/maintenance.default;        

    # Regular locations etc
    ...
}

这将为您的维护页面及其所需的任何资源提供服务(只需确保扩展名在列表中).后端服务器根本不起作用.

This will serve your maintenance page as well as any resources it needs (just make sure extension is in the list). The backend server does not come into play at all.

这篇关于Nginx 502何时提供错误页面内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆