【问题标题】:Debugging 404 error with nginx+uwsgi+flask on Ubuntu在 Ubuntu 上使用 nginx+uwsgi+flask 调试 404 错误
【发布时间】:2021-05-24 06:25:13
【问题描述】:

我正在为我认为是 nginx 的配置错误寻求帮助...

我的 uwsgi .ini 文件位于 /var/www/mysite.com/public_html/api/calc:

[uwsgi]
#application's base folder
base = /var/www/mysite.com/public_html/api/calc

#python module to import
app = hello
module = %(app)

home = %(base)/venv
pythonpath = %(base)

#socket file's location
socket = /var/www/mysite.com/public_html/api/calc/%n.sock

#permissions for the socket file
chmod-socket    = 666

#the variable that holds a flask application inside the module imported at line #6
callable = app

#location of log files
logto = /var/log/uwsgi/%n.log

我的 nginx 配置使用letsencrypt/certbot 来服务两个域。 mysite.com 域也设置为提供 api 子域。相关的 nginx server 块是:

server {
      server_name api.mysite.com;

     root /var/www/mysite.com/public_html/api;

     index index.html index.htm;

     location / {
        try_files $uri $uri/ =404;
     }

     location /calc/ {
        include uwsgi_params;
        uwsgi_pass unix:/var/www/mysite.com/public_html/api/calc/calc_uwsgi.sock;
     }


    listen [::]:443 ssl; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mysite.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

我正在尝试通过添加一个 location 块来创建位于 api.mysite.com/calc/ 的 API,该块指定 /var/www/mysite.com/public_html 中定义的 uwsgi 网关/api/calc.

在运行uwsgi时,当我访问https://api.mysite.com/calc时,uwsgi日志文件显示我的python正在被调用并正在生成响应数据:

[pid: 34683|app: 0|req: 24/24] 192.168.1.1 () {56 vars in 1127 bytes} [Sun Feb 21 18:26:01 2021] GET /calc/ => generated 232 bytes in 2 msecs (HTTP/1.1 404) 2 headers in 87 bytes (1 switches on core 0)

nginx access.log 显示访问:

192.168.1.1 - - [21/Feb/2021:18:50:33 -0500] "GET /calc/ HTTP/1.1" 404 209 "-" "Mozilla/5.0 (X11; CrOS x86_64 13505.73.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.109 Safari/537.36"

但客户端返回 404 not found 错误。 nginx error.log 文件中没有条目。

(显然)我不知道自己在做什么。我在 nginx 配置中尝试了很多不同的东西,但都不喜欢。

任何建议表示赞赏!

更新

我曾假设使用此配置,对 app.mysite.com/calc 的请求将由我的 python 中的“/”路由提供服务,但看起来它实际上由“/calc/”路由提供服务:

@app.route("/")
def hello():
    return "Hello World - '/' route "

@app.route("/calc/")
def hellocalc():
    return "Hello world - '/calc/' route "

有没有办法配置它,以便我的 python 不需要在其所有路由中包含“calc”?

【问题讨论】:

    标签: nginx flask uwsgi


    【解决方案1】:

    我使用内部路由 route-uri 指令让这个工作

    [uwsgi]
    #try rewrite routing
    route-uri = ^/calc/?(.*)$ rewrite:/
    
    

    这需要在安装 pcre 后重新安装 uwsgi:

    sudo apt-get install libpcre3 libpcre
    pip3 install -I --no-cache-dir  uwsgi
    

    但这里还有一个问题:我没有用 sudo 安装 uwsgi,所以现在它存在

    /home/me/.local/bin/uwsgi
    
    

    这是好事还是坏事?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-31
      • 2022-01-09
      • 1970-01-01
      • 2019-09-29
      • 2020-10-29
      • 2019-09-29
      • 1970-01-01
      • 2018-08-17
      相关资源
      最近更新 更多