当我得到错误50x时,我需要将我的页面redirect到维护页面。 它确实redirect,除非它不会加载CSS,JS或图像。 错误日志是干净的,访问日志不显示任何问题。
下面的代码被添加到我的nginx.conf.default文件中,但是我没有看到任何更改:
location ~ \.css { add_header Content-Type text/css; } location ~ \.js { add_header Content-Type application/x-javascript; }
我的html文件在/home/user/Documents ,我的css / js / png文件位于/home/user/Documents/maintenance_files文件夹中。 如果我在浏览器中打开html文件,它会正确地获取这些文件。
我的MIMEtypes在/opt/nginx/mime.types 。 这是我的nginx.conf文件:
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; error_log logs/error.log debug; events { worker_connections 1024; } http { proxy_temp_path /home/user/work/nginx_files/proxy_tmpFiles 1 2; proxy_redirect off; # Tells the server we aren't redirecting content. We actually do that later with proxy_pass. #include /etc/nginx/mime.types; default_type application/octet-stream; ## # Basic Settings ## sendfile on; keepalive_timeout 65; ## # Logging Settings ## #access_log /home/user/nginx_files/logs/access.log; ## # Gzip Settings ## gzip on; ## # Virtual Host Configs ## #include /etc/nginx/conf.d/*.conf; #include /etc/nginx/sites-enabled/*; include /opt/nginx/mime.types; server { listen 80; server_name localhost; #location ~ \.css { # add_header Content-Type text/css; #} #location ~ \.js { # add_header Content-Type application/x-javascript; #} error_page 500 502 503 504 /maintenance.html; location = /maintenance.html { #include /opt/nginx/mime.types; root /home/user/Documents; #index maintenance.html; #internal; -> no changes } location / { #include /opt/nginx/mime.types; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_pass http://somelink; #root /home/user/Documents; #index maintenance.html; } } }
我的问题是在redirect期间,当我得到错误50倍,在这个代码。 HTML页面被加载,但nginx将不会加载图像或CSS。
error_page 500 502 503 504 /maintenance.html; location = /maintenance.html { #include /opt/nginx/mime.types; root /home/user/Documents; #index maintenance.html; #internal; -> no changes }
感谢您的帮助。
你的问题是,你正在从后端应用程序服务器提供静态内容。 所以当后端应用服务器因为忙而无法响应时,Nginx会准备一个502响应。 您的configuration指导Nginx在这种情况下提供特定的HTML,然后从Nginx加载更多的静态内容。 然后这些请求被代理到应用服务器,502也被返回给它们。
无论如何,直接从Nginx提供静态内容是个好主意,以简化后端服务器专注于为应用提供服务的工作。
像这样构build你的Nginxconfiguration让Nginx提供静态文件:
location / { try_files $uri $uri/ @backend; } location @backend { proxy_pass http://example.com; }