在Ubuntu上使用nginx+uwsgi+flask调试404错误

问题描述 投票:0回答:2

我正在寻求帮助,以解决我认为 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/api/calc 中定义的 uwsgi 网关。

运行 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
2个回答
0
投票

我使用内部路由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

这是好是坏?


0
投票

您可以更改权限,例如 sudo chmod 775 -R /var/www/mysite.com/public_html/api/calc/ 在应用程序的工作目录 /var/www/mysite.com/public_html/api/calc 上也相同之后在您的 .ini 文件 chmod-socket = 660 之后,不要运行 certbot 而不进行进一步研究,因为该服务器开始返回 404 错误。此外,无论在 ///.local/bin/uwsgi 中使用 uwsgi 还是传递它的位置你的 .ini 文件

© www.soinside.com 2019 - 2024. All rights reserved.