问题描述
我在我的网站的服务器上使用 Nginx 作为前端代理.我想用它来将用户重定向到我的 Web 应用程序,或者当我处于维护模式时,它是实时的或维护 php 页面.
I'm using Nginx as front proxy on my server for my website.I want to use it to redirect users to my web application when it's live or a maintenance php page when I'm in maintenance mode.
这是我的服务器指令:
server {
listen 443;
return 503;
error_page 503 @maintenance;
root /usr/maintenance;
location @maintenance {
fastcgi_pass php-fpm;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location / {
proxy_pass https://webapp;
}
}
如果我取消注释 return 503
,则会向客户端发送 503 响应,否则,将发送 Web 应用程序.
If I uncomment return 503
, a 503 response is sent to the client, otherwise, the web app is sent.
我的 PHP 503 错误页面按预期显示,但问题是它有静态资产(css、图像、js),当 Chrome 尝试加载它们时,我得到 503 返回代码.资产位于根目录中.
My PHP 503 error page is displayed as expected but the problem is that it has static assets (css, images, js) and I get 503 return code when Chrome tries to load them. The assets are in the root directory.
我该如何解决这个问题?有没有比注释/取消注释 return 503
更好的方法来处理维护和实时模式?
How can I fix this ?Is there a better way than comment/uncomment return 503
to handle maintenance and live modes ?
谢谢
推荐答案
经过一番研究,我找到了一种优雅的方法.来源:http://blog.mythictechnologies.com/2011/02/10/setting-a-maintenance-page-with-nginx/
After some research, I've found an elegant way to do it.Source : http://blog.mythictechnologies.com/2011/02/10/setting-a-maintenance-page-with-nginx/
这是我的新配置
server {
listen 443;
error_page 503 @maintenance;
root /usr/maintenance;
location @maintenance {
fastcgi_pass php-fpm;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* \.(css|png|js|jpg|jpeg) {
# The file will be returned
}
location / {
return 503;
proxy_pass https://webapp;
}
}
您可以随意调整正则表达式 \.(css|png|js|jpg|jpeg)
,但将文件列入白名单似乎是个好主意.
You're free to adjust the regex \.(css|png|js|jpg|jpeg)
as you want but whitelisting files seems a good idea.
这篇关于如何在维护模式下使用 Nginx 提供静态资产 (503)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!