我們訪問網站時,偶爾可能會遇到這樣的錯誤:502 Bad Gateway nginx/xxx
刷新一次或多次就好了。這是什麼原因呢?
最近使用nginx的代理、負載均衡功能發現了這問題的根本原因。
什麼原因?就是後端的服務掛了!
示例配置一:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream tomcatserver1 {
#3個tomcat服務
server 192.168.5.254:8001;
server 192.168.5.254:8002;
server 192.168.5.254:8003;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://192.168.5.254:8001; # 設置代理
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
這種情況如果後端的服務掛了,那你怎麼刷都 是沒有用的,除非後端的服務恢復了
示例配置二:
#############每個指令必須有分號結束############
#配置用戶或者組,默認爲nobody nobody。
#user nobody;
#允許生成的進程數,默認爲1,官方建議設置爲CPU內核數
worker_processes 1;
#指定日誌路徑,級別。這個設置可以放入全局塊,http塊,server塊,級別以此爲:debug|info|notice|warn|error|crit|alert|emerg
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#指定Nginx運行文件存放地址
pid logs/nginx.pid;
events {
#最大連接數,默認爲512
worker_connections 1024;
}
http {
#文件擴展名與文件類型映射表
include mime.types;
#默認文件類型,默認爲text/plain
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"';
#combined爲日誌格式的默認值
#access_log logs/access.log main;
#允許sendfile方式傳輸文件,默認爲off,可以在http塊,server塊,location塊。
sendfile on;
#tcp_nopush on;
#連接超時時間,默認爲75s,可以在http,server,location塊。
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream tomcatserver1 {
#3個tomcat服務
server 192.168.5.254:8001 weight=3;
server 192.168.5.254:8002;
server 192.168.5.254:8003;
}
server {
#監聽端口
listen 8883;
#監聽地址
server_name localhost;
#配置上傳文件的參數
#client_max_body_size 1024M;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcatserver1; # 設置代理
index index.html index.htm;
}
#location / {
# root html;
# index index.html index.htm;
#}
#error_page 404 /404.html;
error_page 404 http://www.buchang.com;
#請求的url過濾,正則匹配,~爲區分大小寫,~*爲不區分大小寫。
#location ~*^.+$ {
# #root path; #根目錄
# #index vv.txt; #設置默認頁
# proxy_pass http://mysvr; #請求轉向mysvr 定義的服務器列表
# deny 127.0.0.1; #拒絕的ip
# allow 172.18.5.54; #允許的ip
#}
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
location ^~ /weixin_40205234 {
proxy_pass https://blog.csdn.net/weixin_40205234;
}
location ^~ /index {
proxy_pass http://www.buchang.com:3454/index;
}
location ^~ /DSWJ {
rewrite ^/DSWJ/(.*)$ /DSWJ/$1 break;
proxy_pass http://192.168.6.5/DSWJ;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
這種情況就是使用了負載均衡,後端有N+1臺服務器,你每次訪問時雖然頁面是一樣的,但訪問的後端服務器可能不是一個,這種情況通過刷新是可以解決問題的。
當然,這兩種情況都只是Nginx的簡單配置應用,沒有判斷後端是否可用,如果添加的判斷後端是否可用的配置,是根本看不到這個錯誤的。