Python中通过域名加端口访问正常,但直接用域名访问出现504 Gateway Time-out错误如何解决?

nginx 的配置文件:

server {
    listen         80;
    server_name    127.0.0.1
    charset UTF-8;
    access_log      /var/log/nginx/myweb_access.log;
    error_log       /var/log/nginx/myweb_error.log;
client_max_body_size 75M;

location / {
    include uwsgi_params;
    uwsgi_pass 127.0.0.1:8023;
    uwsgi_read_timeout 2;
}
location /static {
    expires 30d;
    autoindex on;
    add_header Cache-Control private;
    alias /home/work/JZAssist/collected_static/;
 }

}

我的 supervisord 中的 command 是

command=uwsgi --http :8023 --chdir /home/work/xxxx --module xxxx.wsgi

xxxx 是项目名称。 问题是可以通过域名加端口访问,但是直接用域名访问就出现 504 Gateway Time-out 。我重装过 nginx,但看起来好像并不是 nginx 的问题。


Python中通过域名加端口访问正常,但直接用域名访问出现504 Gateway Time-out错误如何解决?

19 回复

uwsgi_read_timeout 好像小了点


这个问题通常是因为你的服务监听在特定端口(比如8080),但直接访问域名时默认走的是80端口,而80端口上没有服务在响应。

检查一下你的Web服务器配置。如果你用的是Nginx或Apache做反向代理,需要确保代理配置正确地将80端口的请求转发到应用实际运行的端口。比如Nginx的配置里要有类似这样的设置:

server {
    listen 80;
    server_name yourdomain.com;
    
    location / {
        proxy_pass http://localhost:8080;  # 转发到实际应用端口
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

如果是云服务,还要检查安全组/防火墙是否开放了80端口。用netstat -tuln看看80端口有没有在监听。

总结:检查反向代理配置和端口监听情况。

你域名解析到哪个 ip ?
是不是没监听到…

就只有一个 ip 。为什么域名加端口就可以访问?

之前设置成这样也没问题。

sever name 不应该是写域名么?

可以这样写吧。

如果实在不行就换 gunicorn ,之前也是这样配置,都没有问题。

你的 uwsgi 好像是开了个 8023 的 HTTP 端口,你 nginx 居然尝试用 uswsgi 协议连?

include uwsgi_params;
uwsgi_pass 127.0.0.1:8023;
uwsgi_read_timeout 2;

把 uwsgi 换成 proxy 吧,你这个需要用 proxy_pass 127.0.0.1:8023;

如果 nginx 配置上写的是 uwsgi_pass 那么 uwsgi 启动的命令行参数应该写成 socket 而不是 http
command=uwsgi --socket :8023 --chdir /home/work/xxxx --module xxxx.wsgi



确实和你们说的一样。
可能我在配置的时候有点混乱,然后把不同操作的命令混进来了。
谢谢!!!

不贴日志都能猜出来,服你们!

server {
listen 80;
server_name 127.0.0.1

}



这三行的意思可能 LZ 没懂,我帮你翻译一下:只有 http://127.0.0.1:80/ 这种形式的网址才能被这个 server { } 块里的配置匹配到。

如果你配置了个域名,不知道你的 server_name 飞到什么鬼地方去了。

看来还要系统学习下才行。谢谢啦。

你应该好好看看 nignx 文档:“ If a server is the only server for a listen port, then nginx will not test server names at all (and will not build the hash tables for the listen port). However, there is one exception. If a server name is a regular expression with captures, then nginx has to execute the expression to get the captures.”

你觉得 LZ 这配置就是你贴文档里所说的 the only server 么。

我记得当时我在部署 django+uWSGI+Nginx 的时候也出现过这个问题,但最后是怎么解决的有些忘了。。你看一下官方文档步骤,再重来一遍: http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

这个可以作为面试题,挺好的。

回到顶部