[{"data":1,"prerenderedAt":1446},["ShallowReactive",2],{"\u002Ffix-issues\u002Ffix-nginx-not-connecting-to-gunicorn-connection-refused":3},{"id":4,"title":5,"body":6,"description":1436,"extension":1437,"meta":1438,"navigation":416,"path":1442,"seo":1443,"stem":1444,"__hash__":1445},"content\u002Ffix-issues\u002Ffix-nginx-not-connecting-to-gunicorn-connection-refused.md","Fix: Nginx Not Connecting to Gunicorn (Connection Refused)",{"type":7,"value":8,"toc":1419},"minimark",[9,13,17,22,25,129,140,144,147,151,979,983,1057,1061,1064,1225,1228,1248,1252,1317,1321,1343,1347,1352,1361,1365,1378,1382,1385,1389,1401,1405,1408,1412,1415],[10,11,5],"h1",{"id":12},"fix-nginx-not-connecting-to-gunicorn-connection-refused",[14,15,16],"p",{},"If Nginx cannot connect to Gunicorn and you are seeing connection refused, 502, or upstream errors, this guide shows you how to restore the backend connection step-by-step. The goal is to verify Gunicorn is running, confirm Nginx points to the correct socket or port, and fix permission or service issues that block the upstream.",[18,19,21],"h2",{"id":20},"quick-fix-quick-setup","Quick Fix \u002F Quick Setup",[14,23,24],{},"Run the basic validation sequence 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 restart gunicorn\nsudo systemctl restart nginx\nsudo systemctl status gunicorn --no-pager\nsudo ss -ltnp | grep 8000\nsudo ls -l \u002Frun\u002Fgunicorn.sock\nsudo nginx -t\n","bash","",[33,34,35,54,66,83,104,118],"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}," restart",[36,52,53],{"class":46}," gunicorn\n",[36,55,57,59,61,63],{"class":38,"line":56},2,[36,58,43],{"class":42},[36,60,47],{"class":46},[36,62,50],{"class":46},[36,64,65],{"class":46}," nginx\n",[36,67,69,71,73,76,79],{"class":38,"line":68},3,[36,70,43],{"class":42},[36,72,47],{"class":46},[36,74,75],{"class":46}," status",[36,77,78],{"class":46}," gunicorn",[36,80,82],{"class":81},"sj4cs"," --no-pager\n",[36,84,86,88,91,94,98,101],{"class":38,"line":85},4,[36,87,43],{"class":42},[36,89,90],{"class":46}," ss",[36,92,93],{"class":81}," -ltnp",[36,95,97],{"class":96},"szBVR"," |",[36,99,100],{"class":42}," grep",[36,102,103],{"class":81}," 8000\n",[36,105,107,109,112,115],{"class":38,"line":106},5,[36,108,43],{"class":42},[36,110,111],{"class":46}," ls",[36,113,114],{"class":81}," -l",[36,116,117],{"class":46}," \u002Frun\u002Fgunicorn.sock\n",[36,119,121,123,126],{"class":38,"line":120},6,[36,122,43],{"class":42},[36,124,125],{"class":46}," nginx",[36,127,128],{"class":81}," -t\n",[14,130,131,132,135,136,139],{},"Use the port check if Gunicorn is bound to ",[33,133,134],{},"127.0.0.1:8000",". Use the socket check if Nginx is configured with ",[33,137,138],{},"unix:\u002Frun\u002Fgunicorn.sock",". If Gunicorn is not active or the socket does not exist, fix the Gunicorn service first.",[18,141,143],{"id":142},"whats-happening","What’s Happening",[14,145,146],{},"Nginx proxies requests to Gunicorn through either a Unix socket or a local TCP port. A connection refused error means Nginx reached the target path or address, but no process was listening there, the socket path was wrong, or access to the socket was blocked. In most cases, the issue is a stopped Gunicorn service, a bind mismatch, or socket permission problems.",[18,148,150],{"id":149},"step-by-step-guide","Step-by-Step Guide",[152,153,154,207,261,324,379,521,569,664,707,781,844,868,908,959,967],"ol",{},[155,156,157,161,164,165,186,188,189],"li",{},[158,159,160],"strong",{},"Confirm the exact Nginx upstream error",[162,163],"br",{},"Run:",[26,166,168],{"className":28,"code":167,"language":30,"meta":31,"style":31},"sudo tail -n 50 \u002Fvar\u002Flog\u002Fnginx\u002Ferror.log\n",[33,169,170],{"__ignoreMap":31},[36,171,172,174,177,180,183],{"class":38,"line":39},[36,173,43],{"class":42},[36,175,176],{"class":46}," tail",[36,178,179],{"class":81}," -n",[36,181,182],{"class":81}," 50",[36,184,185],{"class":46}," \u002Fvar\u002Flog\u002Fnginx\u002Ferror.log\n",[162,187],{},"Look for messages such as:",[190,191,192,197,202],"ul",{},[155,193,194],{},[33,195,196],{},"connect() failed (111: Connection refused) while connecting to upstream",[155,198,199],{},[33,200,201],{},"No such file or directory",[155,203,204],{},[33,205,206],{},"Permission denied",[155,208,209,212,164,214,231,233,234],{},[158,210,211],{},"Check whether Gunicorn is running",[162,213],{},[26,215,217],{"className":28,"code":216,"language":30,"meta":31,"style":31},"sudo systemctl status gunicorn --no-pager\n",[33,218,219],{"__ignoreMap":31},[36,220,221,223,225,227,229],{"class":38,"line":39},[36,222,43],{"class":42},[36,224,47],{"class":46},[36,226,75],{"class":46},[36,228,78],{"class":46},[36,230,82],{"class":81},[162,232],{},"If it is inactive or failed:",[26,235,237],{"className":28,"code":236,"language":30,"meta":31,"style":31},"sudo systemctl restart gunicorn\nsudo systemctl status gunicorn --no-pager\n",[33,238,239,249],{"__ignoreMap":31},[36,240,241,243,245,247],{"class":38,"line":39},[36,242,43],{"class":42},[36,244,47],{"class":46},[36,246,50],{"class":46},[36,248,53],{"class":46},[36,250,251,253,255,257,259],{"class":38,"line":56},[36,252,43],{"class":42},[36,254,47],{"class":46},[36,256,75],{"class":46},[36,258,78],{"class":46},[36,260,82],{"class":81},[155,262,263,266,164,268,292,294,295,315,317,318,323],{},[158,264,265],{},"Review Gunicorn logs for startup failures",[162,267],{},[26,269,271],{"className":28,"code":270,"language":30,"meta":31,"style":31},"sudo journalctl -u gunicorn -n 100 --no-pager\n",[33,272,273],{"__ignoreMap":31},[36,274,275,277,280,283,285,287,290],{"class":38,"line":39},[36,276,43],{"class":42},[36,278,279],{"class":46}," journalctl",[36,281,282],{"class":81}," -u",[36,284,78],{"class":46},[36,286,179],{"class":81},[36,288,289],{"class":81}," 100",[36,291,82],{"class":81},[162,293],{},"Fix any of these before continuing:",[190,296,297,300,303,306,312],{},[155,298,299],{},"import errors",[155,301,302],{},"missing dependencies",[155,304,305],{},"invalid virtualenv path",[155,307,308,309],{},"wrong ",[33,310,311],{},"WorkingDirectory",[155,313,314],{},"bad WSGI module path",[162,316],{},"If Gunicorn fails to start at all, use ",[319,320,322],"a",{"href":321},"\u002Ffix-issues\u002Fflask-gunicorn-service-failed-to-start","Flask Gunicorn Service Failed to Start",".",[155,325,326,329,331,332,348,350,351,364,366,367],{},[158,327,328],{},"Verify how Gunicorn is bound",[162,330],{},"Inspect the systemd unit:",[26,333,335],{"className":28,"code":334,"language":30,"meta":31,"style":31},"sudo systemctl cat gunicorn\n",[33,336,337],{"__ignoreMap":31},[36,338,339,341,343,346],{"class":38,"line":39},[36,340,43],{"class":42},[36,342,47],{"class":46},[36,344,345],{"class":46}," cat",[36,347,53],{"class":46},[162,349],{},"Look for a bind target such as:",[26,352,354],{"className":28,"code":353,"language":30,"meta":31,"style":31},"--bind unix:\u002Frun\u002Fgunicorn.sock\n",[33,355,356],{"__ignoreMap":31},[36,357,358,361],{"class":38,"line":39},[36,359,360],{"class":42},"--bind",[36,362,363],{"class":46}," unix:\u002Frun\u002Fgunicorn.sock\n",[162,365],{},"or:",[26,368,370],{"className":28,"code":369,"language":30,"meta":31,"style":31},"--bind 127.0.0.1:8000\n",[33,371,372],{"__ignoreMap":31},[36,373,374,376],{"class":38,"line":39},[36,375,360],{"class":42},[36,377,378],{"class":46}," 127.0.0.1:8000\n",[155,380,381,384,386,387,389,390,464,466,467],{},[158,382,383],{},"Match the Nginx upstream to the Gunicorn bind target",[162,385],{},"If using a Unix socket, your Nginx config should point to the same socket path.",[162,388],{},"Example:",[26,391,395],{"className":392,"code":393,"language":394,"meta":31,"style":31},"language-nginx shiki shiki-themes github-light github-dark","server {\n    listen 80;\n    server_name your-domain.com;\n\n    location \u002F {\n        proxy_pass http:\u002F\u002Funix:\u002Frun\u002Fgunicorn.sock;\n        proxy_set_header Host $host;\n        proxy_set_header X-Real-IP $remote_addr;\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n        proxy_set_header X-Forwarded-Proto $scheme;\n    }\n}\n","nginx",[33,396,397,402,407,412,418,423,428,434,440,446,452,458],{"__ignoreMap":31},[36,398,399],{"class":38,"line":39},[36,400,401],{},"server {\n",[36,403,404],{"class":38,"line":56},[36,405,406],{},"    listen 80;\n",[36,408,409],{"class":38,"line":68},[36,410,411],{},"    server_name your-domain.com;\n",[36,413,414],{"class":38,"line":85},[36,415,417],{"emptyLinePlaceholder":416},true,"\n",[36,419,420],{"class":38,"line":106},[36,421,422],{},"    location \u002F {\n",[36,424,425],{"class":38,"line":120},[36,426,427],{},"        proxy_pass http:\u002F\u002Funix:\u002Frun\u002Fgunicorn.sock;\n",[36,429,431],{"class":38,"line":430},7,[36,432,433],{},"        proxy_set_header Host $host;\n",[36,435,437],{"class":38,"line":436},8,[36,438,439],{},"        proxy_set_header X-Real-IP $remote_addr;\n",[36,441,443],{"class":38,"line":442},9,[36,444,445],{},"        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n",[36,447,449],{"class":38,"line":448},10,[36,450,451],{},"        proxy_set_header X-Forwarded-Proto $scheme;\n",[36,453,455],{"class":38,"line":454},11,[36,456,457],{},"    }\n",[36,459,461],{"class":38,"line":460},12,[36,462,463],{},"}\n",[162,465],{},"If using TCP:",[26,468,470],{"className":392,"code":469,"language":394,"meta":31,"style":31},"server {\n    listen 80;\n    server_name your-domain.com;\n\n    location \u002F {\n        proxy_pass http:\u002F\u002F127.0.0.1:8000;\n        proxy_set_header Host $host;\n        proxy_set_header X-Real-IP $remote_addr;\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n        proxy_set_header X-Forwarded-Proto $scheme;\n    }\n}\n",[33,471,472,476,480,484,488,492,497,501,505,509,513,517],{"__ignoreMap":31},[36,473,474],{"class":38,"line":39},[36,475,401],{},[36,477,478],{"class":38,"line":56},[36,479,406],{},[36,481,482],{"class":38,"line":68},[36,483,411],{},[36,485,486],{"class":38,"line":85},[36,487,417],{"emptyLinePlaceholder":416},[36,489,490],{"class":38,"line":106},[36,491,422],{},[36,493,494],{"class":38,"line":120},[36,495,496],{},"        proxy_pass http:\u002F\u002F127.0.0.1:8000;\n",[36,498,499],{"class":38,"line":430},[36,500,433],{},[36,502,503],{"class":38,"line":436},[36,504,439],{},[36,506,507],{"class":38,"line":442},[36,508,445],{},[36,510,511],{"class":38,"line":448},[36,512,451],{},[36,514,515],{"class":38,"line":454},[36,516,457],{},[36,518,519],{"class":38,"line":460},[36,520,463],{},[155,522,523,526,528,529,544,546,547,566,568],{},[158,524,525],{},"Test whether the bind target actually exists",[162,527],{},"For a Unix socket:",[26,530,532],{"className":28,"code":531,"language":30,"meta":31,"style":31},"sudo ls -l \u002Frun\u002Fgunicorn.sock\n",[33,533,534],{"__ignoreMap":31},[36,535,536,538,540,542],{"class":38,"line":39},[36,537,43],{"class":42},[36,539,111],{"class":46},[36,541,114],{"class":81},[36,543,117],{"class":46},[162,545],{},"For TCP:",[26,548,550],{"className":28,"code":549,"language":30,"meta":31,"style":31},"sudo ss -ltnp | grep 8000\n",[33,551,552],{"__ignoreMap":31},[36,553,554,556,558,560,562,564],{"class":38,"line":39},[36,555,43],{"class":42},[36,557,90],{"class":46},[36,559,93],{"class":81},[36,561,97],{"class":96},[36,563,100],{"class":42},[36,565,103],{"class":81},[162,567],{},"If nothing is listening and no socket exists, Gunicorn is not bound correctly.",[155,570,571,574,576,577,642,644,645],{},[158,572,573],{},"Fix the Gunicorn systemd service if needed",[162,575],{},"Example systemd service:",[26,578,582],{"className":579,"code":580,"language":581,"meta":31,"style":31},"language-ini shiki shiki-themes github-light github-dark","[Unit]\nDescription=Gunicorn instance for Flask app\nAfter=network.target\n\n[Service]\nUser=www-data\nGroup=www-data\nWorkingDirectory=\u002Fsrv\u002Fapp\nExecStart=\u002Fsrv\u002Fapp\u002Fvenv\u002Fbin\u002Fgunicorn --workers 3 --bind unix:\u002Frun\u002Fgunicorn.sock wsgi:app\n\n[Install]\nWantedBy=multi-user.target\n","ini",[33,583,584,589,594,599,603,608,613,618,623,628,632,637],{"__ignoreMap":31},[36,585,586],{"class":38,"line":39},[36,587,588],{},"[Unit]\n",[36,590,591],{"class":38,"line":56},[36,592,593],{},"Description=Gunicorn instance for Flask app\n",[36,595,596],{"class":38,"line":68},[36,597,598],{},"After=network.target\n",[36,600,601],{"class":38,"line":85},[36,602,417],{"emptyLinePlaceholder":416},[36,604,605],{"class":38,"line":106},[36,606,607],{},"[Service]\n",[36,609,610],{"class":38,"line":120},[36,611,612],{},"User=www-data\n",[36,614,615],{"class":38,"line":430},[36,616,617],{},"Group=www-data\n",[36,619,620],{"class":38,"line":436},[36,621,622],{},"WorkingDirectory=\u002Fsrv\u002Fapp\n",[36,624,625],{"class":38,"line":442},[36,626,627],{},"ExecStart=\u002Fsrv\u002Fapp\u002Fvenv\u002Fbin\u002Fgunicorn --workers 3 --bind unix:\u002Frun\u002Fgunicorn.sock wsgi:app\n",[36,629,630],{"class":38,"line":448},[36,631,417],{"emptyLinePlaceholder":416},[36,633,634],{"class":38,"line":454},[36,635,636],{},"[Install]\n",[36,638,639],{"class":38,"line":460},[36,640,641],{},"WantedBy=multi-user.target\n",[162,643],{},"Validate:",[190,646,647,653,658],{},[155,648,649,652],{},[33,650,651],{},"ExecStart"," points to the correct Gunicorn binary inside the virtualenv",[155,654,655,657],{},[33,656,311],{}," points to the app directory",[155,659,660,663],{},[33,661,662],{},"wsgi:app"," matches your app entrypoint",[155,665,666,669,164,671],{},[158,667,668],{},"Reload systemd and restart Gunicorn after changes",[162,670],{},[26,672,674],{"className":28,"code":673,"language":30,"meta":31,"style":31},"sudo systemctl daemon-reload\nsudo systemctl restart gunicorn\nsudo systemctl status gunicorn --no-pager\n",[33,675,676,685,695],{"__ignoreMap":31},[36,677,678,680,682],{"class":38,"line":39},[36,679,43],{"class":42},[36,681,47],{"class":46},[36,683,684],{"class":46}," daemon-reload\n",[36,686,687,689,691,693],{"class":38,"line":56},[36,688,43],{"class":42},[36,690,47],{"class":46},[36,692,50],{"class":46},[36,694,53],{"class":46},[36,696,697,699,701,703,705],{"class":38,"line":68},[36,698,43],{"class":42},[36,700,47],{"class":46},[36,702,75],{"class":46},[36,704,78],{"class":46},[36,706,82],{"class":81},[155,708,709,712,164,714,728,730,731,734,735,737,738,770,772,773,776,777,780],{},[158,710,711],{},"Check socket ownership and permissions",[162,713],{},[26,715,716],{"className":28,"code":531,"language":30,"meta":31,"style":31},[33,717,718],{"__ignoreMap":31},[36,719,720,722,724,726],{"class":38,"line":39},[36,721,43],{"class":42},[36,723,111],{"class":46},[36,725,114],{"class":81},[36,727,117],{"class":46},[162,729],{},"Nginx must be able to access the socket. If Nginx runs as ",[33,732,733],{},"www-data",", the socket owner\u002Fgroup must allow access.",[162,736],{},"Example output:",[26,739,741],{"className":28,"code":740,"language":30,"meta":31,"style":31},"srw-rw---- 1 www-data www-data 0 Apr 21 12:00 \u002Frun\u002Fgunicorn.sock\n",[33,742,743],{"__ignoreMap":31},[36,744,745,748,751,754,756,759,762,765,768],{"class":38,"line":39},[36,746,747],{"class":42},"srw-rw----",[36,749,750],{"class":81}," 1",[36,752,753],{"class":46}," www-data",[36,755,753],{"class":46},[36,757,758],{"class":81}," 0",[36,760,761],{"class":46}," Apr",[36,763,764],{"class":81}," 21",[36,766,767],{"class":46}," 12:00",[36,769,117],{"class":46},[162,771],{},"If permissions are wrong, adjust the Gunicorn service ",[33,774,775],{},"User"," and ",[33,778,779],{},"Group",", or configure socket creation appropriately.",[155,782,783,786,164,788,801,803,804,827,829,830],{},[158,784,785],{},"Validate the active Nginx configuration",[162,787],{},[26,789,791],{"className":28,"code":790,"language":30,"meta":31,"style":31},"sudo nginx -t\n",[33,792,793],{"__ignoreMap":31},[36,794,795,797,799],{"class":38,"line":39},[36,796,43],{"class":42},[36,798,125],{"class":46},[36,800,128],{"class":81},[162,802],{},"Also check for stale references:",[26,805,807],{"className":28,"code":806,"language":30,"meta":31,"style":31},"sudo grep -R \"proxy_pass\\|upstream\\|gunicorn.sock\\|127.0.0.1:8000\" \u002Fetc\u002Fnginx\u002Fsites-enabled \u002Fetc\u002Fnginx\u002Fnginx.conf\n",[33,808,809],{"__ignoreMap":31},[36,810,811,813,815,818,821,824],{"class":38,"line":39},[36,812,43],{"class":42},[36,814,100],{"class":46},[36,816,817],{"class":81}," -R",[36,819,820],{"class":46}," \"proxy_pass\\|upstream\\|gunicorn.sock\\|127.0.0.1:8000\"",[36,822,823],{"class":46}," \u002Fetc\u002Fnginx\u002Fsites-enabled",[36,825,826],{"class":46}," \u002Fetc\u002Fnginx\u002Fnginx.conf\n",[162,828],{},"Fix:",[190,831,832,835,838,841],{},[155,833,834],{},"wrong socket path",[155,836,837],{},"wrong port",[155,839,840],{},"duplicate old upstream blocks",[155,842,843],{},"syntax errors",[155,845,846,849,851,852],{},[158,847,848],{},"Reload Nginx",[162,850],{},"After confirming Gunicorn is reachable:",[26,853,855],{"className":28,"code":854,"language":30,"meta":31,"style":31},"sudo systemctl reload nginx\n",[33,856,857],{"__ignoreMap":31},[36,858,859,861,863,866],{"class":38,"line":39},[36,860,43],{"class":42},[36,862,47],{"class":46},[36,864,865],{"class":46}," reload",[36,867,65],{"class":46},[155,869,870,873,546,875,891,893,894],{},[158,871,872],{},"Test the application locally first",[162,874],{},[26,876,878],{"className":28,"code":877,"language":30,"meta":31,"style":31},"curl -I http:\u002F\u002F127.0.0.1:8000\n",[33,879,880],{"__ignoreMap":31},[36,881,882,885,888],{"class":38,"line":39},[36,883,884],{"class":42},"curl",[36,886,887],{"class":81}," -I",[36,889,890],{"class":46}," http:\u002F\u002F127.0.0.1:8000\n",[162,892],{},"For a socket-based setup, confirm the socket exists first, then test through Nginx:",[26,895,897],{"className":28,"code":896,"language":30,"meta":31,"style":31},"curl -I http:\u002F\u002Flocalhost\n",[33,898,899],{"__ignoreMap":31},[36,900,901,903,905],{"class":38,"line":39},[36,902,884],{"class":42},[36,904,887],{"class":81},[36,906,907],{"class":46}," http:\u002F\u002Flocalhost\n",[155,909,910,913,164,915,929,366,931,945,947,948,951,952,951,955,958],{},[158,911,912],{},"Test the full request path",[162,914],{},[26,916,918],{"className":28,"code":917,"language":30,"meta":31,"style":31},"curl -I http:\u002F\u002Fyour-domain\n",[33,919,920],{"__ignoreMap":31},[36,921,922,924,926],{"class":38,"line":39},[36,923,884],{"class":42},[36,925,887],{"class":81},[36,927,928],{"class":46}," http:\u002F\u002Fyour-domain\n",[162,930],{},[26,932,934],{"className":28,"code":933,"language":30,"meta":31,"style":31},"curl -I http:\u002F\u002Fserver-ip\n",[33,935,936],{"__ignoreMap":31},[36,937,938,940,942],{"class":38,"line":39},[36,939,884],{"class":42},[36,941,887],{"class":81},[36,943,944],{"class":46}," http:\u002F\u002Fserver-ip\n",[162,946],{},"A ",[33,949,950],{},"200",", ",[33,953,954],{},"301",[33,956,957],{},"302",", or valid app response confirms Nginx can reach Gunicorn.",[155,960,961,964,966],{},[158,962,963],{},"Check local security policy if the configuration still looks correct",[162,965],{},"On hardened systems, SELinux or other local policy controls can block access to sockets or local ports. Review those controls if Gunicorn is running and Nginx still cannot connect.",[155,968,969,972,974,975,323],{},[158,970,971],{},"If the error presents as a 502 instead of connection refused",[162,973],{},"Use the broader upstream troubleshooting page at ",[319,976,978],{"href":977},"\u002Ffix-issues\u002Ffix-flask-502-bad-gateway-step-by-step-guide","Fix Flask 502 Bad Gateway (Step-by-Step Guide)",[18,980,982],{"id":981},"common-causes","Common Causes",[190,984,985,994,1007,1013,1024,1030,1039,1045,1051],{},[155,986,987,990,991,323],{},[158,988,989],{},"Gunicorn service is stopped or crashed"," → Nginx has no backend to connect to → Restart Gunicorn and inspect logs with ",[33,992,993],{},"journalctl",[155,995,996,999,1000,1003,1004,1006],{},[158,997,998],{},"Nginx points to the wrong Unix socket path"," → The configured socket file does not exist → Update ",[33,1001,1002],{},"proxy_pass"," or Gunicorn ",[33,1005,360],{}," so both use the same path.",[155,1008,1009,1012],{},[158,1010,1011],{},"Nginx points to the wrong TCP port"," → Nothing is listening on that port → Change Nginx upstream or Gunicorn bind to match.",[155,1014,1015,1018,1019,951,1021,1023],{},[158,1016,1017],{},"Gunicorn failed to start because of import or app path errors"," → Service never opens the socket or port → Fix ",[33,1020,651],{},[33,1022,311],{},", and the WSGI module path.",[155,1025,1026,1029],{},[158,1027,1028],{},"Socket permission denied"," → Gunicorn created the socket but Nginx cannot read or connect to it → Adjust service user, group, and socket permissions.",[155,1031,1032,1035,1036,1038],{},[158,1033,1034],{},"Virtualenv or binary path is wrong in systemd"," → Gunicorn command fails during startup → Point ",[33,1037,651],{}," to the correct Gunicorn binary inside the virtualenv.",[155,1040,1041,1044],{},[158,1042,1043],{},"Old Nginx config still loaded"," → Nginx proxies to an outdated socket or port → Test config and reload Nginx.",[155,1046,1047,1050],{},[158,1048,1049],{},"Runtime directory cleanup removed the socket"," → Nginx references a stale path after reboot or restart → Ensure the service recreates the socket on startup.",[155,1052,1053,1056],{},[158,1054,1055],{},"Firewall or local security policy blocks access to the bind target"," → Nginx cannot reach Gunicorn on TCP or local socket policy → Review local security configuration.",[18,1058,1060],{"id":1059},"debugging-section","Debugging Section",[14,1062,1063],{},"Check these commands in order:",[26,1065,1067],{"className":28,"code":1066,"language":30,"meta":31,"style":31},"sudo tail -n 100 \u002Fvar\u002Flog\u002Fnginx\u002Ferror.log\nsudo tail -n 100 \u002Fvar\u002Flog\u002Fnginx\u002Faccess.log\nsudo systemctl status gunicorn --no-pager\nsudo journalctl -u gunicorn -n 100 --no-pager\nsudo journalctl -u nginx -n 100 --no-pager\nsudo nginx -t\nsudo systemctl cat gunicorn\nsudo ss -ltnp | grep 8000\nsudo ls -l \u002Frun\u002Fgunicorn.sock\nps aux | grep gunicorn\ncurl -I http:\u002F\u002F127.0.0.1:8000\ncurl -I http:\u002F\u002Flocalhost\nsudo grep -R \"proxy_pass\\|upstream\\|gunicorn.sock\\|127.0.0.1:8000\" \u002Fetc\u002Fnginx\u002Fsites-enabled \u002Fetc\u002Fnginx\u002Fnginx.conf\n",[33,1068,1069,1081,1094,1106,1122,1138,1146,1156,1170,1180,1194,1202,1210],{"__ignoreMap":31},[36,1070,1071,1073,1075,1077,1079],{"class":38,"line":39},[36,1072,43],{"class":42},[36,1074,176],{"class":46},[36,1076,179],{"class":81},[36,1078,289],{"class":81},[36,1080,185],{"class":46},[36,1082,1083,1085,1087,1089,1091],{"class":38,"line":56},[36,1084,43],{"class":42},[36,1086,176],{"class":46},[36,1088,179],{"class":81},[36,1090,289],{"class":81},[36,1092,1093],{"class":46}," \u002Fvar\u002Flog\u002Fnginx\u002Faccess.log\n",[36,1095,1096,1098,1100,1102,1104],{"class":38,"line":68},[36,1097,43],{"class":42},[36,1099,47],{"class":46},[36,1101,75],{"class":46},[36,1103,78],{"class":46},[36,1105,82],{"class":81},[36,1107,1108,1110,1112,1114,1116,1118,1120],{"class":38,"line":85},[36,1109,43],{"class":42},[36,1111,279],{"class":46},[36,1113,282],{"class":81},[36,1115,78],{"class":46},[36,1117,179],{"class":81},[36,1119,289],{"class":81},[36,1121,82],{"class":81},[36,1123,1124,1126,1128,1130,1132,1134,1136],{"class":38,"line":106},[36,1125,43],{"class":42},[36,1127,279],{"class":46},[36,1129,282],{"class":81},[36,1131,125],{"class":46},[36,1133,179],{"class":81},[36,1135,289],{"class":81},[36,1137,82],{"class":81},[36,1139,1140,1142,1144],{"class":38,"line":120},[36,1141,43],{"class":42},[36,1143,125],{"class":46},[36,1145,128],{"class":81},[36,1147,1148,1150,1152,1154],{"class":38,"line":430},[36,1149,43],{"class":42},[36,1151,47],{"class":46},[36,1153,345],{"class":46},[36,1155,53],{"class":46},[36,1157,1158,1160,1162,1164,1166,1168],{"class":38,"line":436},[36,1159,43],{"class":42},[36,1161,90],{"class":46},[36,1163,93],{"class":81},[36,1165,97],{"class":96},[36,1167,100],{"class":42},[36,1169,103],{"class":81},[36,1171,1172,1174,1176,1178],{"class":38,"line":442},[36,1173,43],{"class":42},[36,1175,111],{"class":46},[36,1177,114],{"class":81},[36,1179,117],{"class":46},[36,1181,1182,1185,1188,1190,1192],{"class":38,"line":448},[36,1183,1184],{"class":42},"ps",[36,1186,1187],{"class":46}," aux",[36,1189,97],{"class":96},[36,1191,100],{"class":42},[36,1193,53],{"class":46},[36,1195,1196,1198,1200],{"class":38,"line":454},[36,1197,884],{"class":42},[36,1199,887],{"class":81},[36,1201,890],{"class":46},[36,1203,1204,1206,1208],{"class":38,"line":460},[36,1205,884],{"class":42},[36,1207,887],{"class":81},[36,1209,907],{"class":46},[36,1211,1213,1215,1217,1219,1221,1223],{"class":38,"line":1212},13,[36,1214,43],{"class":42},[36,1216,100],{"class":46},[36,1218,817],{"class":81},[36,1220,820],{"class":46},[36,1222,823],{"class":46},[36,1224,826],{"class":46},[14,1226,1227],{},"What to look for:",[190,1229,1230,1233,1236,1239,1242,1245],{},[155,1231,1232],{},"Nginx error log contains the exact upstream target and failure type",[155,1234,1235],{},"Gunicorn logs show whether startup failed before binding",[155,1237,1238],{},"The expected socket file exists if using Unix sockets",[155,1240,1241],{},"The expected port is listening if using TCP",[155,1243,1244],{},"Nginx config matches the exact Gunicorn bind target",[155,1246,1247],{},"The loaded systemd unit uses the correct app path and virtualenv binary",[18,1249,1251],{"id":1250},"checklist","Checklist",[190,1253,1256,1265,1271,1281,1287,1296,1302],{"className":1254},[1255],"contains-task-list",[155,1257,1260,1264],{"className":1258},[1259],"task-list-item",[1261,1262],"input",{"disabled":416,"type":1263},"checkbox"," Gunicorn service is active and running under systemd",[155,1266,1268,1270],{"className":1267},[1259],[1261,1269],{"disabled":416,"type":1263}," Gunicorn is bound to the same socket path or TCP port used by Nginx",[155,1272,1274,1276,1277,1280],{"className":1273},[1259],[1261,1275],{"disabled":416,"type":1263}," ",[33,1278,1279],{},"\u002Frun\u002Fgunicorn.sock"," exists if using a Unix socket",[155,1282,1284,1286],{"className":1283},[1259],[1261,1285],{"disabled":416,"type":1263}," Nginx has permission to access the Gunicorn socket",[155,1288,1290,1276,1292,1295],{"className":1289},[1259],[1261,1291],{"disabled":416,"type":1263},[33,1293,1294],{},"nginx -t"," passes without errors",[155,1297,1299,1301],{"className":1298},[1259],[1261,1300],{"disabled":416,"type":1263}," Nginx has been reloaded after config changes",[155,1303,1305,1276,1307,1309,1310,1313,1314],{"className":1304},[1259],[1261,1306],{"disabled":416,"type":1263},[33,1308,884],{}," to the domain returns an application response instead of ",[33,1311,1312],{},"502"," or ",[33,1315,1316],{},"connection refused",[18,1318,1320],{"id":1319},"related-guides","Related Guides",[190,1322,1323,1329,1333,1337],{},[155,1324,1325],{},[319,1326,1328],{"href":1327},"\u002Fdeploy\u002Fdeploy-flask-with-nginx-plus-gunicorn-step-by-step-guide","Deploy Flask with Nginx + Gunicorn (Step-by-Step Guide)",[155,1330,1331],{},[319,1332,978],{"href":977},[155,1334,1335],{},[319,1336,322],{"href":321},[155,1338,1339],{},[319,1340,1342],{"href":1341},"\u002Fchecklist\u002Fflask-production-checklist-everything-you-must-do","Flask Production Checklist (Everything You Must Do)",[18,1344,1346],{"id":1345},"faq","FAQ",[1348,1349,1351],"h3",{"id":1350},"what-nginx-error-usually-confirms-this-issue","What Nginx error usually confirms this issue?",[14,1353,1354,1355,1357,1358,323],{},"Look for ",[33,1356,196],{}," in ",[33,1359,1360],{},"\u002Fvar\u002Flog\u002Fnginx\u002Ferror.log",[1348,1362,1364],{"id":1363},"how-do-i-know-whether-gunicorn-uses-a-socket-or-a-port","How do I know whether Gunicorn uses a socket or a port?",[14,1366,1367,1368,1370,1371,1373,1374,1313,1376,323],{},"Check the Gunicorn startup command or systemd ",[33,1369,651],{}," for a ",[33,1372,360],{}," value such as ",[33,1375,138],{},[33,1377,134],{},[1348,1379,1381],{"id":1380},"why-does-restarting-nginx-not-fix-it","Why does restarting Nginx not fix it?",[14,1383,1384],{},"Because the backend problem is usually Gunicorn not running, failing on startup, or listening on a different target.",[1348,1386,1388],{"id":1387},"can-i-switch-from-a-socket-to-a-tcp-port-to-debug-faster","Can I switch from a socket to a TCP port to debug faster?",[14,1390,1391,1392,1394,1395,776,1398,1400],{},"Yes. Binding Gunicorn to ",[33,1393,134],{}," can simplify testing with ",[33,1396,1397],{},"ss",[33,1399,884],{}," as long as Nginx is updated to proxy to that port.",[1348,1402,1404],{"id":1403},"what-should-i-check-after-a-server-reboot","What should I check after a server reboot?",[14,1406,1407],{},"Verify the Gunicorn service starts automatically, recreates the socket if used, and that Nginx still points to the correct path or port.",[18,1409,1411],{"id":1410},"final-takeaway","Final Takeaway",[14,1413,1414],{},"This issue is usually not in Nginx itself. It is almost always a mismatch between the Nginx upstream target and the actual Gunicorn bind, or a Gunicorn startup failure. Validate Gunicorn first, then confirm the socket or port matches Nginx, then reload and test the full request path.",[1416,1417,1418],"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}",{"title":31,"searchDepth":56,"depth":56,"links":1420},[1421,1422,1423,1424,1425,1426,1427,1428,1435],{"id":20,"depth":56,"text":21},{"id":142,"depth":56,"text":143},{"id":149,"depth":56,"text":150},{"id":981,"depth":56,"text":982},{"id":1059,"depth":56,"text":1060},{"id":1250,"depth":56,"text":1251},{"id":1319,"depth":56,"text":1320},{"id":1345,"depth":56,"text":1346,"children":1429},[1430,1431,1432,1433,1434],{"id":1350,"depth":68,"text":1351},{"id":1363,"depth":68,"text":1364},{"id":1380,"depth":68,"text":1381},{"id":1387,"depth":68,"text":1388},{"id":1403,"depth":68,"text":1404},{"id":1410,"depth":56,"text":1411},"Complete guide on fix: nginx not connecting to gunicorn (connection refused) for Flask production environments.","md",{"ogTitle":5,"ogDescription":1436,"twitterCard":1439,"robots":1440,"canonical":1441},"summary_large_image","index, follow","https:\u002F\u002Fflask-deployment.com\u002Ffix-issues\u002Ffix-nginx-not-connecting-to-gunicorn-connection-refused","\u002Ffix-issues\u002Ffix-nginx-not-connecting-to-gunicorn-connection-refused",{"title":5,"description":1436},"fix-issues\u002Ffix-nginx-not-connecting-to-gunicorn-connection-refused","B9ytVXECvN-KggZoDdS5oHfB6ng8IPfCVKppDfDwSNs",1776805765050]