You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求勿标记重复:Nginx出现Connection Refused Error 111及Bad Gateway故障

Troubleshooting Nginx Connection Refused (111) & Bad Gateway Issues with Your Python Project

Let’s walk through the most common fixes for these errors—they almost always boil down to service availability, socket permissions, or misconfigured paths.

1. Verify Your systemd Service is Running Correctly

First, make sure your myproject.service is active and free of startup errors:

  • Check the service status:

    systemctl status myproject.service
    

    Look for "active (running)" in the output. If it’s failed or inactive, dig into the logs to find the root cause:

    journalctl -u myproject.service -f
    

    This shows real-time logs—watch for issues like missing dependencies, incorrect paths to your wsgi.py or virtual environment, or uWSGI startup failures.

  • Double-check the ExecStart line in your myproject.service file:
    It should point directly to your virtual environment’s uWSGI binary and config file, like this:

    ExecStart=/home/youruser/myproject/venv/bin/uwsgi --ini myproject.ini
    

    Since you have duplicate wsgi.py files, confirm this points to the one with your application’s entry point.

2. Validate uWSGI & Socket Configuration

Your myproject.ini controls how uWSGI creates the socket—this is a frequent failure point:

  • Confirm the socket path matches what’s in your Nginx config:
    In myproject.ini, look for:

    socket = /path/to/your/myproject.sock
    

    Make sure this exact path is used in Nginx’s uwsgi_pass directive.

  • Fix socket permissions for Nginx access:
    Nginx typically runs as the www-data user, so uWSGI needs to create the socket with permissions that let Nginx read it. Add these lines to myproject.ini:

    chmod-socket = 664
    chown-socket = www-data:www-data
    

    Without this, the socket might be owned by root or your user, which Nginx can’t access—directly causing Connection Refused errors.

3. Fix Nginx Configuration Issues

First, a quick typo check: you wrote sites-availible—the correct directory is sites-available. If you created a link from the misspelled directory, fix it with:

sudo rm /etc/nginx/sites-enabled/myproject
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/

Now, verify your Nginx site config:

  • Ensure uwsgi_pass points to the correct socket path, and include critical uWSGI params:

    location / {
        uwsgi_pass unix:/path/to/your/myproject.sock;
        include uwsgi_params;
    }
    

    The include uwsgi_params; line is non-negotiable—it loads the headers needed for uWSGI-Nginx communication.

  • Test your Nginx config for syntax errors, then reload:

    sudo nginx -t
    

    If you see "test is successful", apply changes with:

    sudo systemctl reload nginx
    

4. Check Project Directory Permissions

Even if your socket permissions are correct, Nginx needs access to your project folder:

  • Grant read permissions for www-data:
    sudo chmod -R 755 /path/to/your/myproject
    sudo chown -R youruser:www-data /path/to/your/myproject
    
    This ensures Nginx can read static files (if you have them) and doesn’t hit permission blocks on parent directories.

5. Clean Up Duplicate Files

You mentioned duplicated files like wsgi.py. Take a minute to confirm which version your systemd service and uWSGI config are using, then delete unused duplicates. Extra files can lead to accidental misconfiguration down the line.


内容的提问来源于stack exchange,提问作者John

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:29:45