[{"data":1,"prerenderedAt":1344},["ShallowReactive",2],{"\u002Ffix-issues\u002Ffix-flask-502-bad-gateway-step-by-step-guide":3},{"id":4,"title":5,"body":6,"description":1334,"extension":1335,"meta":1336,"navigation":1177,"path":1340,"seo":1341,"stem":1342,"__hash__":1343},"content\u002Ffix-issues\u002Ffix-flask-502-bad-gateway-step-by-step-guide.md","Fix Flask 502 Bad Gateway (Step-by-Step Guide)",{"type":7,"value":8,"toc":1323},"minimark",[9,13,17,22,25,150,153,157,168,172,829,833,910,914,917,934,937,955,958,978,981,995,998,1010,1013,1034,1037,1065,1068,1091,1094,1116,1119,1138,1141,1162,1166,1228,1232,1259,1263,1271,1279,1290,1304,1312,1316,1319],[10,11,5],"h1",{"id":12},"fix-flask-502-bad-gateway-step-by-step-guide",[14,15,16],"p",{},"If you're seeing a 502 Bad Gateway error on a Flask app, the problem is usually between Nginx and Gunicorn, not Flask routing itself. This guide shows you how to identify the failing layer, restore the upstream connection, and verify the app is serving traffic again.",[18,19,21],"h2",{"id":20},"quick-fix-quick-setup","Quick Fix \u002F Quick Setup",[14,23,24],{},"Run this first:",[26,27,32],"pre",{"className":28,"code":29,"language":30,"meta":31,"style":31},"language-bash shiki shiki-themes github-light github-dark","sudo systemctl status gunicorn\nsudo journalctl -u gunicorn -n 100 --no-pager\nsudo nginx -t\nsudo systemctl restart gunicorn\nsudo systemctl reload nginx\ncurl --unix-socket \u002Frun\u002Fgunicorn.sock http:\u002F\u002Flocalhost\u002F || curl http:\u002F\u002F127.0.0.1:8000\u002F\ncurl -I http:\u002F\u002F127.0.0.1\n","bash","",[33,34,35,54,78,89,101,114,139],"code",{"__ignoreMap":31},[36,37,40,44,48,51],"span",{"class":38,"line":39},"line",1,[36,41,43],{"class":42},"sScJk","sudo",[36,45,47],{"class":46},"sZZnC"," systemctl",[36,49,50],{"class":46}," status",[36,52,53],{"class":46}," gunicorn\n",[36,55,57,59,62,66,69,72,75],{"class":38,"line":56},2,[36,58,43],{"class":42},[36,60,61],{"class":46}," journalctl",[36,63,65],{"class":64},"sj4cs"," -u",[36,67,68],{"class":46}," gunicorn",[36,70,71],{"class":64}," -n",[36,73,74],{"class":64}," 100",[36,76,77],{"class":64}," --no-pager\n",[36,79,81,83,86],{"class":38,"line":80},3,[36,82,43],{"class":42},[36,84,85],{"class":46}," nginx",[36,87,88],{"class":64}," -t\n",[36,90,92,94,96,99],{"class":38,"line":91},4,[36,93,43],{"class":42},[36,95,47],{"class":46},[36,97,98],{"class":46}," restart",[36,100,53],{"class":46},[36,102,104,106,108,111],{"class":38,"line":103},5,[36,105,43],{"class":42},[36,107,47],{"class":46},[36,109,110],{"class":46}," reload",[36,112,113],{"class":46}," nginx\n",[36,115,117,120,123,126,129,133,136],{"class":38,"line":116},6,[36,118,119],{"class":42},"curl",[36,121,122],{"class":64}," --unix-socket",[36,124,125],{"class":46}," \u002Frun\u002Fgunicorn.sock",[36,127,128],{"class":46}," http:\u002F\u002Flocalhost\u002F",[36,130,132],{"class":131},"szBVR"," ||",[36,134,135],{"class":42}," curl",[36,137,138],{"class":46}," http:\u002F\u002F127.0.0.1:8000\u002F\n",[36,140,142,144,147],{"class":38,"line":141},7,[36,143,119],{"class":42},[36,145,146],{"class":64}," -I",[36,148,149],{"class":46}," http:\u002F\u002F127.0.0.1\n",[14,151,152],{},"Use this sequence when Nginx is up but Flask returns 502. Confirm Gunicorn is running, verify the upstream socket or port matches the Nginx config, then restart both services and test the upstream directly.",[18,154,156],{"id":155},"whats-happening","What’s Happening",[14,158,159,160,163,164,167],{},"A 502 from Nginx means Nginx could not get a valid response from the Flask upstream. The upstream is usually Gunicorn over a Unix socket like ",[33,161,162],{},"\u002Frun\u002Fgunicorn.sock"," or a TCP port like ",[33,165,166],{},"127.0.0.1:8000",". Typical failures are Gunicorn not running, wrong socket or port in Nginx, permission issues on the socket, or the app crashing during startup.",[18,169,171],{"id":170},"step-by-step-guide","Step-by-Step Guide",[173,174,175,211,255,302,325,370,433,520,546,578,656,699,744,766,791],"ol",{},[176,177,178,182,185,186,200,202,203],"li",{},[179,180,181],"strong",{},"Confirm the failure path",[183,184],"br",{},"Run:",[26,187,189],{"className":28,"code":188,"language":30,"meta":31,"style":31},"curl -I http:\u002F\u002Fyour-domain\n",[33,190,191],{"__ignoreMap":31},[36,192,193,195,197],{"class":38,"line":39},[36,194,119],{"class":42},[36,196,146],{"class":64},[36,198,199],{"class":46}," http:\u002F\u002Fyour-domain\n",[183,201],{},"Verify the response is:",[26,204,209],{"className":205,"code":207,"language":208,"meta":31},[206],"language-text","HTTP\u002F1.1 502 Bad Gateway\n","text",[33,210,207],{"__ignoreMap":31},[176,212,213,216,185,218,233,235,236],{},[179,214,215],{},"Check Gunicorn service state",[183,217],{},[26,219,221],{"className":28,"code":220,"language":30,"meta":31,"style":31},"sudo systemctl status gunicorn\n",[33,222,223],{"__ignoreMap":31},[36,224,225,227,229,231],{"class":38,"line":39},[36,226,43],{"class":42},[36,228,47],{"class":46},[36,230,50],{"class":46},[36,232,53],{"class":46},[183,234],{},"Look for:",[237,238,239,245],"ul",{},[176,240,241,244],{},[33,242,243],{},"active (running)"," → Gunicorn is up",[176,246,247,250,251,254],{},[33,248,249],{},"failed"," or ",[33,252,253],{},"inactive"," → upstream is down",[176,256,257,260,185,262,283,235,285],{},[179,258,259],{},"Read Gunicorn logs",[183,261],{},[26,263,265],{"className":28,"code":264,"language":30,"meta":31,"style":31},"sudo journalctl -u gunicorn -n 100 --no-pager\n",[33,266,267],{"__ignoreMap":31},[36,268,269,271,273,275,277,279,281],{"class":38,"line":39},[36,270,43],{"class":42},[36,272,61],{"class":46},[36,274,65],{"class":64},[36,276,68],{"class":46},[36,278,71],{"class":64},[36,280,74],{"class":64},[36,282,77],{"class":64},[183,284],{},[237,286,287,290,293,296,299],{},[176,288,289],{},"Python tracebacks",[176,291,292],{},"import errors",[176,294,295],{},"missing environment variables",[176,297,298],{},"bind failures",[176,300,301],{},"bad module paths",[176,303,304,307,185,309,322,324],{},[179,305,306],{},"Validate Nginx config syntax",[183,308],{},[26,310,312],{"className":28,"code":311,"language":30,"meta":31,"style":31},"sudo nginx -t\n",[33,313,314],{"__ignoreMap":31},[36,315,316,318,320],{"class":38,"line":39},[36,317,43],{"class":42},[36,319,85],{"class":46},[36,321,88],{"class":64},[183,323],{},"Fix syntax errors before continuing.",[176,326,327,330,185,332,351,235,353],{},[179,328,329],{},"Check Nginx error logs",[183,331],{},[26,333,335],{"className":28,"code":334,"language":30,"meta":31,"style":31},"sudo tail -n 100 \u002Fvar\u002Flog\u002Fnginx\u002Ferror.log\n",[33,336,337],{"__ignoreMap":31},[36,338,339,341,344,346,348],{"class":38,"line":39},[36,340,43],{"class":42},[36,342,343],{"class":46}," tail",[36,345,71],{"class":64},[36,347,74],{"class":64},[36,349,350],{"class":46}," \u002Fvar\u002Flog\u002Fnginx\u002Ferror.log\n",[183,352],{},[237,354,355,360,365],{},[176,356,357],{},[33,358,359],{},"connect() failed (111: Connection refused)",[176,361,362],{},[33,363,364],{},"No such file or directory",[176,366,367],{},[33,368,369],{},"Permission denied",[176,371,372,375,377,378,405,407,408,430,432],{},[179,373,374],{},"Verify the configured upstream in Nginx",[183,376],{},"Check your Nginx site config:",[26,379,383],{"className":380,"code":381,"language":382,"meta":31,"style":31},"language-nginx shiki shiki-themes github-light github-dark","location \u002F {\n    include proxy_params;\n    proxy_pass http:\u002F\u002Funix:\u002Frun\u002Fgunicorn.sock;\n}\n","nginx",[33,384,385,390,395,400],{"__ignoreMap":31},[36,386,387],{"class":38,"line":39},[36,388,389],{},"location \u002F {\n",[36,391,392],{"class":38,"line":56},[36,393,394],{},"    include proxy_params;\n",[36,396,397],{"class":38,"line":80},[36,398,399],{},"    proxy_pass http:\u002F\u002Funix:\u002Frun\u002Fgunicorn.sock;\n",[36,401,402],{"class":38,"line":91},[36,403,404],{},"}\n",[183,406],{},"or:",[26,409,411],{"className":380,"code":410,"language":382,"meta":31,"style":31},"location \u002F {\n    include proxy_params;\n    proxy_pass http:\u002F\u002F127.0.0.1:8000;\n}\n",[33,412,413,417,421,426],{"__ignoreMap":31},[36,414,415],{"class":38,"line":39},[36,416,389],{},[36,418,419],{"class":38,"line":56},[36,420,394],{},[36,422,423],{"class":38,"line":80},[36,424,425],{},"    proxy_pass http:\u002F\u002F127.0.0.1:8000;\n",[36,427,428],{"class":38,"line":91},[36,429,404],{},[183,431],{},"The upstream target must match Gunicorn exactly.",[176,434,435,438,185,440,456,458,459,491,493,494],{},[179,436,437],{},"Inspect Gunicorn bind settings in systemd",[183,439],{},[26,441,443],{"className":28,"code":442,"language":30,"meta":31,"style":31},"sudo systemctl cat gunicorn\n",[33,444,445],{"__ignoreMap":31},[36,446,447,449,451,454],{"class":38,"line":39},[36,448,43],{"class":42},[36,450,47],{"class":46},[36,452,453],{"class":46}," cat",[36,455,53],{"class":46},[183,457],{},"Example Unix socket bind:",[26,460,464],{"className":461,"code":462,"language":463,"meta":31,"style":31},"language-ini shiki shiki-themes github-light github-dark","[Service]\nUser=www-data\nGroup=www-data\nWorkingDirectory=\u002Fsrv\u002Fmyapp\nExecStart=\u002Fsrv\u002Fmyapp\u002Fvenv\u002Fbin\u002Fgunicorn --workers 3 --bind unix:\u002Frun\u002Fgunicorn.sock wsgi:app\n","ini",[33,465,466,471,476,481,486],{"__ignoreMap":31},[36,467,468],{"class":38,"line":39},[36,469,470],{},"[Service]\n",[36,472,473],{"class":38,"line":56},[36,474,475],{},"User=www-data\n",[36,477,478],{"class":38,"line":80},[36,479,480],{},"Group=www-data\n",[36,482,483],{"class":38,"line":91},[36,484,485],{},"WorkingDirectory=\u002Fsrv\u002Fmyapp\n",[36,487,488],{"class":38,"line":103},[36,489,490],{},"ExecStart=\u002Fsrv\u002Fmyapp\u002Fvenv\u002Fbin\u002Fgunicorn --workers 3 --bind unix:\u002Frun\u002Fgunicorn.sock wsgi:app\n",[183,492],{},"Example TCP bind:",[26,495,497],{"className":461,"code":496,"language":463,"meta":31,"style":31},"[Service]\nUser=www-data\nGroup=www-data\nWorkingDirectory=\u002Fsrv\u002Fmyapp\nExecStart=\u002Fsrv\u002Fmyapp\u002Fvenv\u002Fbin\u002Fgunicorn --workers 3 --bind 127.0.0.1:8000 wsgi:app\n",[33,498,499,503,507,511,515],{"__ignoreMap":31},[36,500,501],{"class":38,"line":39},[36,502,470],{},[36,504,505],{"class":38,"line":56},[36,506,475],{},[36,508,509],{"class":38,"line":80},[36,510,480],{},[36,512,513],{"class":38,"line":91},[36,514,485],{},[36,516,517],{"class":38,"line":103},[36,518,519],{},"ExecStart=\u002Fsrv\u002Fmyapp\u002Fvenv\u002Fbin\u002Fgunicorn --workers 3 --bind 127.0.0.1:8000 wsgi:app\n",[176,521,522,525,185,527,543,545],{},[179,523,524],{},"If using a Unix socket, verify it exists",[183,526],{},[26,528,530],{"className":28,"code":529,"language":30,"meta":31,"style":31},"ls -l \u002Frun\u002Fgunicorn.sock\n",[33,531,532],{"__ignoreMap":31},[36,533,534,537,540],{"class":38,"line":39},[36,535,536],{"class":42},"ls",[36,538,539],{"class":64}," -l",[36,541,542],{"class":46}," \u002Frun\u002Fgunicorn.sock\n",[183,544],{},"If the socket is missing, Gunicorn likely did not start or is binding elsewhere.",[176,547,548,551,185,553,575,577],{},[179,549,550],{},"If using a TCP port, verify Gunicorn is listening",[183,552],{},[26,554,556],{"className":28,"code":555,"language":30,"meta":31,"style":31},"ss -ltnp | grep 8000\n",[33,557,558],{"__ignoreMap":31},[36,559,560,563,566,569,572],{"class":38,"line":39},[36,561,562],{"class":42},"ss",[36,564,565],{"class":64}," -ltnp",[36,567,568],{"class":131}," |",[36,570,571],{"class":42}," grep",[36,573,574],{"class":64}," 8000\n",[183,576],{},"Confirm the expected process is listening on the expected address and port.",[176,579,580,583,585,586,588,589,605,607,608,622,624,625,628,629,631,632],{},[179,581,582],{},"Fix ownership and permissions if using a socket",[183,584],{},"Ensure Nginx can access the socket path and file.",[183,587],{},"Example systemd override:",[26,590,592],{"className":28,"code":591,"language":30,"meta":31,"style":31},"sudo systemctl edit gunicorn\n",[33,593,594],{"__ignoreMap":31},[36,595,596,598,600,603],{"class":38,"line":39},[36,597,43],{"class":42},[36,599,47],{"class":46},[36,601,602],{"class":46}," edit",[36,604,53],{"class":46},[183,606],{},"Add:",[26,609,611],{"className":461,"code":610,"language":463,"meta":31,"style":31},"[Service]\nUMask=0007\n",[33,612,613,617],{"__ignoreMap":31},[36,614,615],{"class":38,"line":39},[36,616,470],{},[36,618,619],{"class":38,"line":56},[36,620,621],{},"UMask=0007\n",[183,623],{},"Then ensure Gunicorn runs with a group Nginx can access, such as ",[33,626,627],{},"www-data",".",[183,630],{},"After changes:",[26,633,635],{"className":28,"code":634,"language":30,"meta":31,"style":31},"sudo systemctl daemon-reload\nsudo systemctl restart gunicorn\n",[33,636,637,646],{"__ignoreMap":31},[36,638,639,641,643],{"class":38,"line":39},[36,640,43],{"class":42},[36,642,47],{"class":46},[36,644,645],{"class":46}," daemon-reload\n",[36,647,648,650,652,654],{"class":38,"line":56},[36,649,43],{"class":42},[36,651,47],{"class":46},[36,653,98],{"class":46},[36,655,53],{"class":46},[176,657,658,661,663,664,680,682,683,696,698],{},[179,659,660],{},"Test the upstream directly",[183,662],{},"For a Unix socket:",[26,665,667],{"className":28,"code":666,"language":30,"meta":31,"style":31},"curl --unix-socket \u002Frun\u002Fgunicorn.sock http:\u002F\u002Flocalhost\u002F\n",[33,668,669],{"__ignoreMap":31},[36,670,671,673,675,677],{"class":38,"line":39},[36,672,119],{"class":42},[36,674,122],{"class":64},[36,676,125],{"class":46},[36,678,679],{"class":46}," http:\u002F\u002Flocalhost\u002F\n",[183,681],{},"For TCP:",[26,684,686],{"className":28,"code":685,"language":30,"meta":31,"style":31},"curl -I http:\u002F\u002F127.0.0.1:8000\u002F\n",[33,687,688],{"__ignoreMap":31},[36,689,690,692,694],{"class":38,"line":39},[36,691,119],{"class":42},[36,693,146],{"class":64},[36,695,138],{"class":46},[183,697],{},"If this fails, the problem is Gunicorn or the app, not Nginx.",[176,700,701,704,185,706,721,723,724],{},[179,702,703],{},"Restart Gunicorn after changes",[183,705],{},[26,707,709],{"className":28,"code":708,"language":30,"meta":31,"style":31},"sudo systemctl restart gunicorn\n",[33,710,711],{"__ignoreMap":31},[36,712,713,715,717,719],{"class":38,"line":39},[36,714,43],{"class":42},[36,716,47],{"class":46},[36,718,98],{"class":46},[36,720,53],{"class":46},[183,722],{},"If it fails, immediately re-check logs:",[26,725,726],{"className":28,"code":264,"language":30,"meta":31,"style":31},[33,727,728],{"__ignoreMap":31},[36,729,730,732,734,736,738,740,742],{"class":38,"line":39},[36,731,43],{"class":42},[36,733,61],{"class":46},[36,735,65],{"class":64},[36,737,68],{"class":46},[36,739,71],{"class":64},[36,741,74],{"class":64},[36,743,77],{"class":64},[176,745,746,749,185,751],{},[179,747,748],{},"Reload Nginx after upstream or config changes",[183,750],{},[26,752,754],{"className":28,"code":753,"language":30,"meta":31,"style":31},"sudo systemctl reload nginx\n",[33,755,756],{"__ignoreMap":31},[36,757,758,760,762,764],{"class":38,"line":39},[36,759,43],{"class":42},[36,761,47],{"class":46},[36,763,110],{"class":46},[36,765,113],{"class":46},[176,767,768,771,185,773,785,787,788,628],{},[179,769,770],{},"Re-test the public endpoint",[183,772],{},[26,774,775],{"className":28,"code":188,"language":30,"meta":31,"style":31},[33,776,777],{"__ignoreMap":31},[36,778,779,781,783],{"class":38,"line":39},[36,780,119],{"class":42},[36,782,146],{"class":64},[36,784,199],{"class":46},[183,786],{},"Confirm the response is no longer ",[33,789,790],{},"502 Bad Gateway",[176,792,793,796,185,798,822,824,825,828],{},[179,794,795],{},"If the app starts but crashes under Gunicorn, test the app inside the virtualenv",[183,797],{},[26,799,801],{"className":28,"code":800,"language":30,"meta":31,"style":31},"source \u002Fpath\u002Fto\u002Fvenv\u002Fbin\u002Factivate\npython -c 'import yourapp'\n",[33,802,803,811],{"__ignoreMap":31},[36,804,805,808],{"class":38,"line":39},[36,806,807],{"class":64},"source",[36,809,810],{"class":46}," \u002Fpath\u002Fto\u002Fvenv\u002Fbin\u002Factivate\n",[36,812,813,816,819],{"class":38,"line":56},[36,814,815],{"class":42},"python",[36,817,818],{"class":64}," -c",[36,820,821],{"class":46}," 'import yourapp'\n",[183,823],{},"Replace ",[33,826,827],{},"yourapp"," with the actual import path used by Gunicorn.",[18,830,832],{"id":831},"common-causes","Common Causes",[237,834,835,844,857,863,869,875,888,898,904],{},[176,836,837,840,841],{},[179,838,839],{},"Gunicorn service is stopped or failed"," → Nginx has no upstream → restart Gunicorn and fix service errors from ",[33,842,843],{},"journalctl",[176,845,846,849,850,853,854],{},[179,847,848],{},"Nginx points to the wrong socket or port"," → upstream target does not exist → align ",[33,851,852],{},"proxy_pass"," with Gunicorn ",[33,855,856],{},"--bind",[176,858,859,862],{},[179,860,861],{},"Gunicorn socket file missing"," → service failed before creating it → inspect startup logs and service configuration",[176,864,865,868],{},[179,866,867],{},"Socket permission denied"," → Nginx worker cannot access the Unix socket → fix user\u002Fgroup permissions and socket location",[176,870,871,874],{},[179,872,873],{},"App import or startup crash"," → Gunicorn exits immediately → fix Python exceptions, missing packages, or bad module path",[176,876,877,880,881,250,884,887],{},[179,878,879],{},"Environment variables missing in systemd"," → app crashes only in production service mode → add ",[33,882,883],{},"Environment=",[33,885,886],{},"EnvironmentFile="," and restart",[176,889,890,893,894,897],{},[179,891,892],{},"Virtualenv or Gunicorn binary path incorrect"," → systemd launches the wrong executable → fix ",[33,895,896],{},"ExecStart"," paths",[176,899,900,903],{},[179,901,902],{},"Port already in use"," → Gunicorn cannot bind → free the port or change the bind address",[176,905,906,909],{},[179,907,908],{},"SELinux\u002FAppArmor or host security policy blocking access"," → Nginx cannot connect to upstream → review security denials and adjust policy if applicable",[18,911,913],{"id":912},"debugging-section","Debugging Section",[14,915,916],{},"Check service state:",[26,918,920],{"className":28,"code":919,"language":30,"meta":31,"style":31},"sudo systemctl status gunicorn nginx\n",[33,921,922],{"__ignoreMap":31},[36,923,924,926,928,930,932],{"class":38,"line":39},[36,925,43],{"class":42},[36,927,47],{"class":46},[36,929,50],{"class":46},[36,931,68],{"class":46},[36,933,113],{"class":46},[14,935,936],{},"View Gunicorn logs:",[26,938,940],{"className":28,"code":939,"language":30,"meta":31,"style":31},"sudo journalctl -u gunicorn -f\n",[33,941,942],{"__ignoreMap":31},[36,943,944,946,948,950,952],{"class":38,"line":39},[36,945,43],{"class":42},[36,947,61],{"class":46},[36,949,65],{"class":64},[36,951,68],{"class":46},[36,953,954],{"class":64}," -f\n",[14,956,957],{},"View Nginx logs:",[26,959,961],{"className":28,"code":960,"language":30,"meta":31,"style":31},"sudo tail -f \u002Fvar\u002Flog\u002Fnginx\u002Ferror.log \u002Fvar\u002Flog\u002Fnginx\u002Faccess.log\n",[33,962,963],{"__ignoreMap":31},[36,964,965,967,969,972,975],{"class":38,"line":39},[36,966,43],{"class":42},[36,968,343],{"class":46},[36,970,971],{"class":64}," -f",[36,973,974],{"class":46}," \u002Fvar\u002Flog\u002Fnginx\u002Ferror.log",[36,976,977],{"class":46}," \u002Fvar\u002Flog\u002Fnginx\u002Faccess.log\n",[14,979,980],{},"Show full Gunicorn unit:",[26,982,983],{"className":28,"code":442,"language":30,"meta":31,"style":31},[33,984,985],{"__ignoreMap":31},[36,986,987,989,991,993],{"class":38,"line":39},[36,988,43],{"class":42},[36,990,47],{"class":46},[36,992,453],{"class":46},[36,994,53],{"class":46},[14,996,997],{},"Validate Nginx syntax:",[26,999,1000],{"className":28,"code":311,"language":30,"meta":31,"style":31},[33,1001,1002],{"__ignoreMap":31},[36,1003,1004,1006,1008],{"class":38,"line":39},[36,1005,43],{"class":42},[36,1007,85],{"class":46},[36,1009,88],{"class":64},[14,1011,1012],{},"Inspect active listeners:",[26,1014,1016],{"className":28,"code":1015,"language":30,"meta":31,"style":31},"ss -ltnp | grep -E '8000|5000|gunicorn'\n",[33,1017,1018],{"__ignoreMap":31},[36,1019,1020,1022,1024,1026,1028,1031],{"class":38,"line":39},[36,1021,562],{"class":42},[36,1023,565],{"class":64},[36,1025,568],{"class":131},[36,1027,571],{"class":42},[36,1029,1030],{"class":64}," -E",[36,1032,1033],{"class":46}," '8000|5000|gunicorn'\n",[14,1035,1036],{},"Inspect socket file:",[26,1038,1040],{"className":28,"code":1039,"language":30,"meta":31,"style":31},"ls -lah \u002Frun\u002F | grep gunicorn\nstat \u002Frun\u002Fgunicorn.sock\n",[33,1041,1042,1058],{"__ignoreMap":31},[36,1043,1044,1046,1049,1052,1054,1056],{"class":38,"line":39},[36,1045,536],{"class":42},[36,1047,1048],{"class":64}," -lah",[36,1050,1051],{"class":46}," \u002Frun\u002F",[36,1053,568],{"class":131},[36,1055,571],{"class":42},[36,1057,53],{"class":46},[36,1059,1060,1063],{"class":38,"line":56},[36,1061,1062],{"class":64},"stat",[36,1064,542],{"class":46},[14,1066,1067],{},"Test upstream directly:",[26,1069,1071],{"className":28,"code":1070,"language":30,"meta":31,"style":31},"curl --unix-socket \u002Frun\u002Fgunicorn.sock http:\u002F\u002Flocalhost\u002F\ncurl -I http:\u002F\u002F127.0.0.1:8000\u002F\n",[33,1072,1073,1083],{"__ignoreMap":31},[36,1074,1075,1077,1079,1081],{"class":38,"line":39},[36,1076,119],{"class":42},[36,1078,122],{"class":64},[36,1080,125],{"class":46},[36,1082,679],{"class":46},[36,1084,1085,1087,1089],{"class":38,"line":56},[36,1086,119],{"class":42},[36,1088,146],{"class":64},[36,1090,138],{"class":46},[14,1092,1093],{},"Check app dependency and import issues inside the virtualenv:",[26,1095,1097],{"className":28,"code":1096,"language":30,"meta":31,"style":31},"source \u002Fpath\u002Fto\u002Fvenv\u002Fbin\u002Factivate && python -c 'import yourapp'\n",[33,1098,1099],{"__ignoreMap":31},[36,1100,1101,1103,1106,1110,1112,1114],{"class":38,"line":39},[36,1102,807],{"class":64},[36,1104,1105],{"class":46}," \u002Fpath\u002Fto\u002Fvenv\u002Fbin\u002Factivate",[36,1107,1109],{"class":1108},"sVt8B"," && ",[36,1111,815],{"class":42},[36,1113,818],{"class":64},[36,1115,821],{"class":46},[14,1117,1118],{},"Inspect running processes:",[26,1120,1122],{"className":28,"code":1121,"language":30,"meta":31,"style":31},"ps aux | grep gunicorn\n",[33,1123,1124],{"__ignoreMap":31},[36,1125,1126,1129,1132,1134,1136],{"class":38,"line":39},[36,1127,1128],{"class":42},"ps",[36,1130,1131],{"class":46}," aux",[36,1133,568],{"class":131},[36,1135,571],{"class":42},[36,1137,53],{"class":46},[14,1139,1140],{},"What to look for:",[237,1142,1143,1149,1154,1159],{},[176,1144,1145,1148],{},[33,1146,1147],{},"Connection refused"," → Gunicorn is not listening",[176,1150,1151,1153],{},[33,1152,364],{}," → wrong or missing socket",[176,1155,1156,1158],{},[33,1157,369],{}," → socket access issue",[176,1160,1161],{},"Python traceback → app startup failure",[18,1163,1165],{"id":1164},"checklist","Checklist",[237,1167,1170,1184,1192,1198,1204,1210,1216,1222],{"className":1168},[1169],"contains-task-list",[176,1171,1174,1179,1180,1183],{"className":1172},[1173],"task-list-item",[1175,1176],"input",{"disabled":1177,"type":1178},true,"checkbox"," ",[33,1181,1182],{},"nginx -t"," passes without errors",[176,1185,1187,1189,1190],{"className":1186},[1173],[1175,1188],{"disabled":1177,"type":1178}," Gunicorn service is ",[33,1191,243],{},[176,1193,1195,1197],{"className":1194},[1173],[1175,1196],{"disabled":1177,"type":1178}," Nginx upstream target matches the Gunicorn bind address exactly",[176,1199,1201,1203],{"className":1200},[1173],[1175,1202],{"disabled":1177,"type":1178}," Unix socket exists or TCP port is listening",[176,1205,1207,1209],{"className":1206},[1173],[1175,1208],{"disabled":1177,"type":1178}," Nginx has permission to access the Gunicorn socket if using Unix sockets",[176,1211,1213,1215],{"className":1212},[1173],[1175,1214],{"disabled":1177,"type":1178}," Direct curl to Gunicorn works before testing through Nginx",[176,1217,1219,1221],{"className":1218},[1173],[1175,1220],{"disabled":1177,"type":1178}," Public request no longer returns 502",[176,1223,1225,1227],{"className":1224},[1173],[1175,1226],{"disabled":1177,"type":1178}," Recent deploy changes to env vars, paths, or service files have been reloaded correctly",[18,1229,1231],{"id":1230},"related-guides","Related Guides",[237,1233,1234,1241,1247,1253],{},[176,1235,1236],{},[1237,1238,1240],"a",{"href":1239},"\u002Fdeploy\u002Fdeploy-flask-with-nginx-plus-gunicorn-step-by-step-guide","Deploy Flask with Nginx + Gunicorn (Step-by-Step Guide)",[176,1242,1243],{},[1237,1244,1246],{"href":1245},"\u002Ffix-issues\u002Ffix-nginx-not-connecting-to-gunicorn-connection-refused","Fix: Nginx Not Connecting to Gunicorn (Connection Refused)",[176,1248,1249],{},[1237,1250,1252],{"href":1251},"\u002Ffix-issues\u002Fflask-gunicorn-service-failed-to-start","Flask Gunicorn Service Failed to Start",[176,1254,1255],{},[1237,1256,1258],{"href":1257},"\u002Fchecklist\u002Fflask-production-checklist-everything-you-must-do","Flask Production Checklist (Everything You Must Do)",[18,1260,1262],{"id":1261},"faq","FAQ",[14,1264,1265,1268,1270],{},[179,1266,1267],{},"What does 502 Bad Gateway mean for Flask behind Nginx?",[183,1269],{},"\nNginx could not get a valid response from Gunicorn, usually because the upstream is down, misconfigured, or inaccessible.",[14,1272,1273,1276,1278],{},[179,1274,1275],{},"How do I know if the problem is Nginx or Gunicorn?",[183,1277],{},"\nTest Gunicorn directly with the socket or port. If direct access fails, the issue is Gunicorn or the app. If it works directly, inspect Nginx config and permissions.",[14,1280,1281,1287,1289],{},[179,1282,1283,1284,1286],{},"Why do I see ",[33,1285,359],{}," in Nginx logs?",[183,1288],{},"\nNginx is trying to reach a port where Gunicorn is not listening, or Gunicorn crashed before handling requests.",[14,1291,1292,1301,1303],{},[179,1293,1283,1294,1296,1297,1300],{},[33,1295,369],{}," for ",[33,1298,1299],{},"gunicorn.sock","?",[183,1302],{},"\nThe Nginx worker user cannot access the Unix socket or its parent directory. Fix ownership, group membership, or socket permissions.",[14,1305,1306,1309,1311],{},[179,1307,1308],{},"Should I restart or reload services after fixing 502 issues?",[183,1310],{},"\nRestart Gunicorn when its app, bind, or environment changes. Reload Nginx after config updates. Then re-test both the upstream and the public endpoint.",[18,1313,1315],{"id":1314},"final-takeaway","Final Takeaway",[14,1317,1318],{},"A Flask 502 is almost always an upstream connectivity problem between Nginx and Gunicorn. Check Gunicorn status, match the bind target to Nginx exactly, test the upstream directly, and use logs to identify whether the failure is service state, path mismatch, permissions, or app startup.",[1320,1321,1322],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":31,"searchDepth":56,"depth":56,"links":1324},[1325,1326,1327,1328,1329,1330,1331,1332,1333],{"id":20,"depth":56,"text":21},{"id":155,"depth":56,"text":156},{"id":170,"depth":56,"text":171},{"id":831,"depth":56,"text":832},{"id":912,"depth":56,"text":913},{"id":1164,"depth":56,"text":1165},{"id":1230,"depth":56,"text":1231},{"id":1261,"depth":56,"text":1262},{"id":1314,"depth":56,"text":1315},"Complete guide on fix flask 502 bad gateway (step-by-step guide) for Flask production environments.","md",{"ogTitle":5,"ogDescription":1334,"twitterCard":1337,"robots":1338,"canonical":1339},"summary_large_image","index, follow","https:\u002F\u002Fflask-deployment.com\u002Ffix-issues\u002Ffix-flask-502-bad-gateway-step-by-step-guide","\u002Ffix-issues\u002Ffix-flask-502-bad-gateway-step-by-step-guide",{"title":5,"description":1334},"fix-issues\u002Ffix-flask-502-bad-gateway-step-by-step-guide","w2a-XZUmyZ3tDpA6xpA1dFKNfKFAsG7yjTRhGAffO_Q",1776805765050]